Barracuda Application Server C/C++ Reference
NO
JParser.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Application Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: JParser.h 5839 2026-07-29 13:27:22Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2006-2026
17 *
18 * This software is copyrighted by and is the sole property of Real
19 * Time Logic LLC. All rights, title, ownership, or other interests in
20 * the software remain the property of Real Time Logic LLC. This
21 * software may only be used in accordance with the terms and
22 * conditions stipulated in the corresponding license agreement under
23 * which the software has been supplied. Any unauthorized use,
24 * duplication, transmission, distribution, or disclosure of this
25 * software is expressly forbidden.
26 *
27 * This Copyright notice may not be removed or modified without prior
28 * written consent of Real Time Logic LLC.
29 *
30 * Real Time Logic LLC. reserves the right to modify this software
31 * without notice.
32 *
33 * http://realtimelogic.com
34 ****************************************************************************
35 *
36 */
37
38#ifndef __JParser_h
39#define __JParser_h
40
41
42#include <BaAtoi.h>
43#include <AllocatorIntf.h>
44
49struct JParserIntf;
50struct JParserVal;
51
57typedef int (*JParserIntf_Service)(
58 struct JParserIntf* o, struct JParserVal* v, int recLevel);
59
60
64typedef struct JParserIntf
65{
66#ifdef __cplusplus
70 JParserIntf(JParserIntf_Service s) { service = s; }
71
77 int serviceCB(JParserVal* v, int recLevel);
78
79 ~JParserIntf(){}
80 JParserIntf() {}
81#endif
82 JParserIntf_Service service;
84
85#define JParserIntf_constructor(o,serviceMA) (o)->service=serviceMA
86
87#define JParserIntf_serviceCB(o, v, recLev) (o)->service(o,v,recLev)
88
89#ifdef __cplusplus
90
91inline int JParserIntf::serviceCB(JParserVal* v, int recLevel) {
92 return JParserIntf_serviceCB(this,v, recLevel); }
93#endif
94 /* end of JSONCB */
96
103typedef enum {
128
131typedef enum
132{
151
154typedef struct JErr
155{
156#ifdef __cplusplus
158 JErr();
160 void reset();
162 bool isError();
164 bool noError();
166 JErrT getErrT();
170 JVType getExpT();
174 JVType getRecT();
175 int setTooFewParams();
176 int setTypeErr(JVType expT, JVType recT);
177 int setError(JErrT err,const char* msg);
178#endif
179 const char* msg;
180 JErrT err;
181 JVType expType;
182 JVType recType;
184#ifdef __cplusplus
185extern "C" {
186#endif
187#define JErr_constructor(o) (o)->err=JErrT_NoErr
188#define JErr_reset(o) (o)->err=JErrT_NoErr, (o)->msg=0
189#define JErr_isError(o) ((o)->err!=JErrT_NoErr)
190#define JErr_noError(o) ((o)->err==JErrT_NoErr)
191#define JErr_getErrT(o) (o)->err
192#define JErr_getExpT(o) (o)->expType
193#define JErr_getRecT(o) (o)->recType
194BA_API int JErr_setTooFewParams(JErr* o);
195BA_API int JErr_setTypeErr(JErr* o, JVType expT, JVType recT);
196BA_API int JErr_setError(JErr* o,JErrT err,const char* msg);
197#ifdef __cplusplus
198}
199inline JErr::JErr() {
200 JErr_constructor(this); }
201inline void JErr::reset() {
202 JErr_reset(this); }
203inline bool JErr::isError() {
204 return JErr_isError(this) ? true : false;}
205inline bool JErr::noError() {
206 return JErr_noError(this) ? true : false;}
208 return JErr_getErrT(this);}
210 return JErr_getExpT(this);}
212 return JErr_getRecT(this);}
213inline int JErr::setTooFewParams() {
214 return JErr_setTooFewParams(this);}
215inline int JErr::setTypeErr(JVType expT, JVType recT) {
216 return JErr_setTypeErr(this, expT, recT);}
217inline int JErr::setError(JErrT e,const char* message) {
218 return JErr_setError(this,e, message);}
219#endif
220
221
222
223#ifndef __DOXYGEN__
224typedef struct
225{
226 AllocatorIntf* alloc;
227 U8* buf;
228 U32 index;
229 size_t size;
230} JDBuf;
231
232
233/* JLextT: JSON Lexer Types.
234 The following types are used by the lexer and parser.
235*/
236typedef enum {
237 JLexerT_Null,
238 JLexerT_Boolean,
239 JLexerT_Number,
240 JLexerT_String,
241 JLexerT_BeginObject,
242 JLexerT_BeginArray,
243 JLexerT_EndObject,
244 JLexerT_EndArray,
245 JLexerT_Comma, /* ',' Array or object list comma. */
246 JLexerT_MemberSep, /* ':' for string : value */
247 JLexerT_NeedMoreData, /* Lexer not completed with current token. */
248 JLexerT_ParseErr,
249 JLexerT_MemErr
250} JLexerT;
251
252
253
254typedef enum {
255 JLexerSt_StartComment,
256 JLexerSt_EatComment,
257 JLexerSt_EndComment,
258 JLexerSt_EatCppComment,
259 JLexerSt_TrueFalseNull,
260 JLexerSt_String,
261 JLexerSt_StringEscape,
262 JLexerSt_StringUnicode,
263 JLexerSt_StringSurrogateEscape,
264 JLexerSt_StringSurrogateU,
265 JLexerSt_StringUtf8,
266 JLexerSt_Number,
267 JLexerSt_GetNextToken
268} JLexerSt;
269
270
271typedef struct
272{
273 JDBuf* asmB;
274 const U8* bufStart;
275 const U8* bufEnd;
276 const U8* tokenPtr;
277
278 U32 unicode;
279 S16 unicodeShift;
280 U16 surrogate;
281 U8 utf8Len;
282
283 /* typeChkPtr and retVal is used if the Lexer finds the start of
284 true, false, or null.
285 */
286 const U8* typeChkPtr;
287 U8 retVal;
288
289 U8 state; /* JLexerSt */
290
291 /* state for string or number.
292 If in state number, 0 means positive number and 255 means neg.
293 If in state String, sn is ' or ".
294 If in state Boolean: true or false.
295 */
296 U8 sn;
297 U8 isDouble; /* Used when reading a number */
298} JLexer;
299
300#endif /* __DOXYGEN__ */
301
304typedef enum {
315 JParserT_EndArray = ']'
317
321typedef struct JParserVal
322{
325 union
326 {
327 char* s;
328#ifndef NO_DOUBLE
329 double f;
330#endif
331 S32 d;
332 U64 l;
333 BaBool b;
334 } v;
335
351 size_t stringLen;
356
357
360typedef enum {
361
365
370
371
374
378
382
386
390
391
392typedef enum {
393 JParserSt_StartObj,
394 JParserSt_BeginArray,
395 JParserSt_MemberName,
396 JParserSt_MemberSep,
397 JParserSt_Value,
398 JParserSt_EndObj,
399 JParserSt_Comma
400} JParserSt;
401
402
403#define JPARSER_STACK_LEN 8
404
407
414{
415#ifdef __cplusplus
430 JParser(JParserIntf* intf, char* nameBuf, int namebufSize,
431 AllocatorIntf* alloc, int extraStackLen=0);
432
443 int parse(const U8* buf, U32 size);
444
446 ~JParser();
447
453#endif
454 JLexer lexer;
455 JParserVal val;
456 JDBuf asmB; /* Assembling various values */
457 JDBuf mnameB; /* Assembling object member names */
458 JParserIntf* intf;
459 S16 stackIx;
460 S16 stackSize;
461 U8 status; /* JParsStat */
462 U8 state; /* JParserSt */
463 /* It's possible to extend the stack size by reserving
464 * N*JParserStackNode bytes immediately following the memory for
465 * this struct instance. N is then used as 'extraStackLen' in constructor.
466 */
467 JParserStackNode stack[JPARSER_STACK_LEN];
468};
469
470typedef struct JParser JParser;
471
472#ifdef __cplusplus
473extern "C" {
474#endif
475BA_API void JParser_constructor(JParser* o, JParserIntf* intf, char* nameBuf,
476 int namebufSize, AllocatorIntf* alloc,
477 int extraStackLen);
478BA_API int JParser_parse(JParser* o, const U8* buf, U32 size);
479BA_API void JParser_destructor(JParser* o);
480#define JParser_getStatus(o) ((JParsStat)(o)->status)
481#ifdef __cplusplus
482}
483inline JParser::JParser(JParserIntf* intf, char* nameBuf,
484 int namebufSize, AllocatorIntf* alloc,
485 int extraStackLen) {
486 JParser_constructor(this, intf, nameBuf, namebufSize, alloc, extraStackLen);
487}
488inline int JParser::parse(const U8* buf, U32 size) {
489 return JParser_parse(this, buf, size);}
491 JParser_destructor(this);}
493 return JParser_getStatus(this);}
494#endif
495 /* end of JSONRef */
497
498#endif
int serviceCB(JParserVal *v, int recLevel)
Calling the JParserIntf service function is typically performed in a design using a chained parser ca...
Definition: JParser.h:91
int(* JParserIntf_Service)(struct JParserIntf *o, struct JParserVal *v, int recLevel)
The parser callback function.
Definition: JParser.h:57
struct JParserIntf JParserIntf
The JParserIntf interface class is the interface between the parser and an object that implements the...
struct JParserVal JParserVal
The parser sets a JParserVal before calling the parser callback JParserIntf.
struct JErr JErr
The JSON error container object.
JParser(JParserIntf *intf, char *nameBuf, int namebufSize, AllocatorIntf *alloc, int extraStackLen=0)
Create a JSON parser object.
Definition: JParser.h:483
JParsStat getStatus()
Returns the parser status.
Definition: JParser.h:492
JParsStat
JSON Parser Status.
Definition: JParser.h:360
JVType getRecT()
Returns the parsed type if this is a parse error.
Definition: JParser.h:211
JErrT
JSON error codes.
Definition: JParser.h:132
~JParser()
Terminate and release the internal buffers.
Definition: JParser.h:490
void reset()
Reset – remove any error so the object can be reused.
Definition: JParser.h:201
bool noError()
Returns true if no error.
Definition: JParser.h:205
JErrT getErrT()
Returns the error type.
Definition: JParser.h:207
int parse(const U8 *buf, U32 size)
Parse a JSON text chunk.
Definition: JParser.h:488
JVType
The JSON types.
Definition: JParser.h:103
JParserT
Type 't' in JParserVal.
Definition: JParser.h:304
JVType getExpT()
Returns the expected type if this is a parse error.
Definition: JParser.h:209
U8 JParserStackNode
The stack used internally by JParser.
Definition: JParser.h:406
JErr()
Create a new object.
Definition: JParser.h:199
bool isError()
Returns true if the error flag is set.
Definition: JParser.h:203
@ JParsStat_ParseErr
JSON Parse error.
Definition: JParser.h:377
@ JParsStat_IntfErr
The JParserIntf interface reported a problem.
Definition: JParser.h:381
@ JParsStat_DoneEOS
The parser completed parsing a new JSON object.
Definition: JParser.h:364
@ JParsStat_NeedMoreData
The parser needs more data to complete.
Definition: JParser.h:373
@ JParsStat_StackOverflow
Object nested too deep.
Definition: JParser.h:388
@ JParsStat_Done
The parser completed parsing a new JSON object, but found the start of a new object in the provided b...
Definition: JParser.h:369
@ JParsStat_MemErr
Memory allocation error.
Definition: JParser.h:385
@ JErrT_InvalidMethodParams
Invalid RPC method parameters.
Definition: JParser.h:142
@ JErrT_NoErr
No error.
Definition: JParser.h:134
@ JErrT_MemErr
Memory allocation error.
Definition: JParser.h:147
@ JErrT_FmtValErr
A format error in JEncoder or JVal::get.
Definition: JParser.h:145
@ JErrT_WrongType
Unexpected JSON type when parsing.
Definition: JParser.h:140
@ JErrT_JsonErr
Generic error.
Definition: JParser.h:136
@ JErrT_IOErr
I/O error.
Definition: JParser.h:149
@ JVType_Null
JSON NULL value.
Definition: JParser.h:122
@ JVType_Int
JSON Number encoded as an integer.
Definition: JParser.h:115
@ JVType_Double
JSON Number encoded as a float.
Definition: JParser.h:112
@ JVType_Object
The JVAL is of type JSON object.
Definition: JParser.h:124
@ JVType_String
JSON string.
Definition: JParser.h:109
@ JVType_Long
JSON Number encoded as a long integer.
Definition: JParser.h:118
@ JVType_Array
The JVAL is of type JSON array.
Definition: JParser.h:126
@ JVType_Boolean
JSON boolean.
Definition: JParser.h:120
@ JVType_InvalidType
Invalid JSON type.
Definition: JParser.h:106
@ JParserT_BeginObject
BeginObject.
Definition: JParser.h:312
@ JParserT_Long
64-bit (long long)
Definition: JParser.h:310
@ JParserT_String
String.
Definition: JParser.h:307
@ JParserT_EndArray
EndArray.
Definition: JParser.h:315
@ JParserT_EndObject
EndObject.
Definition: JParser.h:314
@ JParserT_Boolean
Boolean.
Definition: JParser.h:311
@ JParserT_InvalidType
InvalidType.
Definition: JParser.h:305
@ JParserT_BeginArray
BeginArray.
Definition: JParser.h:313
@ JParserT_Null
Null.
Definition: JParser.h:306
@ JParserT_Double
Double.
Definition: JParser.h:308
@ JParserT_Int
Int.
Definition: JParser.h:309
Memory allocation and deallocation Interface class.
Definition: AllocatorIntf.h:83
The JSON error container object.
Definition: JParser.h:155
The JParserIntf interface class is the interface between the parser and an object that implements the...
Definition: JParser.h:65
JParserIntf(JParserIntf_Service s)
Create the callback interface object.
Definition: JParser.h:70
The parser sets a JParserVal before calling the parser callback JParserIntf.
Definition: JParser.h:322
union JParserVal::@1 v
A union controlled by the type 't'.
size_t memberNameLen
Object member-name length.
Definition: JParser.h:352
JParserT t
The type controlling 'v'.
Definition: JParser.h:354
S32 d
If integer.
Definition: JParser.h:331
BaBool memberNameSet
True when this value is an object member.
Definition: JParser.h:353
char * memberName
object member name is set for objects.
Definition: JParser.h:350
size_t stringLen
String length when t is JParserT_String.
Definition: JParser.h:351
U64 l
If long integer.
Definition: JParser.h:332
double f
If number of type double.
Definition: JParser.h:329
char * s
If string.
Definition: JParser.h:327
The JSON parser parses a JSON stream and calls the JParserIntf callback interface for each parsed obj...
Definition: JParser.h:414