Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
JParser.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Application Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: JParser.h 5978 2026-09-11 16:13:48Z 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
40#ifndef __JParser_h
41#define __JParser_h
42
43
44#include <BaAtoi.h>
45#include <AllocatorIntf.h>
46
51struct JParserIntf;
52struct JParserVal;
53
64typedef int (*JParserIntf_Service)(
65 struct JParserIntf* o, struct JParserVal* v, int recLevel);
66
67
71typedef struct JParserIntf
72{
73#ifdef __cplusplus
77 JParserIntf(JParserIntf_Service s) { service = s; }
78
85 int serviceCB(JParserVal* v, int recLevel);
86
87 ~JParserIntf(){}
88 JParserIntf() {}
89#endif
90 JParserIntf_Service service;
92
97#define JParserIntf_constructor(o,serviceMA) (o)->service=serviceMA
98
105#define JParserIntf_serviceCB(o, v, recLev) (o)->service(o,v,recLev)
106
107#ifdef __cplusplus
108
109inline int JParserIntf::serviceCB(JParserVal* v, int recLevel) {
110 return JParserIntf_serviceCB(this,v, recLevel); }
111#endif
112 /* end of JSONCB */
114
121typedef enum {
146
149typedef enum
150{
169
172typedef struct JErr
173{
174#ifdef __cplusplus
178 JErr();
182 void reset();
185 bool isError();
188 bool noError();
191 JErrT getErrT();
194 JVType getExpT();
197 JVType getRecT();
201 int setTooFewParams();
207 int setTypeErr(JVType expT, JVType recT);
213 int setError(JErrT err,const char* msg);
214#endif
216 const char* msg;
217 JErrT err;
218 JVType expType;
219 JVType recType;
221#ifdef __cplusplus
222extern "C" {
223#endif
227#define JErr_constructor(o) (o)->err=JErrT_NoErr
231#define JErr_reset(o) (o)->err=JErrT_NoErr, (o)->msg=0
235#define JErr_isError(o) ((o)->err!=JErrT_NoErr)
239#define JErr_noError(o) ((o)->err==JErrT_NoErr)
243#define JErr_getErrT(o) (o)->err
247#define JErr_getExpT(o) (o)->expType
251#define JErr_getRecT(o) (o)->recType
259BA_API int JErr_setTypeErr(JErr* o, JVType expT, JVType recT);
263BA_API int JErr_setError(JErr* o,JErrT err,const char* msg);
264#ifdef __cplusplus
265}
266inline JErr::JErr() {
267 JErr_constructor(this); }
268inline void JErr::reset() {
269 JErr_reset(this); }
270inline bool JErr::isError() {
271 return JErr_isError(this) ? true : false;}
272inline bool JErr::noError() {
273 return JErr_noError(this) ? true : false;}
275 return JErr_getErrT(this);}
277 return JErr_getExpT(this);}
279 return JErr_getRecT(this);}
281 return JErr_setTooFewParams(this);}
282inline int JErr::setTypeErr(JVType expT, JVType recT) {
283 return JErr_setTypeErr(this, expT, recT);}
284inline int JErr::setError(JErrT e,const char* message) {
285 return JErr_setError(this,e, message);}
286#endif
287
288
289
290#ifndef __DOXYGEN__
291typedef struct
292{
293 AllocatorIntf* alloc;
294 U8* buf;
295 U32 index;
296 size_t size;
297} JDBuf;
298
299
300/* JLextT: JSON Lexer Types.
301 The following types are used by the lexer and parser.
302*/
303typedef enum {
304 JLexerT_Null,
305 JLexerT_Boolean,
306 JLexerT_Number,
307 JLexerT_String,
308 JLexerT_BeginObject,
309 JLexerT_BeginArray,
310 JLexerT_EndObject,
311 JLexerT_EndArray,
312 JLexerT_Comma, /* ',' Array or object list comma. */
313 JLexerT_MemberSep, /* ':' for string : value */
314 JLexerT_NeedMoreData, /* Lexer not completed with current token. */
315 JLexerT_ParseErr,
316 JLexerT_MemErr
317} JLexerT;
318
319
320
321typedef enum {
322 JLexerSt_StartComment,
323 JLexerSt_EatComment,
324 JLexerSt_EndComment,
325 JLexerSt_EatCppComment,
326 JLexerSt_TrueFalseNull,
327 JLexerSt_String,
328 JLexerSt_StringEscape,
329 JLexerSt_StringUnicode,
330 JLexerSt_StringSurrogateEscape,
331 JLexerSt_StringSurrogateU,
332 JLexerSt_StringUtf8,
333 JLexerSt_Number,
334 JLexerSt_GetNextToken
335} JLexerSt;
336
337
338typedef struct
339{
340 JDBuf* asmB;
341 const U8* bufStart;
342 const U8* bufEnd;
343 const U8* tokenPtr;
344
345 U32 unicode;
346 S16 unicodeShift;
347 U16 surrogate;
348 U8 utf8Len;
349
350 /* typeChkPtr and retVal is used if the Lexer finds the start of
351 true, false, or null.
352 */
353 const U8* typeChkPtr;
354 U8 retVal;
355
356 U8 state; /* JLexerSt */
357
358 /* state for string or number.
359 If in state number, 0 means positive number and 255 means neg.
360 If in state String, sn is ' or ".
361 If in state Boolean: true or false.
362 */
363 U8 sn;
364 U8 isDouble; /* Used when reading a number */
365} JLexer;
366
367#endif /* __DOXYGEN__ */
368
371typedef enum {
382 JParserT_EndArray = ']'
384
388typedef struct JParserVal
389{
392 union
393 {
394 char* s;
395#ifndef NO_DOUBLE
396 double f;
397#endif
400 BaBool b;
401 } v;
402
418 size_t stringLen;
423
424
427typedef enum {
428
432
438
439
442
446
450
454
458
459
460typedef enum {
461 JParserSt_StartObj,
462 JParserSt_BeginArray,
463 JParserSt_MemberName,
464 JParserSt_MemberSep,
465 JParserSt_Value,
466 JParserSt_EndObj,
467 JParserSt_Comma
468} JParserSt;
469
470
471#define JPARSER_STACK_LEN 8
472
475
482{
483#ifdef __cplusplus
502 JParser(JParserIntf* intf, char* nameBuf, int namebufSize,
503 AllocatorIntf* alloc, int extraStackLen=0);
504
521 int parse(const U8* buf, U32 size);
522
525 ~JParser();
526
533#endif
534 JLexer lexer;
535 JParserVal val;
536 JDBuf asmB; /* Assembling various values */
537 JDBuf mnameB; /* Assembling object member names */
538 JParserIntf* intf;
539 S16 stackIx;
540 S16 stackSize;
541 U8 status; /* JParsStat */
542 U8 state; /* JParserSt */
543 /* It's possible to extend the stack size by reserving
544 * N*JParserStackNode bytes immediately following the memory for
545 * this struct instance. N is then used as 'extraStackLen' in constructor.
546 */
547 JParserStackNode stack[JPARSER_STACK_LEN];
548};
549
550typedef struct JParser JParser;
551
552#ifdef __cplusplus
553extern "C" {
554#endif
558BA_API void JParser_constructor(JParser* o, JParserIntf* intf, char* nameBuf,
559 int namebufSize, AllocatorIntf* alloc,
560 int extraStackLen);
564BA_API int JParser_parse(JParser* o, const U8* buf, U32 size);
572#define JParser_getStatus(o) ((JParsStat)(o)->status)
573#ifdef __cplusplus
574}
575inline JParser::JParser(JParserIntf* intf, char* nameBuf,
576 int namebufSize, AllocatorIntf* alloc,
577 int extraStackLen) {
578 JParser_constructor(this, intf, nameBuf, namebufSize, alloc, extraStackLen);
579}
580inline int JParser::parse(const U8* buf, U32 size) {
581 return JParser_parse(this, buf, size);}
583 JParser_destructor(this);}
585 return JParser_getStatus(this);}
586#endif
587 /* end of JSONRef */
589
590#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:109
int(* JParserIntf_Service)(struct JParserIntf *o, struct JParserVal *v, int recLevel)
Receive one parsed value or container boundary synchronously.
Definition: JParser.h:64
struct JParserIntf JParserIntf
The JParserIntf interface class is the interface between the parser and an object that implements the...
#define JParserIntf_serviceCB(o, v, recLev)
Invoke the callback synchronously.
Definition: JParser.h:105
BA_API int JErr_setTooFewParams(JErr *o)
Record JErrT_InvalidMethodParams with a static message if no error exists.
BA_API void JParser_constructor(JParser *o, JParserIntf *intf, char *nameBuf, int namebufSize, AllocatorIntf *alloc, int extraStackLen)
Create a JSON parser object.
BA_API int JErr_setError(JErr *o, JErrT err, const char *msg)
Record an error only when no earlier error exists.
struct JParserVal JParserVal
The parser sets a JParserVal before calling the parser callback JParserIntf.
struct JErr JErr
The JSON error container object.
BA_API void JParser_destructor(JParser *o)
Free internal assembly storage.
JParser(JParserIntf *intf, char *nameBuf, int namebufSize, AllocatorIntf *alloc, int extraStackLen=0)
Create a JSON parser object.
Definition: JParser.h:575
JParsStat getStatus()
Inspect the last parser result without changing state.
Definition: JParser.h:584
JParsStat
JSON Parser Status.
Definition: JParser.h:427
#define JErr_getExpT(o)
Definition: JParser.h:247
JVType getRecT()
Definition: JParser.h:278
#define JErr_isError(o)
Definition: JParser.h:235
JErrT
JSON error codes.
Definition: JParser.h:150
~JParser()
Free internal assembly storage.
Definition: JParser.h:582
#define JErr_getRecT(o)
Definition: JParser.h:251
void reset()
Clear the error code and message pointer so the object can be reused.
Definition: JParser.h:268
bool noError()
Definition: JParser.h:272
JErrT getErrT()
Definition: JParser.h:274
int parse(const U8 *buf, U32 size)
Feed or resume parsing a top-level JSON object or array.
Definition: JParser.h:580
int setTypeErr(JVType expT, JVType recT)
Record JErrT_WrongType only when no earlier error exists.
Definition: JParser.h:282
JVType
The JSON types.
Definition: JParser.h:121
#define JErr_getErrT(o)
Definition: JParser.h:243
int setError(JErrT err, const char *msg)
Record an error only when no earlier error exists.
Definition: JParser.h:284
BA_API int JErr_setTypeErr(JErr *o, JVType expT, JVType recT)
Record JErrT_WrongType only when no earlier error exists.
#define JErr_reset(o)
Clear the error code and message pointer so the object can be reused.
Definition: JParser.h:231
JParserT
Type 't' in JParserVal.
Definition: JParser.h:371
#define JParser_getStatus(o)
Inspect the last parser result without changing state.
Definition: JParser.h:572
JVType getExpT()
Definition: JParser.h:276
U8 JParserStackNode
The stack used internally by JParser.
Definition: JParser.h:474
JErr()
Initialize only the error code to JErrT_NoErr.
Definition: JParser.h:266
bool isError()
Definition: JParser.h:270
#define JErr_constructor(o)
Initialize only the error code to JErrT_NoErr.
Definition: JParser.h:227
int setTooFewParams()
Record JErrT_InvalidMethodParams with a static message if no error exists.
Definition: JParser.h:280
BA_API int JParser_parse(JParser *o, const U8 *buf, U32 size)
Feed or resume parsing a top-level JSON object or array.
#define JErr_noError(o)
Definition: JParser.h:239
@ JParsStat_ParseErr
JSON Parse error.
Definition: JParser.h:445
@ JParsStat_IntfErr
The JParserIntf interface reported a problem.
Definition: JParser.h:449
@ JParsStat_DoneEOS
The parser completed parsing a new JSON object.
Definition: JParser.h:431
@ JParsStat_NeedMoreData
The parser needs more data to complete.
Definition: JParser.h:441
@ JParsStat_StackOverflow
Object nested too deep.
Definition: JParser.h:456
@ JParsStat_Done
The parser completed parsing a new JSON object, but found the unread bytes in the provided buffer.
Definition: JParser.h:437
@ JParsStat_MemErr
Memory allocation error.
Definition: JParser.h:453
@ JErrT_InvalidMethodParams
Invalid RPC method parameters.
Definition: JParser.h:160
@ JErrT_NoErr
No error.
Definition: JParser.h:152
@ JErrT_MemErr
Memory allocation error.
Definition: JParser.h:165
@ JErrT_FmtValErr
A format error in JEncoder or JVal::get.
Definition: JParser.h:163
@ JErrT_WrongType
Unexpected JSON type when parsing.
Definition: JParser.h:158
@ JErrT_JsonErr
Generic error.
Definition: JParser.h:154
@ JErrT_IOErr
I/O error.
Definition: JParser.h:167
@ JVType_Null
JSON NULL value.
Definition: JParser.h:140
@ JVType_Int
JSON Number encoded as an integer.
Definition: JParser.h:133
@ JVType_Double
JSON Number encoded as a float.
Definition: JParser.h:130
@ JVType_Object
The JVAL is of type JSON object.
Definition: JParser.h:142
@ JVType_String
JSON string.
Definition: JParser.h:127
@ JVType_Long
JSON Number encoded as a long integer.
Definition: JParser.h:136
@ JVType_Array
The JVAL is of type JSON array.
Definition: JParser.h:144
@ JVType_Boolean
JSON boolean.
Definition: JParser.h:138
@ JVType_InvalidType
Invalid JSON type.
Definition: JParser.h:124
@ JParserT_BeginObject
BeginObject.
Definition: JParser.h:379
@ JParserT_Long
64-bit (long long)
Definition: JParser.h:377
@ JParserT_String
String.
Definition: JParser.h:374
@ JParserT_EndArray
EndArray.
Definition: JParser.h:382
@ JParserT_EndObject
EndObject.
Definition: JParser.h:381
@ JParserT_Boolean
Boolean.
Definition: JParser.h:378
@ JParserT_InvalidType
InvalidType.
Definition: JParser.h:372
@ JParserT_BeginArray
BeginArray.
Definition: JParser.h:380
@ JParserT_Null
Null.
Definition: JParser.h:373
@ JParserT_Double
Double.
Definition: JParser.h:375
@ JParserT_Int
Int.
Definition: JParser.h:376
uint16_t U16
Unsigned 16-bit integer.
Definition: GenPrimT.h:91
uint64_t U64
Unsigned 64-bit integer.
Definition: GenPrimT.h:95
int32_t S32
Signed 32-bit integer.
Definition: GenPrimT.h:85
uint32_t U32
Unsigned 32-bit integer.
Definition: GenPrimT.h:93
int16_t S16
Signed 16-bit integer.
Definition: GenPrimT.h:83
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:89
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:118
Memory allocation and deallocation Interface class.
Definition: AllocatorIntf.h:98
The JSON error container object.
Definition: JParser.h:173
const char * msg
Borrowed message for the recorded error; constructor alone does not initialize it.
Definition: JParser.h:216
The JParserIntf interface class is the interface between the parser and an object that implements the...
Definition: JParser.h:72
JParserIntf(JParserIntf_Service s)
Create the callback interface object.
Definition: JParser.h:77
The parser sets a JParserVal before calling the parser callback JParserIntf.
Definition: JParser.h:389
size_t memberNameLen
Object member-name length.
Definition: JParser.h:419
JParserT t
The type controlling 'v'.
Definition: JParser.h:421
union JParserVal::@3 v
A union controlled by the type 't'.
S32 d
If integer.
Definition: JParser.h:398
BaBool memberNameSet
TRUE when this event carries an object member name, including an empty name.
Definition: JParser.h:420
char * memberName
object member name is set for objects.
Definition: JParser.h:417
size_t stringLen
String length when t is JParserT_String.
Definition: JParser.h:418
U64 l
Long integer bit pattern; signed JSON long values are interpreted as S64 by consumers.
Definition: JParser.h:399
double f
If number of type double.
Definition: JParser.h:396
char * s
Borrowed UTF-8 bytes for String; use stringLen to preserve embedded NUL.
Definition: JParser.h:394
The JSON parser parses a JSON stream and calls the JParserIntf callback interface for each parsed obj...
Definition: JParser.h:482