1/**************** json H Declares Source Code File (.H) ****************/
2/* Name: json.h Version 1.2 */
3/* */
4/* (C) Copyright to the author Olivier BERTRAND 2014 - 2017 */
5/* */
6/* This file contains the JSON classes declares. */
7/***********************************************************************/
8#include "value.h"
9
10#if defined(_DEBUG)
11#define X assert(false);
12#else
13#define X
14#endif
15
16enum JTYP {TYPE_NULL = TYPE_VOID,
17 TYPE_STRG = TYPE_STRING,
18 TYPE_DBL = TYPE_DOUBLE,
19 TYPE_BOOL = TYPE_TINY,
20 TYPE_BINT = TYPE_BIGINT,
21 TYPE_DTM = TYPE_DATE,
22 TYPE_INTG = TYPE_INT,
23 TYPE_VAL = 12,
24 TYPE_JSON,
25 TYPE_JAR,
26 TYPE_JOB,
27 TYPE_JVAL};
28
29class JOUT;
30class JSON;
31class JMAP;
32class JVALUE;
33class JOBJECT;
34class JARRAY;
35
36typedef class JPAIR *PJPR;
37typedef class JSON *PJSON;
38typedef class JVALUE *PJVAL;
39typedef class JOBJECT *PJOB;
40typedef class JARRAY *PJAR;
41
42typedef struct {
43 char *str;
44 int len;
45 } STRG, *PSG;
46
47char *NextChr(PSZ s, char sep);
48char *GetJsonNull(void);
49
50PJSON ParseJson(PGLOBAL g, char *s, int n, int *prty = NULL, bool *b = NULL);
51PJAR ParseArray(PGLOBAL g, int& i, STRG& src, bool *pty);
52PJOB ParseObject(PGLOBAL g, int& i, STRG& src, bool *pty);
53PJVAL ParseValue(PGLOBAL g, int& i, STRG& src, bool *pty);
54char *ParseString(PGLOBAL g, int& i, STRG& src);
55PVAL ParseNumeric(PGLOBAL g, int& i, STRG& src);
56PSZ Serialize(PGLOBAL g, PJSON jsp, char *fn, int pretty);
57bool SerializeArray(JOUT *js, PJAR jarp, bool b);
58bool SerializeObject(JOUT *js, PJOB jobp);
59bool SerializeValue(JOUT *js, PJVAL jvp);
60char *NextChr(PSZ s, char sep);
61DllExport bool IsNum(PSZ s);
62
63/***********************************************************************/
64/* Class JOUT. Used by Serialize. */
65/***********************************************************************/
66class JOUT : public BLOCK {
67 public:
68 JOUT(PGLOBAL gp) : BLOCK() {g = gp; Pretty = 3;}
69
70 virtual bool WriteStr(const char *s) = 0;
71 virtual bool WriteChr(const char c) = 0;
72 virtual bool Escape(const char *s) = 0;
73 int Prty(void) {return Pretty;}
74
75 // Member
76 PGLOBAL g;
77 int Pretty;
78}; // end of class JOUT
79
80/***********************************************************************/
81/* Class JOUTSTR. Used to Serialize to a string. */
82/***********************************************************************/
83class JOUTSTR : public JOUT {
84 public:
85 JOUTSTR(PGLOBAL g);
86
87 virtual bool WriteStr(const char *s);
88 virtual bool WriteChr(const char c);
89 virtual bool Escape(const char *s);
90
91 // Member
92 char *Strp; // The serialized string
93 size_t N; // Position of next char
94 size_t Max; // String max size
95}; // end of class JOUTSTR
96
97/***********************************************************************/
98/* Class JOUTFILE. Used to Serialize to a file. */
99/***********************************************************************/
100class JOUTFILE : public JOUT {
101 public:
102 JOUTFILE(PGLOBAL g, FILE *str, int pty) : JOUT(g) {Stream = str; Pretty = pty;}
103
104 virtual bool WriteStr(const char *s);
105 virtual bool WriteChr(const char c);
106 virtual bool Escape(const char *s);
107
108 // Member
109 FILE *Stream;
110}; // end of class JOUTFILE
111
112/***********************************************************************/
113/* Class JOUTPRT. Used to Serialize to a pretty file. */
114/***********************************************************************/
115class JOUTPRT : public JOUTFILE {
116 public:
117 JOUTPRT(PGLOBAL g, FILE *str) : JOUTFILE(g, str, 2) {M = 0; B = false;}
118
119 virtual bool WriteStr(const char *s);
120 virtual bool WriteChr(const char c);
121
122 // Member
123 int M;
124 bool B;
125}; // end of class JOUTPRT
126
127/***********************************************************************/
128/* Class PAIR. The pairs of a json Object. */
129/***********************************************************************/
130class JPAIR : public BLOCK {
131 friend class JOBJECT;
132 friend class JSNX;
133 friend PJOB ParseObject(PGLOBAL, int&, STRG&, bool*);
134 friend bool SerializeObject(JOUT *, PJOB);
135 public:
136 JPAIR(PCSZ key) : BLOCK() {Key = key; Val = NULL; Next = NULL;}
137
138 inline PCSZ GetKey(void) {return Key;}
139 inline PJVAL GetVal(void) {return Val;}
140 inline PJPR GetNext(void) {return Next;}
141
142 protected:
143 PCSZ Key; // This pair key name
144 PJVAL Val; // To the value of the pair
145 PJPR Next; // To the next pair
146}; // end of class JPAIR
147
148/***********************************************************************/
149/* Class JSON. The base class for all other json classes. */
150/***********************************************************************/
151class JSON : public BLOCK {
152 public:
153 JSON(void) {Size = 0;}
154
155 int size(void) {return Size;}
156 virtual int GetSize(bool b) {return Size;}
157 virtual void Clear(void) {Size = 0;}
158 virtual JTYP GetType(void) {return TYPE_JSON;}
159 virtual JTYP GetValType(void) {X return TYPE_JSON;}
160 virtual void InitArray(PGLOBAL g) {X}
161//virtual PJVAL AddValue(PGLOBAL g, PJVAL jvp = NULL, int *x = NULL) {X return NULL;}
162 virtual PJPR AddPair(PGLOBAL g, PCSZ key) {X return NULL;}
163 virtual PJAR GetKeyList(PGLOBAL g) {X return NULL;}
164 virtual PJAR GetValList(PGLOBAL g) {X return NULL;}
165 virtual PJVAL GetValue(const char *key) {X return NULL;}
166 virtual PJOB GetObject(void) {return NULL;}
167 virtual PJAR GetArray(void) {return NULL;}
168 virtual PJVAL GetValue(int i) {X return NULL;}
169 virtual PVAL GetValue(void) {X return NULL;}
170 virtual PJSON GetJsp(void) { X return NULL; }
171 virtual PJSON GetJson(void) { X return NULL; }
172 virtual PJPR GetFirst(void) {X return NULL;}
173 virtual int GetInteger(void) {X return 0;}
174 virtual double GetFloat() {X return 0.0;}
175 virtual PSZ GetString(PGLOBAL g) {X return NULL;}
176 virtual PSZ GetText(PGLOBAL g, PSZ text) {X return NULL;}
177 virtual bool Merge(PGLOBAL g, PJSON jsp) { X return true; }
178 virtual bool SetValue(PGLOBAL g, PJVAL jvp, int i) { X return true; }
179 virtual void SetValue(PGLOBAL g, PJVAL jvp, PCSZ key) {X}
180 virtual void SetValue(PVAL valp) {X}
181 virtual void SetValue(PJSON jsp) {X}
182 virtual void SetString(PGLOBAL g, PSZ s, short c) {X}
183 virtual void SetInteger(PGLOBAL g, int n) {X}
184 virtual void SetFloat(PGLOBAL g, double f) {X}
185 virtual void DeleteKey(PCSZ k) {X}
186 virtual bool DeleteValue(int i) {X return true;}
187 virtual bool IsNull(void) {X return true;}
188
189 protected:
190 int Size;
191}; // end of class JSON
192
193/***********************************************************************/
194/* Class JOBJECT: contains a list of value pairs. */
195/***********************************************************************/
196class JOBJECT : public JSON {
197 friend PJOB ParseObject(PGLOBAL, int&, STRG&, bool*);
198 friend bool SerializeObject(JOUT *, PJOB);
199 friend class JSNX;
200 public:
201 JOBJECT(void) : JSON() {First = Last = NULL;}
202
203 using JSON::GetValue;
204 using JSON::SetValue;
205 virtual void Clear(void) {First = Last = NULL; Size = 0;}
206 virtual JTYP GetType(void) {return TYPE_JOB;}
207 virtual PJPR GetFirst(void) {return First;}
208 virtual int GetSize(bool b);
209 virtual PJPR AddPair(PGLOBAL g, PCSZ key);
210 virtual PJOB GetObject(void) {return this;}
211 virtual PJVAL GetValue(const char* key);
212 virtual PJAR GetKeyList(PGLOBAL g);
213 virtual PJAR GetValList(PGLOBAL g);
214 virtual PSZ GetText(PGLOBAL g, PSZ text);
215 virtual bool Merge(PGLOBAL g, PJSON jsp);
216 virtual void SetValue(PGLOBAL g, PJVAL jvp, PCSZ key);
217 virtual void DeleteKey(PCSZ k);
218 virtual bool IsNull(void);
219
220 protected:
221 PJPR First;
222 PJPR Last;
223}; // end of class JOBJECT
224
225/***********************************************************************/
226/* Class JARRAY. */
227/***********************************************************************/
228class JARRAY : public JSON {
229 friend PJAR ParseArray(PGLOBAL, int&, STRG&, bool*);
230 public:
231 JARRAY(void) : JSON() {Alloc = 0; First = Last = NULL; Mvals = NULL;}
232
233 using JSON::GetValue;
234 using JSON::SetValue;
235 virtual void Clear(void) {First = Last = NULL; Size = 0;}
236 virtual JTYP GetType(void) {return TYPE_JAR;}
237 virtual PJAR GetArray(void) {return this;}
238 virtual int GetSize(bool b);
239 PJVAL AddValue(PGLOBAL g, PJVAL jvp = NULL, int *x = NULL);
240 virtual void InitArray(PGLOBAL g);
241 virtual PJVAL GetValue(int i);
242 virtual PSZ GetText(PGLOBAL g, PSZ text);
243 virtual bool Merge(PGLOBAL g, PJSON jsp);
244 virtual bool SetValue(PGLOBAL g, PJVAL jvp, int i);
245 virtual bool DeleteValue(int n);
246 virtual bool IsNull(void);
247
248 protected:
249 // Members
250 int Alloc; // The Mvals allocated size
251 PJVAL First; // Used when constructing
252 PJVAL Last; // Last constructed value
253 PJVAL *Mvals; // Allocated when finished
254}; // end of class JARRAY
255
256/***********************************************************************/
257/* Class JVALUE. */
258/***********************************************************************/
259class JVALUE : public JSON {
260 friend class JARRAY;
261 friend class JSNX;
262 friend class JSONCOL;
263 friend PJVAL ParseValue(PGLOBAL, int&, STRG&, bool*);
264 friend bool SerializeValue(JOUT *, PJVAL);
265 public:
266 JVALUE(void) : JSON() {Clear();}
267 JVALUE(PJSON jsp);
268 JVALUE(PGLOBAL g, PVAL valp);
269 JVALUE(PGLOBAL g, PCSZ strp);
270
271 using JSON::GetValue;
272 using JSON::SetValue;
273 virtual void Clear(void)
274 {Jsp = NULL; Value = NULL; Next = NULL; Del = false; Size = 1;}
275 virtual JTYP GetType(void) {return TYPE_JVAL;}
276 virtual JTYP GetValType(void);
277 virtual PJOB GetObject(void);
278 virtual PJAR GetArray(void);
279 virtual PVAL GetValue(void) {return Value;}
280 virtual PJSON GetJsp(void) {return Jsp;}
281 virtual PJSON GetJson(void) { return (Jsp ? Jsp : this); }
282 virtual int GetInteger(void);
283 virtual long long GetBigint(void);
284 virtual double GetFloat(void);
285 virtual PSZ GetString(PGLOBAL g);
286 virtual PSZ GetText(PGLOBAL g, PSZ text);
287 virtual void SetValue(PJSON jsp);
288 virtual void SetValue(PVAL valp) { Value = valp; Jsp = NULL; }
289 virtual void SetString(PGLOBAL g, PSZ s, short c = 0);
290 virtual void SetInteger(PGLOBAL g, int n);
291 virtual void SetBigint(PGLOBAL g, longlong ll);
292 virtual void SetFloat(PGLOBAL g, double f);
293 virtual void SetTiny(PGLOBAL g, char f);
294 virtual bool IsNull(void);
295
296 protected:
297 PJSON Jsp; // To the json value
298 PVAL Value; // The numeric value
299 PJVAL Next; // Next value in array
300 bool Del; // True when deleted
301}; // end of class JVALUE
302