Barracuda Application Server C/C++ Reference
NO
ubjson.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Application Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: ubjson.h 5392 2023-02-21 15:56:50Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2014 - 2023
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 Universal Binary JSON http://ubjson.org/
37 Core API and parser
38
39*/
40
41#ifndef __ubjson_h
42#define __ubjson_h
43
44#include <TargConfig.h>
45#include <stdarg.h>
46
52#ifndef UBJ_ALIGNMT
53#define UBJ_ALIGNMT uintptr_t
54#endif
55#define UBJ_POINTER_NOT_ALIGNED(p) \
56 (0 != ((uintptr_t)p & (sizeof(UBJ_ALIGNMT) - 1)))
57
58
59#define UBJPARS_STACK_LEN 3
60
65typedef enum {
84 UBJT_EndArray=']'
86
87
90typedef enum {
91
95
100
103
107
111
116
118typedef struct {
120 union {
121 U8 uint8;
122 S8 int8;
123 char ch;
124 S16 int16;
125 S32 int32;
126 S64 int64;
127#ifndef NO_DOUBLE
128 double float64;
129 float float32;
130#endif
131 const char* string;
132 } u;
133
135 S32 len;
136
138 S32 x;
139
141 char* name;
142
144 U8 t;
145} UBJVal;
146
147
148#ifdef __cplusplus
149extern "C" {
150#endif
151
152/* Sets the value type (t) and the union (u) to the smallest number
153 representation for 'in'
154*/
155void UBJVal_setMinInteger(UBJVal* o, S64 in);
156
157#ifdef __cplusplus
158}
159#endif
160
161
165typedef struct
166{
167 S32 count; /* Set if optimized format */
168 S32 ix; /* Current index goes from 0 to count-1 */
169 U8 isObj; /* TRUE for object, FALSE for Array */
170 U8 stronglyTyped; /* Set to token type if a strongly typed container */
172
173
174struct UBJPIntf;
175
181typedef int (*UBJPIntf_Service)(struct UBJPIntf* o, UBJVal* v, int recLevel);
182
183
188typedef struct UBJPIntf
189{
190#ifdef __cplusplus
195 ~UBJPIntf();
196 UBJPIntf() {}
197#endif
198 UBJPIntf_Service service;
200
201#define UBJPIntf_constructor(o,serviceMA) (o)->service=serviceMA
202#define UBJPIntf_destructor(o)
203#define UBJPIntf_service(o,v,recLev) (o)->service(o,v,recLev)
204
205#ifdef __cplusplus
207 UBJPIntf_constructor(this, s);
208}
209inline UBJPIntf::~UBJPIntf() {
210 UBJPIntf_destructor(this);
211}
212#endif
213
220typedef struct UBJParser
221{
222#ifdef __cplusplus
236 UBJParser(UBJPIntf* intf,char* name,int memberNameLen,int extraStackLen=0);
237
248 int parse(const U8* buf, U32 size);
249
251 ~UBJParser();
252
256 int getIndex();
257
262 int getCount();
263
268 int getStatus();
269
270#endif
271 const U8* lxBufEnd;
272 const U8* lxTokenPtr;
273 U8* valPtr;
274 S32 lxBytes2Read;
275 U8 lxParseX; /* Parsing one of: S, H # */
276 U8 lxState; /* UBJLxState */
277 U8 status; /* UBJPStatus */
278 U8 pState; /* parser state */
279 UBJVal val;
280 UBJPIntf* intf;
281 int memberNameLen;
282 int stackIx; /* index in 'stack' */
283 int stackLen; /* min UBJPARS_STACK_LEN */
284 U8 stringFragment;
285 UBJPStackNode stack[UBJPARS_STACK_LEN];
287
288#ifdef __cplusplus
289extern "C" {
290#endif
291void UBJParser_constructor(UBJParser* o, UBJPIntf* intf, char* name,
292 int memberNameLen, int extraStackLen);
293#define UBJParser_destructor(o)
294int UBJParser_parse(UBJParser* o, const U8* buf, U32 size);
295#define UBJParser_getIndex(o) (o)->stack[(o)->stackIx].ix
296#define UBJParser_getCount(o) (o)->stack[(o)->stackIx].count
297#define UBJParser_getStatus(o) (o)->status
298#ifdef __cplusplus
299}
301 UBJPIntf* intf, char* name, int memberNameLen, int extraStackLen) {
302 UBJParser_constructor(this, intf, name, memberNameLen, extraStackLen);
303}
305 UBJParser_destructor(this);
306}
307inline int UBJParser::parse(const U8* buf, U32 size) {
308 return UBJParser_parse(this, buf, size);
309}
311 return UBJParser_getIndex(this);
312}
314 return UBJParser_getCount(this);
315}
317 return UBJParser_getStatus(this);
318}
319#endif
320
321struct UBJEBuf;
322
330typedef int (*UBJEBuf_FlushCB)(struct UBJEBuf* o, int sizeRequired);
331
333typedef struct UBJEBuf
334{
335#ifdef __cplusplus
341 UBJEBuf(UBJEBuf_FlushCB cb, U8* buf, S32 bufLen);
342 ~UBJEBuf();
343#endif
344 UBJEBuf_FlushCB flushCB;
345 U8* data;
346 S32 dlen;
347 S32 cursor;
349
350#define UBJEBuf_constructor(o, flushCBM, dataM, dlenM) \
351 (o)->flushCB=flushCBM,(o)->data=dataM,(o)->dlen=dlenM,(o)->cursor=0
352#define UBJEBuf_destructor(o)
353
354#ifdef __cplusplus
355inline UBJEBuf::UBJEBuf(UBJEBuf_FlushCB cb, U8* buf, S32 bufLen) {
356 UBJEBuf_constructor(this, cb,buf,bufLen);
357}
358inline UBJEBuf::~UBJEBuf() {
359 UBJEBuf_destructor(this);
360}
361#endif
362
364typedef enum
365{
368
371
374
377
380
383
387
388
394typedef struct UBJEncoder
395{
396#ifdef __cplusplus
397
403 UBJEncoder(UBJEBuf* buf);
404
407 ~UBJEncoder();
408
412 int setName(const char* n);
413
415 int null();
416
418 int boolean(bool b);
419
421 int uint8(U8 v);
422
424 int int8(S8 v);
425
427 int character(char v);
428
430 int int16(S16 v);
431
433 int int32(S32 v);
434
436 int int64(S64 v);
437
439 int float64(double v);
440
442 int float32(float v);
443
445 int string(const char* s, S32 len);
446
459 int beginArray(S32 count=-1, UBJT type=UBJT_InvalidType);
460
473 int beginObject(S32 count=-1, UBJT type=UBJT_InvalidType);
474
476 int endArray();
477
479 int endObject();
480
482 void reset();
483
485 int vset(const char** fmt, va_list* argList, bool isObj);
486
515 int set(const char* fmt, ...);
516#endif
517
518 UBJVal val;
519 struct /* Used as bit field and set if the object/array has a 'count' */
520 {
521 S32 level;
522 U8 data[8];
523 } countStack;
524 UBJEBuf* buf;
525 int status;
526 UBJT stronglyTyped;
528
529
533#define UBJE_MEMBER(o, m) #m, (o)->m
534
535#define UBJEncoder_constructor(o, ubjsBuf) \
536 memset(o,0,sizeof(UBJEncoder)),(o)->buf=ubjsBuf
537#define UBJEncoder_destructor(o)
538#define UBJEncoder_reset(o) ((o)->status=0,(o)->val.name=0,o->buf->cursor=0,0)
539
540#ifdef __cplusplus
541extern "C" {
542#endif
543int UBJEncoder_setStatus(UBJEncoder* o, UBJEStatus s);
544int UBJEncoder_val(UBJEncoder* o);
545int UBJEncoder_vset(UBJEncoder* o,const char** fmt,va_list* argList,int isObj);
546int UBJEncoder_set(UBJEncoder* o, const char* fmt, ...);
547#ifdef __cplusplus
548}
549#endif
550
551#define UBJEncoder_setName(o,v) ((o)->val.name=(char*)v)
552#define UBJEncoder_null(o) ((o)->val.t=UBJT_Null,UBJEncoder_val(o))
553#define UBJEncoder_boolean(o,v) \
554 ((o)->val.t=UBJT_Boolean,(o)->val.u.uint8=v,UBJEncoder_val(o))
555#define UBJEncoder_uint8(o,v) \
556 ((o)->val.t=UBJT_Uint8,(o)->val.u.uint8=v,UBJEncoder_val(o))
557#define UBJEncoder_int8(o,v) \
558 ((o)->val.t=UBJT_Int8,(o)->val.u.int8=v,UBJEncoder_val(o))
559#define UBJEncoder_character(o,v) \
560 ((o)->val.t=UBJT_Char,(o)->val.u.int8=v,UBJEncoder_val(o))
561#define UBJEncoder_int16(o,v) \
562 ((o)->val.t=UBJT_Int16,(o)->val.u.int16=v,UBJEncoder_val(o))
563#define UBJEncoder_int32(o,v) \
564 ((o)->val.t=UBJT_Int32,(o)->val.u.int32=v,UBJEncoder_val(o))
565#define UBJEncoder_int64(o,v) \
566 ((o)->val.t=UBJT_Int64,(o)->val.u.int64=v,UBJEncoder_val(o))
567#define UBJEncoder_float64(o,v) \
568 ((o)->val.t=UBJT_Float64,(o)->val.u.float64=v,UBJEncoder_val(o))
569#define UBJEncoder_float32(o,v) \
570 ((o)->val.t=UBJT_Float32,(o)->val.u.float32=v,UBJEncoder_val(o))
571#define UBJEncoder_string(o,v,l) \
572 ((o)->val.t=UBJT_String,(o)->val.u.string=v,(o)->val.len=l,UBJEncoder_val(o))
573#define UBJEncoder_beginArray(o,count, sType) \
574 ((o)->val.t=UBJT_BeginArray,(o)->val.len=count, \
575 (o)->val.x=sType,UBJEncoder_val(o))
576#define UBJEncoder_beginObject(o,count, sType) \
577 ((o)->val.t=UBJT_BeginObject,(o)->val.len=count, \
578 (o)->val.x=sType,UBJEncoder_val(o))
579#define UBJEncoder_endArray(o) ((o)->val.t=UBJT_EndArray,UBJEncoder_val(o))
580#define UBJEncoder_endObject(o) ((o)->val.t=UBJT_EndObject,UBJEncoder_val(o))
581
582#ifdef __cplusplus
584 UBJEncoder_constructor(this, b);
585}
587 UBJEncoder_destructor(this);
588}
589inline int UBJEncoder::setName(const char* n) {
590 return UBJEncoder_setName(this, n);
591}
592inline int UBJEncoder::null() {
593 return UBJEncoder_null(this);
594}
595inline int UBJEncoder::boolean(bool b) {
596 return UBJEncoder_boolean(this, b ? 1 : 0);
597}
598inline int UBJEncoder::uint8(U8 v) {
599 return UBJEncoder_uint8(this, v);
600}
601inline int UBJEncoder::int8(S8 v) {
602 return UBJEncoder_int8(this, v);
603}
604inline int UBJEncoder::character(char v) {
605 return UBJEncoder_character(this, v);
606}
607inline int UBJEncoder::int16(S16 v) {
608 return UBJEncoder_int16(this, v);
609}
610inline int UBJEncoder::int32(S32 v) {
611 return UBJEncoder_int32(this, v);
612}
613inline int UBJEncoder::int64(S64 v) {
614 return UBJEncoder_int64(this, v);
615}
616inline int UBJEncoder::float64(double v) {
617 return UBJEncoder_float64(this, v);
618}
619inline int UBJEncoder::float32(float v) {
620 return UBJEncoder_float32(this, v);
621}
622inline int UBJEncoder::string(const char* s, S32 len) {
623 return UBJEncoder_string(this, s, len);
624}
625inline int UBJEncoder::beginArray(S32 count, UBJT t) {
626 return UBJEncoder_beginArray(this, count, t);
627}
628inline int UBJEncoder::beginObject(S32 count, UBJT t) {
629 return UBJEncoder_beginObject(this, count, t);
630}
632 return UBJEncoder_endArray(this);
633}
635 return UBJEncoder_endObject(this);
636}
637inline void UBJEncoder::reset() {
638 UBJEncoder_reset(this);
639}
640inline int UBJEncoder::vset(const char** fmt, va_list* argList, bool isObj){
641 return UBJEncoder_vset(this, fmt, argList, isObj ? 1 : 0);
642}
643inline int UBJEncoder::set(const char* fmt, ...){
644 int status;
645 va_list varg;
646 va_start(varg, fmt);
647 status = UBJEncoder_vset(this, &fmt, &varg, 0);
648 va_end(varg);
649 return status;
650}
651#endif
652 /* end of UBJSONRef */
654
655
656#endif
struct UBJEncoder UBJEncoder
UBJSON Encoder.
int endObject()
End of object.
Definition: ubjson.h:634
struct UBJParser UBJParser
The UBJSON parser parses a binary UBJSON stream and calls the UBJPIntf callback interface for each pa...
int vset(const char **fmt, va_list *argList, bool isObj)
See UBJEncoder:set for details.
Definition: ubjson.h:640
int int32(S32 v)
Set int32.
Definition: ubjson.h:610
int uint8(U8 v)
Set uint8.
Definition: ubjson.h:598
struct UBJEBuf UBJEBuf
The UBJSON Encoder buffer is used by the encoder UBJEncoder.
struct UBJPIntf UBJPIntf
The UBJPIntf interface class is the interface between the parser and an object that implements the UB...
UBJT
UBJSON Value Type: See UBJVal::t and the UBJSON type reference for more information.
Definition: ubjson.h:65
int int8(S8 v)
Set int8.
Definition: ubjson.h:601
~UBJEncoder()
Destructor.
Definition: ubjson.h:586
int getIndex()
Returns the current container index position when parsing an optimized container.
Definition: ubjson.h:310
int character(char v)
Set 'char'.
Definition: ubjson.h:604
UBJEStatus
UBJSON Encoder status.
Definition: ubjson.h:365
UBJEncoder(UBJEBuf *buf)
Create/initialize an UBJEncoder instance.
Definition: ubjson.h:583
int setName(const char *n)
Set member name.
Definition: ubjson.h:589
UBJEBuf(UBJEBuf_FlushCB cb, U8 *buf, S32 bufLen)
Initialize the UBJSON Encoder buffer.
Definition: ubjson.h:355
int getCount()
Returns the length of the optimized container when type (t) is UBJT_Count.
Definition: ubjson.h:313
int parse(const U8 *buf, U32 size)
Parse a UBJSON chunk.
Definition: ubjson.h:307
int(* UBJEBuf_FlushCB)(struct UBJEBuf *o, int sizeRequired)
UBJSON Encoder buffer callback can be used for flushing the buffer or for expanding the buffer.
Definition: ubjson.h:330
int set(const char *fmt,...)
Encode/serialize C structs/data to UBJSON using formatted output.
Definition: ubjson.h:643
UBJPStatus
UBJSON Parser Status.
Definition: ubjson.h:90
int endArray()
End of array.
Definition: ubjson.h:631
UBJParser(UBJPIntf *intf, char *name, int memberNameLen, int extraStackLen=0)
Create the callback interface object.
Definition: ubjson.h:300
int string(const char *s, S32 len)
Set string.
Definition: ubjson.h:622
int(* UBJPIntf_Service)(struct UBJPIntf *o, UBJVal *v, int recLevel)
Parser callback function used by interface class UBJPIntf.
Definition: ubjson.h:181
int beginArray(S32 count=-1, UBJT type=UBJT_InvalidType)
Begin formatting an array.
Definition: ubjson.h:625
int float32(float v)
Set float32.
Definition: ubjson.h:619
UBJPIntf(UBJPIntf_Service s)
Create the callback interface object.
Definition: ubjson.h:206
int float64(double v)
Set float64.
Definition: ubjson.h:616
int getStatus()
Returns the parser status.
Definition: ubjson.h:316
~UBJParser()
Destructor.
Definition: ubjson.h:304
int int64(S64 v)
Set int64.
Definition: ubjson.h:613
void reset()
Resets the UBJEBuf cursor (the buffer provided in the constructor)
Definition: ubjson.h:637
int boolean(bool b)
Set boolean.
Definition: ubjson.h:595
int null()
Set UBJSON NULL.
Definition: ubjson.h:592
int int16(S16 v)
Set int16.
Definition: ubjson.h:607
int beginObject(S32 count=-1, UBJT type=UBJT_InvalidType)
Begin formatting an object.
Definition: ubjson.h:628
@ UBJT_Int8
Type UBJT_Int8.
Definition: ubjson.h:70
@ UBJT_BeginObject
Type UBJT_BeginObject.
Definition: ubjson.h:80
@ UBJT_EndArray
Type UBJT_EndArray.
Definition: ubjson.h:84
@ UBJT_Boolean
Type UBJT_Boolean.
Definition: ubjson.h:69
@ UBJT_HNumber
Type UBJT_HNumber.
Definition: ubjson.h:77
@ UBJT_BeginArray
Type UBJT_BeginArray.
Definition: ubjson.h:81
@ UBJT_Int64
Type UBJT_Int64.
Definition: ubjson.h:74
@ UBJT_NoOp
Type UBJT_NoOp.
Definition: ubjson.h:68
@ UBJT_Float64
Type UBJT_Float64.
Definition: ubjson.h:76
@ UBJT_Int32
Type UBJT_Int32.
Definition: ubjson.h:73
@ UBJT_String
Type UBJT_String.
Definition: ubjson.h:79
@ UBJT_Count
Type UBJT_Count.
Definition: ubjson.h:82
@ UBJT_Null
Type UBJT_Null.
Definition: ubjson.h:67
@ UBJT_Int16
Type UBJT_Int16.
Definition: ubjson.h:72
@ UBJT_InvalidType
Not a valid UBJSON type.
Definition: ubjson.h:66
@ UBJT_Char
Type UBJT_Char.
Definition: ubjson.h:78
@ UBJT_Float32
Type UBJT_Float32.
Definition: ubjson.h:75
@ UBJT_Uint8
Type UBJT_Uint8.
Definition: ubjson.h:71
@ UBJT_EndObject
Type UBJT_EndObject.
Definition: ubjson.h:83
@ UBJEStatus_ok
No error.
Definition: ubjson.h:385
@ UBJEStatus_TypeMismatch
The value set does not mach type set for Optimized Strongly Typed
Definition: ubjson.h:376
@ UBJEStatus_LengthRequired
Length required for Optimized Strongly Typed Container.
Definition: ubjson.h:370
@ UBJEStatus_StackOverflow
Object depth exceeding internal stack.
Definition: ubjson.h:373
@ UBJEStatus_Unknown
Unknown type (Must be a type from UBJT)
Definition: ubjson.h:382
@ UBJEStatus_FlushErr
UBJEBuf_FlushCB returned a non zero value.
Definition: ubjson.h:367
@ UBJEStatus_Unbalanced
(curly) bracket start/end mismatch
Definition: ubjson.h:379
@ UBJPStatus_Done
The parser completed parsing a new UBJSON object, but found the start of a new object in the provided...
Definition: ubjson.h:99
@ UBJPStatus_ParseErr
UBJSON Parse error.
Definition: ubjson.h:106
@ UBJPStatus_Overflow
Parsed data/array/object too big.
Definition: ubjson.h:114
@ UBJPStatus_DoneEOS
The parser completed parsing a new UBJSON object.
Definition: ubjson.h:94
@ UBJPStatus_IntfErr
The UBJParserIntf interface reported a problem.
Definition: ubjson.h:110
@ UBJPStatus_NeedMoreData
The parser requires more data to complete.
Definition: ubjson.h:102
The UBJSON Encoder buffer is used by the encoder UBJEncoder.
Definition: ubjson.h:334
UBJSON Encoder.
Definition: ubjson.h:395
The UBJPIntf interface class is the interface between the parser and an object that implements the UB...
Definition: ubjson.h:189
The event based parser uses a stack instead of recursion and the UBJPStackNode type represent one ent...
Definition: ubjson.h:166
The UBJSON parser parses a binary UBJSON stream and calls the UBJPIntf callback interface for each pa...
Definition: ubjson.h:221
UBJParser Value (passed into the parser callback interface UBJPIntf)
Definition: ubjson.h:118
U8 uint8
Use when 't' is UBJT_Uint8 or UBJT_Boolean.
Definition: ubjson.h:121
S32 int32
Use when 't' is UBJT_Int32.
Definition: ubjson.h:125
double float64
Use when 't' is UBJT_Float64.
Definition: ubjson.h:128
S8 int8
Use when 't' is UBJT_Uint8.
Definition: ubjson.h:122
S64 int64
Use when 't' is UBJT_Int64.
Definition: ubjson.h:126
float float32
Use when 't' is UBJT_Float32.
Definition: ubjson.h:129
const char * string
Use when 't' is UBJT_String.
Definition: ubjson.h:131
S16 int16
Use when 't' is UBJT_Int16.
Definition: ubjson.h:124
S32 len
When t=UBJT_Count: container len, When t=UBJT_String: string chunk len.
Definition: ubjson.h:135
char * name
Object member name: set when parent is an object.
Definition: ubjson.h:141
S32 x
When t=UBJT_Count: type UBJT.
Definition: ubjson.h:138
char ch
Use when 't' is UBJT_Char.
Definition: ubjson.h:123
U8 t
UBJSON Type, t is set to one of the types in UBJT.
Definition: ubjson.h:144