Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
UBJSON Reference

Detailed Description

Universal Binary JSON Reference Manual.

See the UBJSON tutorial for an introduction to using the UBJSON API.

Collaboration diagram for UBJSON Reference:

Modules

 UBJSON Parser Callback
 UBJSON Parser Callback Interface.
 

Classes

struct  UBJVal
 UBJParser Value (passed into the parser callback interface UBJPIntf) More...
 
struct  UBJPStackNode
 The event based parser uses a stack instead of recursion and the UBJPStackNode type represent one entry in the stack. More...
 
struct  UBJPIntf
 The UBJPIntf interface class is the interface between the parser and an object that implements the UBJPIntf interface. More...
 
struct  UBJParser
 The UBJSON parser parses a binary UBJSON stream and calls the UBJPIntf callback interface for each parsed object/primitive type. More...
 
struct  UBJEBuf
 The UBJSON Encoder buffer is used by the encoder UBJEncoder. More...
 
struct  UBJEncoder
 UBJSON Encoder. More...
 
struct  UBJDecoderStackNode
 Used internally in UBJDecoder for nested data structures. More...
 
struct  UBJDecoderV
 Used internally in UBJDecoder, where one instance represents one node in the pointer value tree. More...
 
struct  UBJDecoder
 UBJDecoder implements the parser callback API UBJPIntf and builds a pointer value tree when UBJDecoder::get is called. More...
 

Macros

#define UBJParser_destructor(o)
 No-op destructor. More...
 
#define UBJParser_getIndex(o)   (o)->stack[(o)->stackIx].ix
 Query the current optimized-container position. More...
 
#define UBJParser_getCount(o)   (o)->stack[(o)->stackIx].count
 Query the current container's declared count. More...
 
#define UBJParser_getStatus(o)   (o)->status
 
#define UBJE_MEMBER(o, m)   #m, (o)->m
 UBJEncoder::set helper macro, used when setting a value for an object. More...
 
#define UBJEncoder_constructor(o, ubjsBuf)    memset(o,0,sizeof(UBJEncoder)),(o)->buf=ubjsBuf
 Initialize the encoder; no allocation, flush, or return value. More...
 
#define UBJEncoder_destructor(o)
 No-op; does not flush or free buffers. More...
 
#define UBJEncoder_reset(o)   ((o)->status=0,(o)->val.name=0,o->buf->cursor=0,0)
 Discard buffered bytes and clear status/name. More...
 
#define UBJEncoder_setName(o, v)   ((o)->val.name=(char*)v)
 Set the pending member name. More...
 
#define UBJEncoder_null(o)   ((o)->val.t=UBJT_Null,UBJEncoder_val(o))
 Encode null. More...
 
#define UBJEncoder_boolean(o, v)    ((o)->val.t=UBJT_Boolean,(o)->val.u.uint8=v,UBJEncoder_val(o))
 Encode a boolean. More...
 
#define UBJEncoder_uint8(o, v)    ((o)->val.t=UBJT_Uint8,(o)->val.u.uint8=v,UBJEncoder_val(o))
 Encode a U8 value. More...
 
#define UBJEncoder_int8(o, v)    ((o)->val.t=UBJT_Int8,(o)->val.u.int8=v,UBJEncoder_val(o))
 Encode a S8 value. More...
 
#define UBJEncoder_character(o, v)    ((o)->val.t=UBJT_Char,(o)->val.u.int8=v,UBJEncoder_val(o))
 Encode a char value. More...
 
#define UBJEncoder_int16(o, v)    ((o)->val.t=UBJT_Int16,(o)->val.u.int16=v,UBJEncoder_val(o))
 Encode a S16 value. More...
 
#define UBJEncoder_int32(o, v)    ((o)->val.t=UBJT_Int32,(o)->val.u.int32=v,UBJEncoder_val(o))
 Encode a S32 value. More...
 
#define UBJEncoder_int64(o, v)    ((o)->val.t=UBJT_Int64,(o)->val.u.int64=v,UBJEncoder_val(o))
 Encode a S64 value. More...
 
#define UBJEncoder_float64(o, v)    ((o)->val.t=UBJT_Float64,(o)->val.u.float64=v,UBJEncoder_val(o))
 Encode a double value. More...
 
#define UBJEncoder_float32(o, v)    ((o)->val.t=UBJT_Float32,(o)->val.u.float32=v,UBJEncoder_val(o))
 Encode a float value. More...
 
#define UBJEncoder_string(o, v, l)    ((o)->val.t=UBJT_String,(o)->val.u.string=v,(o)->val.len=l,UBJEncoder_val(o))
 Encode string bytes without requiring NUL termination. More...
 
#define UBJEncoder_beginArray(o, count, sType)
 Begin an array; emit exactly count children when count is nonnegative. More...
 
#define UBJEncoder_beginObject(o, count, sType)
 Begin an object; emit exactly count named members when count is nonnegative. More...
 
#define UBJEncoder_endArray(o)   ((o)->val.t=UBJT_EndArray,UBJEncoder_val(o))
 End the current array. More...
 
#define UBJEncoder_endObject(o)   ((o)->val.t=UBJT_EndObject,UBJEncoder_val(o))
 End the current object. More...
 
#define UBJD_MNUM(o, m)   #m, &(o)->m
 UBJDecoder::get helper macro, used when setting a number pointer in an object. More...
 
#define UBJD_MSTR(o, m)   #m, &(o)->m, sizeof((o)->m)
 UBJDecoder::get helper macro, used when setting a string pointer in an object. More...
 
#define UBJD_ASTR(o, m)   &(o)->m, sizeof((o)->m)
 UBJDecoder::get helper macro, used when setting a string pointer in an array. More...
 

Typedefs

typedef int(* UBJPIntf_Service) (struct UBJPIntf *o, UBJVal *v, int recLevel)
 Receive a parsed UBJSON event synchronously. More...
 
typedef struct UBJPIntf UBJPIntf
 The UBJPIntf interface class is the interface between the parser and an object that implements the UBJPIntf interface. More...
 
typedef struct UBJParser UBJParser
 The UBJSON parser parses a binary UBJSON stream and calls the UBJPIntf callback interface for each parsed object/primitive type. More...
 
typedef int(* UBJEBuf_FlushCB) (struct UBJEBuf *o, int sizeRequired)
 Make room in an encoder buffer by flushing or expanding it. More...
 
typedef struct UBJEBuf UBJEBuf
 The UBJSON Encoder buffer is used by the encoder UBJEncoder. More...
 
typedef struct UBJEncoder UBJEncoder
 UBJSON Encoder. More...
 
typedef UBJDecoder UBJDecoder
 UBJDecoder implements the parser callback API UBJPIntf and builds a pointer value tree when UBJDecoder::get is called. More...
 

Enumerations

enum  UBJT {
  UBJT_InvalidType =0 , UBJT_Null ='Z' , UBJT_NoOp ='N' , UBJT_Boolean ,
  UBJT_Int8 ='i' , UBJT_Uint8 ='U' , UBJT_Int16 ='I' , UBJT_Int32 ='l' ,
  UBJT_Int64 ='L' , UBJT_Float32 ='d' , UBJT_Float64 ='D' , UBJT_HNumber ='H' ,
  UBJT_Char ='C' , UBJT_String ='S' , UBJT_BeginObject ='{' , UBJT_BeginArray ='[' ,
  UBJT_Count ='#' , UBJT_EndObject ='}' , UBJT_EndArray =']'
}
 UBJSON Value Type: See UBJVal::t and the UBJSON type reference for more information. More...
 
enum  UBJPStatus {
  UBJPStatus_DoneEOS =1 , UBJPStatus_Done , UBJPStatus_NeedMoreData = 100 , UBJPStatus_ParseErr = 200 ,
  UBJPStatus_IntfErr , UBJPStatus_Overflow
}
 UBJSON Parser Status. More...
 
enum  UBJEStatus {
  UBJEStatus_FlushErr = -100 , UBJEStatus_LengthRequired , UBJEStatus_StackOverflow , UBJEStatus_TypeMismatch ,
  UBJEStatus_Unbalanced , UBJEStatus_Unknown , UBJEStatus_EmptyName , UBJEStatus_ok =0
}
 UBJSON Encoder status. More...
 
enum  UBJDecoderS {
  UBJDecoderS_FormatErr = -100 , UBJDecoderS_NameNotFound , UBJDecoderS_Overflow , UBJDecoderS_StringOverflow ,
  UBJDecoderS_Unbalanced , UBJDecoderS_Underflow , UBJDecoderS_Unknown , UBJDecoderS_BufNotAligned ,
  UBJDecoderS_ChainedErr , UBJDecoderS_OK = 0
}
 UBJDecoder Status Codes. More...
 

Functions

void UBJVal_setMinInteger (UBJVal *o, S64 in)
 Choose the smallest supported integer representation. More...
 
void UBJParser_constructor (UBJParser *o, UBJPIntf *intf, char *name, int memberNameLen, int extraStackLen)
 Create the callback interface object. More...
 
int UBJParser_parse (UBJParser *o, const U8 *buf, U32 size)
 Feed or resume parsing a top-level UBJSON object/array. More...
 
int UBJEncoder_vset (UBJEncoder *o, const char **fmt, va_list *argList, int isObj)
 Emit formatted values as for set(), advancing the format cursor. More...
 
int UBJEncoder_set (UBJEncoder *o, const char *fmt,...)
 Encode/serialize C structs/data to UBJSON using formatted output. More...
 
int UBJDecoder_vget (UBJDecoder *o, const char *fmt, va_list *argList)
 Build the schema using a va_list; see UBJDecoder::get for format rules. More...
 
int UBJDecoder_get (UBJDecoder *o, const char *fmt,...)
 Build a pointer value tree that is used by the integrated parser callback function when the parser feeds elements to the UBJDecoder instance. More...
 
void UBJDecoder_constructor (UBJDecoder *o, U8 *buf, int bufSize, int extraStackLen)
 Create/initialize a UBJDecoder instance. More...
 
 UBJPIntf::UBJPIntf (UBJPIntf_Service s)
 Create the callback interface object. More...
 
 UBJParser::UBJParser (UBJPIntf *intf, char *name, int memberNameLen, int extraStackLen=0)
 Create the callback interface object. More...
 
 UBJParser::~UBJParser ()
 No-op destructor. More...
 
int UBJParser::parse (const U8 *buf, U32 size)
 Feed or resume parsing a top-level UBJSON object/array. More...
 
int UBJParser::getIndex ()
 Query the current optimized-container position. More...
 
int UBJParser::getCount ()
 Query the current container's declared count. More...
 
int UBJParser::getStatus ()
 
 UBJEBuf::UBJEBuf (UBJEBuf_FlushCB cb, U8 *buf, S32 bufLen)
 Initialize the UBJSON Encoder buffer. More...
 
 UBJEBuf::~UBJEBuf ()
 No-op destructor; neither flushes nor frees application buffer storage. More...
 
 UBJEncoder::UBJEncoder (UBJEBuf *buf)
 Create/initialize an UBJEncoder instance. More...
 
 UBJEncoder::~UBJEncoder ()
 No-op destructor; does not flush output or free the borrowed buffer. More...
 
int UBJEncoder::setName (const char *n)
 Store a name for the next object value; do not use for array elements. More...
 
int UBJEncoder::null ()
 Emit UBJSON null. More...
 
int UBJEncoder::boolean (bool b)
 Emit one boolean value. More...
 
int UBJEncoder::uint8 (U8 v)
 Emit one uint8 value. More...
 
int UBJEncoder::int8 (S8 v)
 Emit one int8 value. More...
 
int UBJEncoder::character (char v)
 Emit one character value. More...
 
int UBJEncoder::int16 (S16 v)
 Emit one int16 value. More...
 
int UBJEncoder::int32 (S32 v)
 Emit one int32 value. More...
 
int UBJEncoder::int64 (S64 v)
 Emit one int64 value. More...
 
int UBJEncoder::float64 (double v)
 Emit one float64 value. More...
 
int UBJEncoder::float32 (float v)
 Emit one float32 value. More...
 
int UBJEncoder::string (const char *s, S32 len)
 Emit a length-delimited string. More...
 
int UBJEncoder::beginArray (S32 count=-1, UBJT type=UBJT_InvalidType)
 Begin formatting an array. More...
 
int UBJEncoder::beginObject (S32 count=-1, UBJT type=UBJT_InvalidType)
 Begin formatting an object. More...
 
int UBJEncoder::endArray ()
 Close the matching current container. More...
 
int UBJEncoder::endObject ()
 Close the matching current container. More...
 
void UBJEncoder::reset ()
 Discard buffered bytes and clear status and the pending member name. More...
 
int UBJEncoder::vset (const char **fmt, va_list *argList, bool isObj)
 Emit formatted values as for set(), advancing the format cursor. More...
 
int UBJEncoder::set (const char *fmt,...)
 Encode/serialize C structs/data to UBJSON using formatted output. More...
 
int UBJDecoder::vget (const char *fmt, va_list *argList)
 See UBJDecoder::get for details. More...
 
int UBJDecoder::get (const char *fmt,...)
 Build a pointer value tree that is used by the integrated parser callback function when the parser feeds elements to the UBJDecoder instance. More...
 
 UBJDecoder::UBJDecoder (U8 *buf, int bufSize, int extraStackLen=0)
 Create/initialize a UBJDecoder instance. More...
 

Macro Definition Documentation

◆ UBJD_ASTR

#define UBJD_ASTR (   o,
  m 
)    &(o)->m, sizeof((o)->m)

UBJDecoder::get helper macro, used when setting a string pointer in an array.

decoder.get("[S]", UBJD_ASTR(structval, membername));

◆ UBJD_MNUM

#define UBJD_MNUM (   o,
  m 
)    #m, &(o)->m

UBJDecoder::get helper macro, used when setting a number pointer in an object.

decoder.get("{l}", UBJD_MNUM(structval, membername));

◆ UBJD_MSTR

#define UBJD_MSTR (   o,
  m 
)    #m, &(o)->m, sizeof((o)->m)

UBJDecoder::get helper macro, used when setting a string pointer in an object.

decoder.get("{S}", UBJD_MSTR(structval, membername));

◆ UBJE_MEMBER

#define UBJE_MEMBER (   o,
  m 
)    #m, (o)->m

UBJEncoder::set helper macro, used when setting a value for an object.

encoder.set("{d}", UBJE_MEMBER(structval, membername));

◆ UBJEncoder_beginArray

#define UBJEncoder_beginArray (   o,
  count,
  sType 
)
Value:
((o)->val.t=UBJT_BeginArray,(o)->val.len=count, \
(o)->val.x=sType,UBJEncoder_val(o))
@ UBJT_BeginArray
Type UBJT_BeginArray.
Definition: ubjson.h:83

Begin an array; emit exactly count children when count is nonnegative.

Parameters
oRequired initialized encoder.
countNumber of children, or -1 for an uncounted array.
sTypeStrong element type, or UBJT_InvalidType for mixed types.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_beginObject

#define UBJEncoder_beginObject (   o,
  count,
  sType 
)
Value:
((o)->val.t=UBJT_BeginObject,(o)->val.len=count, \
(o)->val.x=sType,UBJEncoder_val(o))
@ UBJT_BeginObject
Type UBJT_BeginObject.
Definition: ubjson.h:82

Begin an object; emit exactly count named members when count is nonnegative.

Parameters
oRequired initialized encoder.
countNumber of members, or -1 for an uncounted object.
sTypeStrong member-value type, or UBJT_InvalidType for mixed types.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_boolean

#define UBJEncoder_boolean (   o,
  v 
)     ((o)->val.t=UBJT_Boolean,(o)->val.u.uint8=v,UBJEncoder_val(o))

Encode a boolean.

Parameters
oRequired initialized encoder.
vBoolean value; zero false, nonzero true.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_character

#define UBJEncoder_character (   o,
  v 
)     ((o)->val.t=UBJT_Char,(o)->val.u.int8=v,UBJEncoder_val(o))

Encode a char value.

Parameters
oRequired initialized encoder.
vValue representable in char.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_constructor

#define UBJEncoder_constructor (   o,
  ubjsBuf 
)     memset(o,0,sizeof(UBJEncoder)),(o)->buf=ubjsBuf

Initialize the encoder; no allocation, flush, or return value.

Parameters
oRequired encoder storage.
ubjsBufRequired borrowed initialized UBJEBuf.

◆ UBJEncoder_destructor

#define UBJEncoder_destructor (   o)

No-op; does not flush or free buffers.

Parameters
oRequired initialized encoder.

◆ UBJEncoder_endArray

#define UBJEncoder_endArray (   o)    ((o)->val.t=UBJT_EndArray,UBJEncoder_val(o))

End the current array.

Parameters
oRequired initialized encoder.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_endObject

#define UBJEncoder_endObject (   o)    ((o)->val.t=UBJT_EndObject,UBJEncoder_val(o))

End the current object.

Parameters
oRequired initialized encoder.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_float32

#define UBJEncoder_float32 (   o,
  v 
)     ((o)->val.t=UBJT_Float32,(o)->val.u.float32=v,UBJEncoder_val(o))

Encode a float value.

Parameters
oRequired initialized encoder.
vValue representable in float.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_float64

#define UBJEncoder_float64 (   o,
  v 
)     ((o)->val.t=UBJT_Float64,(o)->val.u.float64=v,UBJEncoder_val(o))

Encode a double value.

Parameters
oRequired initialized encoder.
vValue representable in double.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_int16

#define UBJEncoder_int16 (   o,
  v 
)     ((o)->val.t=UBJT_Int16,(o)->val.u.int16=v,UBJEncoder_val(o))

Encode a S16 value.

Parameters
oRequired initialized encoder.
vValue representable in S16.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_int32

#define UBJEncoder_int32 (   o,
  v 
)     ((o)->val.t=UBJT_Int32,(o)->val.u.int32=v,UBJEncoder_val(o))

Encode a S32 value.

Parameters
oRequired initialized encoder.
vValue representable in S32.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_int64

#define UBJEncoder_int64 (   o,
  v 
)     ((o)->val.t=UBJT_Int64,(o)->val.u.int64=v,UBJEncoder_val(o))

Encode a S64 value.

Parameters
oRequired initialized encoder.
vValue representable in S64.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_int8

#define UBJEncoder_int8 (   o,
  v 
)     ((o)->val.t=UBJT_Int8,(o)->val.u.int8=v,UBJEncoder_val(o))

Encode a S8 value.

Parameters
oRequired initialized encoder.
vValue representable in S8.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_null

#define UBJEncoder_null (   o)    ((o)->val.t=UBJT_Null,UBJEncoder_val(o))

Encode null.

Parameters
oRequired initialized encoder.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_reset

#define UBJEncoder_reset (   o)    ((o)->status=0,(o)->val.name=0,o->buf->cursor=0,0)

Discard buffered bytes and clear status/name.

Nesting state is not reset. Returns 0.

Parameters
oRequired initialized encoder.

◆ UBJEncoder_setName

#define UBJEncoder_setName (   o,
  v 
)    ((o)->val.name=(char*)v)

Set the pending member name.

Returns the assigned char pointer (not a status).

Parameters
oRequired initialized encoder.
vBorrowed nonempty NUL-terminated member name, valid until the next value. Embedded NUL bytes are unsupported. An empty name is rejected by the next value operation with UBJEStatus_EmptyName.

◆ UBJEncoder_string

#define UBJEncoder_string (   o,
  v,
  l 
)     ((o)->val.t=UBJT_String,(o)->val.u.string=v,(o)->val.len=l,UBJEncoder_val(o))

Encode string bytes without requiring NUL termination.

Parameters
oRequired initialized encoder.
vRequired readable string bytes, borrowed for the call.
lNonnegative byte count, excluding any optional NUL.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJEncoder_uint8

#define UBJEncoder_uint8 (   o,
  v 
)     ((o)->val.t=UBJT_Uint8,(o)->val.u.uint8=v,UBJEncoder_val(o))

Encode a U8 value.

Parameters
oRequired initialized encoder.
vValue representable in U8.
Returns
0 on success, negative encoder/output status on failure. Partial output is possible. See the corresponding UBJEncoder method.

◆ UBJParser_destructor

#define UBJParser_destructor (   o)

No-op destructor.

Input, name storage, and callback interface are borrowed and never freed by the parser. Stop using the parser before releasing them.

Parameters
oRequired initialized parser.

◆ UBJParser_getCount

#define UBJParser_getCount (   o)    (o)->stack[(o)->stackIx].count

Query the current container's declared count.

Returns
Nonnegative count for an optimized container, otherwise -1. Meaningful only while the container is active, typically during a callback.
Parameters
oRequired initialized parser.

◆ UBJParser_getIndex

#define UBJParser_getIndex (   o)    (o)->stack[(o)->stackIx].ix

Query the current optimized-container position.

Returns
Zero-based current index when a count is active, otherwise -1. Intended for callbacks while the container is active; do not interpret stale stack state after completion/error as a current position.
Parameters
oRequired initialized parser.

◆ UBJParser_getStatus

#define UBJParser_getStatus (   o)    (o)->status

Returns
Current UBJPStatus as an int, initially DoneEOS. This query does not advance parsing or clear the status.
Parameters
oRequired initialized parser.

Typedef Documentation

◆ UBJDecoder

UBJDecoder implements the parser callback API UBJPIntf and builds a pointer value tree when UBJDecoder::get is called.

The pointer value tree is used by the parser callback when the parser feeds the callback with parsed data.

See the JSON and UBJSON tutorial for more information on using UBJDecoder.

◆ UBJEBuf

typedef struct UBJEBuf UBJEBuf

The UBJSON Encoder buffer is used by the encoder UBJEncoder.

◆ UBJEBuf_FlushCB

typedef int(* UBJEBuf_FlushCB) (struct UBJEBuf *o, int sizeRequired)

Make room in an encoder buffer by flushing or expanding it.

Parameters
oRequired borrowed buffer object. Preserve unflushed bytes when growing. After flushing, reset cursor to zero; when growing, update data and dlen.
sizeRequiredAdditional capacity requested in bytes.
Returns
Zero after providing usable space, nonzero on failure. A successful callback must leave writable capacity; the encoder does not retry indefinitely. Buffer ownership remains with the application, and prior output is not rolled back.

◆ UBJEncoder

typedef struct UBJEncoder UBJEncoder

UBJSON Encoder.

The encoder performs limited error checking and you can produce incorrect UBJSON data if used incorrectly. Supply matching containers and exact optimized counts. Output is incremental, and an error can leave partial data. Final buffered bytes must be consumed/flushed by the application; the destructor does not flush. The current implementation has not been verified against the complete UBJSON specification. Supply UTF-8 strings and nonempty member names without embedded NUL bytes. UTF-8 is not validated. Empty member names return UBJEStatus_EmptyName. Native floating-point values must be finite; the caller must explicitly encode null for nonfinite values. H numeric text must follow JSON number syntax; the encoder does not validate that syntax.

◆ UBJParser

typedef struct UBJParser UBJParser

The UBJSON parser parses a binary UBJSON stream and calls the UBJPIntf callback interface for each parsed object/primitive type.

Note
This implementation may not match the latest UBJSON specification. Specification compatibility is reserved for a separate review. The contracts here describe the current implementation, not a conformance certification.

Only object/array roots are supported. String payloads must be UTF-8; the parser does not validate this encoding. High-precision H values are delivered as raw text for the application to interpret or reject.

See also
UBJVal
JParser

◆ UBJPIntf

typedef struct UBJPIntf UBJPIntf

The UBJPIntf interface class is the interface between the parser and an object that implements the UBJPIntf interface.

See also
UBJPIntf_Service

◆ UBJPIntf_Service

typedef int(* UBJPIntf_Service) (struct UBJPIntf *o, UBJVal *v, int recLevel)

Receive a parsed UBJSON event synchronously.

Parameters
oRequired callback interface.
vBorrowed value event; inspect t before its union. Strings/HNumber are delivered in chunks: len is this chunk's bytes and x is bytes remaining. Neither chunk data nor member-name storage may be retained without copying.
recLevelTop-level begin/end depth is zero; primitive children use one greater than their containing level. Count events use the container level.
Returns
Zero to continue, nonzero to stop with UBJPStatus_IntfErr. Do not destroy the parser during its callback.

Enumeration Type Documentation

◆ UBJDecoderS

UBJDecoder Status Codes.

Enumerator
UBJDecoderS_FormatErr 

Parsed data does not match the value tree that was created when method UBJDecoder::get was called.

UBJDecoderS_NameNotFound 

Parsed data includes a member whose name is not in value tree.

UBJDecoderS_Overflow 

Unconsumed schema values remain when a container ends; also used for excessive schema nesting.

UBJDecoderS_StringOverflow 

Parsed string longer than buffer provided.

UBJDecoderS_Unbalanced 

Incorrect use of '{', '}', '[', or ']' in UBJDecoder::get.

UBJDecoderS_Underflow 

Input supplies another value after all schema children were consumed.

UBJDecoderS_Unknown 

Unknown error.

UBJDecoderS_BufNotAligned 

Memory buffer(s) provided in UBJDecoder constructor must be word aligned.

UBJDecoderS_ChainedErr 

A chained parser callback, provided via the 'X' format flag in UBJDecoder::get, reported an error.

UBJDecoderS_OK 

OK, no errors.

◆ UBJEStatus

enum UBJEStatus

UBJSON Encoder status.

Enumerator
UBJEStatus_FlushErr 

UBJEBuf_FlushCB returned a non zero value.

UBJEStatus_LengthRequired 

Length required for Optimized Strongly Typed Container.

UBJEStatus_StackOverflow 

Object depth exceeding internal stack.

UBJEStatus_TypeMismatch 

The value set does not match the type set for Optimized Strongly Typed.

UBJEStatus_Unbalanced 

(curly) bracket start/end mismatch

UBJEStatus_Unknown 

Unknown type (Must be a type from UBJT)

UBJEStatus_EmptyName 

Empty object member names are unsupported.

UBJEStatus_ok 

No error.

◆ UBJPStatus

enum UBJPStatus

UBJSON Parser Status.

Enumerator
UBJPStatus_DoneEOS 

The parser completed parsing a new UBJSON object.

UBJPStatus_Done 

The parser completed parsing a new UBJSON object, but found the unread bytes in the supplied buffer, not yet validated as another container.

UBJPStatus_NeedMoreData 

The parser requires more data to complete.

UBJPStatus_ParseErr 

UBJSON Parse error.

UBJPStatus_IntfErr 

The UBJParserIntf interface reported a problem.

UBJPStatus_Overflow 

Parsed data/array/object too big.

◆ UBJT

enum UBJT

UBJSON Value Type: See UBJVal::t and the UBJSON type reference for more information.

Enumerator
UBJT_InvalidType 

Not a valid UBJSON type.

UBJT_Null 

Type UBJT_Null.

UBJT_NoOp 

Type UBJT_NoOp.

UBJT_Boolean 

Type UBJT_Boolean.

UBJT_Int8 

Type UBJT_Int8.

UBJT_Uint8 

Type UBJT_Uint8.

UBJT_Int16 

Type UBJT_Int16.

UBJT_Int32 

Type UBJT_Int32.

UBJT_Int64 

Type UBJT_Int64.

UBJT_Float32 

Type UBJT_Float32.

UBJT_Float64 

Type UBJT_Float64.

UBJT_HNumber 

Raw numeric text; the parser does not validate its grammar.

UBJT_Char 

Type UBJT_Char.

UBJT_String 

Type UBJT_String.

UBJT_BeginObject 

Type UBJT_BeginObject.

UBJT_BeginArray 

Type UBJT_BeginArray.

UBJT_Count 

Type UBJT_Count.

UBJT_EndObject 

Type UBJT_EndObject.

UBJT_EndArray 

Type UBJT_EndArray.

Function Documentation

◆ beginArray()

int UBJEncoder::beginArray ( S32  count = -1,
UBJT  type = UBJT_InvalidType 
)

Begin formatting an array.

Use the default values for the two parameters, or consult the UBJSON specification, section: Optimized Format.

Parameters
countmust be set to the number of elements in the array when formatting an optimized array with count or to -1 if not used.
typemust be set to the type of the optimized array if formatting a strongly typed array, or to UBJT_InvalidType if not used.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization. Count must equal the number of values/members emitted; the encoder does not count them for you. Keep nesting within 63 containers. General nested strongly typed combinations need separate compatibility verification.

◆ beginObject()

int UBJEncoder::beginObject ( S32  count = -1,
UBJT  type = UBJT_InvalidType 
)

Begin formatting an object.

Use the default values for the two parameters, or consult the UBJSON specification, section: Optimized Format.

Parameters
countmust be set to the number of elements in the object when formatting an optimized object with count or to -1 if not used.
typemust be set to the type of the optimized object if formatting a strongly typed object, or to UBJT_InvalidType if not used.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization. Count must equal the number of values/members emitted; the encoder does not count them for you. Keep nesting within 63 containers. General nested strongly typed combinations need separate compatibility verification.

◆ boolean()

int UBJEncoder::boolean ( bool  b)

Emit one boolean value.

Parameters
bValue copied into output.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ character()

int UBJEncoder::character ( char  v)

Emit one character value.

Parameters
vValue copied into output. Supply a protocol-valid single-byte character.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ endArray()

int UBJEncoder::endArray ( )

Close the matching current container.

A counted container omits its end marker from the wire, but this call is still required for encoder state.

Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ endObject()

int UBJEncoder::endObject ( )

Close the matching current container.

A counted container omits its end marker from the wire, but this call is still required for encoder state.

Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ float32()

int UBJEncoder::float32 ( float  v)

Emit one float32 value.

Parameters
vValue copied into output. Requires floating-point support.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ float64()

int UBJEncoder::float64 ( double  v)

Emit one float64 value.

Parameters
vValue copied into output. Requires floating-point support.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ get()

int UBJDecoder::get ( const char *  fmt,
  ... 
)

Build a pointer value tree that is used by the integrated parser callback function when the parser feeds elements to the UBJDecoder instance.

Parameters
fmtformat flags:
UBJSON typeFormat flagC type
boolbU8*
int8iS8*
uint8UU8*
int16IS16*
int32lS32*
int64LS64*
float32dfloat*
float64Ddouble*
charCS8*
stringSchar* buffer, size_t capacity

See the JSON and UBJSON tutorial for more information on using this method.

See also
UBJD_MNUM
UBJD_MSTR
UBJD_ASTR
UBJEncoder::set Use a complete top-level object/array format with matching braces/brackets. Each object value takes a borrowed nonempty member-name string before its destination. S takes a writable char buffer and size_t capacity; the current check requires complete string length + 2 bytes. Null mapped to S stores an empty C string. X delegates a container to a borrowed UBJPIntf callback. All names/destinations must remain valid through parsing. Rebuild with get() before decoding another document; the schema is consumed as values arrive. Outputs can be partially modified on failure. Numeric conversions are implementation-specific; use matching wire and destination types.
Returns
Zero when the schema is ready, -1 on setup failure. Check this return even when status is OK; schema buffer exhaustion need not set status. Successful setup does not mean any input has been decoded.

◆ getCount()

int UBJParser::getCount ( )

Query the current container's declared count.

Returns
Nonnegative count for an optimized container, otherwise -1. Meaningful only while the container is active, typically during a callback.

◆ getIndex()

int UBJParser::getIndex ( )

Query the current optimized-container position.

Returns
Zero-based current index when a count is active, otherwise -1. Intended for callbacks while the container is active; do not interpret stale stack state after completion/error as a current position.

◆ getStatus()

int UBJParser::getStatus ( )
Returns
Current UBJPStatus as an int, initially DoneEOS. This query does not advance parsing or clear the status.

◆ int16()

int UBJEncoder::int16 ( S16  v)

Emit one int16 value.

Parameters
vValue copied into output.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ int32()

int UBJEncoder::int32 ( S32  v)

Emit one int32 value.

Parameters
vValue copied into output.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ int64()

int UBJEncoder::int64 ( S64  v)

Emit one int64 value.

Parameters
vValue copied into output.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ int8()

int UBJEncoder::int8 ( S8  v)

Emit one int8 value.

Parameters
vValue copied into output.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ null()

int UBJEncoder::null ( )

Emit UBJSON null.

Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ parse()

int UBJParser::parse ( const U8 *  buf,
U32  size 
)

Feed or resume parsing a top-level UBJSON object/array.

Parameters
bufBorrowed binary input chunk. Supply new input after DoneEOS or NeedMoreData. After Done, this argument is ignored while the parser consumes unread bytes retained from the previous chunk; keep that storage valid.
sizeNew chunk byte count, ignored during continuation after Done.
Returns
0 needs more bytes, 1 completed one container, -1 reports a parse, callback, or capacity error. Inspect getStatus(). NeedMoreData at actual end of input means incomplete input. No whole-document allocation is made. After failure, reinitialize before reuse. Callback effects are not rolled back. A complete first container does not validate trailing bytes or another document.

◆ reset()

void UBJEncoder::reset ( )

Discard buffered bytes and clear status and the pending member name.

This does not flush output or reset nesting/strongly-typed state. Use only after a balanced complete value. Reinitialize to recover from a partial container.

◆ set()

int UBJEncoder::set ( const char *  fmt,
  ... 
)

Encode/serialize C structs/data to UBJSON using formatted output.

The set method works in a similar fashion to the ANSI C function printf; thus, it performs formatted output conversion.

The method internally uses the 'setXXX' methods in this class for setting the actual value(s), where XXX is uint8, float32, etc.. The following table shows the relationship between the format flags and the UBJSON values:

Parameters
fmtformat flags:
UBJSON typeFormat flagC type
boolbU8
int8iS8
uint8UU8
int16IS16
int32lS32
int64LS64
float32dfloat
float64Ddouble
charCS8
stringSchar*
See also
UBJE_MEMBER
UBJDecoder::get Format braces/brackets describe ordinary containers. Each object member takes a const char* name before its value. S takes a non-NULL NUL-terminated string. Arguments must follow C variadic promotions and the exact expected type; use primitive methods when avoiding variadic conversions.
Returns
Zero on success, a negative status on detected format/output errors.
Warning
The current d (float32) variadic path reads float instead of the C-promoted double. Use the primitive float32() method for this value type.

◆ setName()

int UBJEncoder::setName ( const char *  n)

Store a name for the next object value; do not use for array elements.

Parameters
nBorrowed NUL-terminated name retained until the next value is emitted.
Returns
The C macro yields the assigned pointer, not a status code.
Warning
The C++ wrapper declares int but returns that pointer expression; it requires a separate source correction. No output is produced by the setter.

◆ string()

int UBJEncoder::string ( const char *  s,
S32  len 
)

Emit a length-delimited string.

Parameters
sReadable UTF-8 bytes borrowed for this call, required for positive len.
lenNonnegative byte count, excluding a terminator. The encoder does not validate UTF-8. Embedded NUL bytes are included.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ UBJDecoder()

UBJDecoder::UBJDecoder ( U8 *  buf,
int  bufSize,
int  extraStackLen = 0 
)

Create/initialize a UBJDecoder instance.

Parameters
bufis a pointer to a buffer used internally for memory storage when building the pointer value tree. The minimum size must be greater than sizeof(UBJDecoderV) * N, where N is the number of format flags minus the end of array/object flags (] or }).
bufSizePositive byte capacity. buf must be aligned to sizeof(UBJ_ALIGNMT) and retained through parsing. Keep byte offsets representable in U16. This storage does not grow.
extraStackLenis an undocumented value and must be set to 0.

◆ UBJDecoder_constructor()

void UBJDecoder_constructor ( UBJDecoder *  o,
U8 *  buf,
int  bufSize,
int  extraStackLen 
)

Create/initialize a UBJDecoder instance.

Parameters
bufis a pointer to a buffer used internally for memory storage when building the pointer value tree. The minimum size must be greater than sizeof(UBJDecoderV) * N, where N is the number of format flags minus the end of array/object flags (] or }).
bufSizePositive byte capacity. buf must be aligned to sizeof(UBJ_ALIGNMT) and retained through parsing. Keep byte offsets representable in U16. This storage does not grow.
extraStackLenis an undocumented value and must be set to 0.
oRequired storage to initialize.

◆ UBJDecoder_get()

int UBJDecoder_get ( UBJDecoder *  o,
const char *  fmt,
  ... 
)

Build a pointer value tree that is used by the integrated parser callback function when the parser feeds elements to the UBJDecoder instance.

Parameters
fmtformat flags:
UBJSON typeFormat flagC type
boolbU8*
int8iS8*
uint8UU8*
int16IS16*
int32lS32*
int64LS64*
float32dfloat*
float64Ddouble*
charCS8*
stringSchar* buffer, size_t capacity

See the JSON and UBJSON tutorial for more information on using this method.

See also
UBJD_MNUM
UBJD_MSTR
UBJD_ASTR
UBJEncoder::set Use a complete top-level object/array format with matching braces/brackets. Each object value takes a borrowed nonempty member-name string before its destination. S takes a writable char buffer and size_t capacity; the current check requires complete string length + 2 bytes. Null mapped to S stores an empty C string. X delegates a container to a borrowed UBJPIntf callback. All names/destinations must remain valid through parsing. Rebuild with get() before decoding another document; the schema is consumed as values arrive. Outputs can be partially modified on failure. Numeric conversions are implementation-specific; use matching wire and destination types.
Returns
Zero when the schema is ready, -1 on setup failure. Check this return even when status is OK; schema buffer exhaustion need not set status. Successful setup does not mean any input has been decoded.
Parameters
oRequired initialized decoder.

◆ UBJDecoder_vget()

int UBJDecoder_vget ( UBJDecoder *  o,
const char *  fmt,
va_list *  argList 
)

Build the schema using a va_list; see UBJDecoder::get for format rules.

Parameters
oRequired initialized decoder.
fmtRequired NUL-terminated schema format.
argListRequired pointer to a va_list matching fmt; consumed by the call.
Returns
0 on success, -1 on schema construction failure.

◆ UBJEBuf()

UBJEBuf::UBJEBuf ( UBJEBuf_FlushCB  cb,
U8 *  buf,
S32  bufLen 
)

Initialize the UBJSON Encoder buffer.

Parameters
cbRequired expansion/flush callback; remains callable while used.
bufRequired borrowed writable encoder buffer, retained while used.
bufLenPositive buffer capacity in bytes, at least 14. No storage is allocated and the initial cursor is zero.

◆ UBJEncoder()

UBJEncoder::UBJEncoder ( UBJEBuf *  buf)

Create/initialize an UBJEncoder instance.

Parameters
bufa buffer that either buffers all produced UBJSON data or small chunks, which are then flushed out to a stream when the buffer is full. Required borrowed initialized UBJEBuf, which must outlive the encoder.

◆ UBJEncoder_set()

int UBJEncoder_set ( UBJEncoder *  o,
const char *  fmt,
  ... 
)

Encode/serialize C structs/data to UBJSON using formatted output.

The set method works in a similar fashion to the ANSI C function printf; thus, it performs formatted output conversion.

The method internally uses the 'setXXX' methods in this class for setting the actual value(s), where XXX is uint8, float32, etc.. The following table shows the relationship between the format flags and the UBJSON values:

Parameters
fmtformat flags:
UBJSON typeFormat flagC type
boolbU8
int8iS8
uint8UU8
int16IS16
int32lS32
int64LS64
float32dfloat
float64Ddouble
charCS8
stringSchar*
See also
UBJE_MEMBER
UBJDecoder::get Format braces/brackets describe ordinary containers. Each object member takes a const char* name before its value. S takes a non-NULL NUL-terminated string. Arguments must follow C variadic promotions and the exact expected type; use primitive methods when avoiding variadic conversions.
Returns
Zero on success, a negative status on detected format/output errors.
Warning
The current d (float32) variadic path reads float instead of the C-promoted double. Use the primitive float32() method for this value type.
Parameters
oRequired initialized encoder.

◆ UBJEncoder_vset()

int UBJEncoder_vset ( UBJEncoder *  o,
const char **  fmt,
va_list *  argList,
int  isObj 
)

Emit formatted values as for set(), advancing the format cursor.

Parameters
fmtRequired pointer to a NUL-terminated format cursor.
argListRequired pointer to a matching initialized va_list, consumed.
isObjTrue consumes a member-name argument before each value; false emits unnamed values. Use the matching container context.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.
Parameters
oRequired initialized encoder.

◆ UBJParser()

UBJParser::UBJParser ( UBJPIntf *  intf,
char *  name,
int  memberNameLen,
int  extraStackLen = 0 
)

Create the callback interface object.

Parameters
intfRequired borrowed interface with a non-NULL callback.
nameis a buffer used for storing an object member name during parsing. Required writable storage retained for the parser lifetime.
memberNameLenis the length of the object member name buffer. The length must be no less than the largest member name expected, plus one byte for its NUL terminator. Empty member names are rejected. Names containing NUL bytes are unsupported.
extraStackLeninforms the parser that it can use a stack larger than the default depth of 3. Use zero for an ordinary object. Positive values require the additional writable storage shown below and must fit the integer stack-length representation. The memory for the UBJParser object must be constructed as follows: malloc(sizeof(UBJParser) + extraStackLen * sizeof(UBJPStackNode))

◆ UBJParser_constructor()

void UBJParser_constructor ( UBJParser *  o,
UBJPIntf *  intf,
char *  name,
int  memberNameLen,
int  extraStackLen 
)

Create the callback interface object.

Parameters
intfRequired borrowed interface with a non-NULL callback.
nameis a buffer used for storing an object member name during parsing. Required writable storage retained for the parser lifetime.
memberNameLenis the length of the object member name buffer. The length must be no less than the largest member name expected, plus one byte for its NUL terminator. Empty member names are rejected. Names containing NUL bytes are unsupported.
extraStackLeninforms the parser that it can use a stack larger than the default depth of 3. Use zero for an ordinary object. Positive values require the additional writable storage shown below and must fit the integer stack-length representation. The memory for the UBJParser object must be constructed as follows: malloc(sizeof(UBJParser) + extraStackLen * sizeof(UBJPStackNode))
oRequired storage to initialize.

◆ UBJParser_parse()

int UBJParser_parse ( UBJParser *  o,
const U8 *  buf,
U32  size 
)

Feed or resume parsing a top-level UBJSON object/array.

Parameters
bufBorrowed binary input chunk. Supply new input after DoneEOS or NeedMoreData. After Done, this argument is ignored while the parser consumes unread bytes retained from the previous chunk; keep that storage valid.
sizeNew chunk byte count, ignored during continuation after Done.
Returns
0 needs more bytes, 1 completed one container, -1 reports a parse, callback, or capacity error. Inspect getStatus(). NeedMoreData at actual end of input means incomplete input. No whole-document allocation is made. After failure, reinitialize before reuse. Callback effects are not rolled back. A complete first container does not validate trailing bytes or another document.
Parameters
oRequired initialized parser.

◆ UBJPIntf()

UBJPIntf::UBJPIntf ( UBJPIntf_Service  s)

Create the callback interface object.

Parameters
sRequired callback, callable while installed.

◆ UBJVal_setMinInteger()

void UBJVal_setMinInteger ( UBJVal *  o,
S64  in 
)

Choose the smallest supported integer representation.

Parameters
oRequired output record; sets t and its selected union member only.
inSigned 64-bit input value. Other record fields remain unchanged.

◆ uint8()

int UBJEncoder::uint8 ( U8  v)

Emit one uint8 value.

Parameters
vValue copied into output.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ vget()

int UBJDecoder::vget ( const char *  fmt,
va_list *  argList 
)

See UBJDecoder::get for details.

◆ vset()

int UBJEncoder::vset ( const char **  fmt,
va_list *  argList,
bool  isObj 
)

Emit formatted values as for set(), advancing the format cursor.

Parameters
fmtRequired pointer to a NUL-terminated format cursor.
argListRequired pointer to a matching initialized va_list, consumed.
isObjTrue consumes a member-name argument before each value; false emits unnamed values. Use the matching container context.
Returns
Zero on success, negative UBJEStatus on a detected error. The error remains in status and blocks later value output until reset/reinitialization.

◆ ~UBJEBuf()

UBJEBuf::~UBJEBuf ( )

No-op destructor; neither flushes nor frees application buffer storage.

◆ ~UBJEncoder()

UBJEncoder::~UBJEncoder ( )

No-op destructor; does not flush output or free the borrowed buffer.

◆ ~UBJParser()

UBJParser::~UBJParser ( )

No-op destructor.

Input, name storage, and callback interface are borrowed and never freed by the parser. Stop using the parser before releasing them.