|
Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
|
JSON Reference Manual.
See the JSON tutorial for an introduction to using the JSON API.

Modules | |
| JSON Parser Callback | |
| JSON Parser Callback Interface. | |
Classes | |
| struct | JEncoder |
| The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format. More... | |
| struct | JErr |
| The JSON error container object. More... | |
| struct | JParserVal |
| The parser sets a JParserVal before calling the parser callback JParserIntf. More... | |
| struct | JParser |
| The JSON parser parses a JSON stream and calls the JParserIntf callback interface for each parsed object/primitive type. More... | |
| struct | JVal |
| JVal represents a value in a JSON tree. More... | |
| struct | JValFact |
| The JValFact enables one to manually create a JVal syntax tree or add nodes to an existing syntax tree. More... | |
Macros | |
| #define | JEncoder_destructor(o) JEncoder_flush(o) |
| Call flush() without freeing the borrowed writer or error object. More... | |
| #define | JEncoder_getErr(o) (o)->err |
| Query error storage. More... | |
| #define | JEncoder_getBufPrint(o) (o)->out |
| Query output storage. More... | |
| #define | JE_MEMBER(o, m) #m, (o)->m |
| JEncoder::set helper macro, used when setting a value for an object. More... | |
| #define | JErr_constructor(o) (o)->err=JErrT_NoErr |
| Initialize only the error code to JErrT_NoErr. More... | |
| #define | JErr_reset(o) (o)->err=JErrT_NoErr, (o)->msg=0 |
| Clear the error code and message pointer so the object can be reused. More... | |
| #define | JErr_isError(o) ((o)->err!=JErrT_NoErr) |
| #define | JErr_noError(o) ((o)->err==JErrT_NoErr) |
| #define | JErr_getErrT(o) (o)->err |
| #define | JErr_getExpT(o) (o)->expType |
| #define | JErr_getRecT(o) (o)->recType |
| #define | JParser_getStatus(o) ((JParsStat)(o)->status) |
| Inspect the last parser result without changing state. More... | |
| #define | JVal_getType(o) (o)->type |
| #define | JVal_getStringLen(o) (o)->stringLen |
| #define | JVal_getNameLen(o) (o)->memberNameLen |
| #define | JVal_getNextElem(o) (o)->next |
| #define | JVal_isObjectMember(o) ((o)->memberName != 0) |
| #define | JVal_setInt(o, e, v) JVal_setX(o, e, JVType_Int, &v) |
| Replace a scalar value and set its type. More... | |
| #define | JVal_setLong(o, e, v) JVal_setX(o, e, JVType_Long, &v) |
| Replace a scalar value and set its type. More... | |
| #define | JVal_setDouble(o, e, v) JVal_setX(o, e, JVType_Double, &v) |
| Replace a scalar value and set its type. More... | |
| #define | JVal_setBoolean(o, e, v) JVal_setX(o, e, JVType_Boolean, &v) |
| Replace a scalar value and set its type. More... | |
| #define | JVal_setNull(o, e) JVal_setX(o, e, JVType_Null, 0) |
| Replace a scalar value with JSON null. More... | |
| #define | JVal_setString(o, e, v) JVal_setX(o, e, JVType_String, v) |
| Store a string pointer without copying it and set the node's type. More... | |
| #define | JValFact_mkString(o, v) JValFact_mkVal(o, JVType_String, v) |
| Create a detached string node. More... | |
| #define | JValFact_mkDouble(o, v) JValFact_mkVal(o, JVType_Double, &v) |
| Create a detached double node. More... | |
| #define | JValFact_mkInt(o, v) JValFact_mkVal(o, JVType_Int, &v) |
| Create a detached int node. More... | |
| #define | JValFact_mkLong(o, v) JValFact_mkVal(o, JVType_Long, &v) |
| Create a detached long node. More... | |
| #define | JValFact_mkBoolean(o, v) JValFact_mkVal(o, JVType_Boolean, &v) |
| Create a detached boolean node. More... | |
| #define | JValFact_mkNull(o) JValFact_mkVal(o, JVType_Null, 0) |
| Create a detached null node. More... | |
| #define | JValFact_mkObject(o) JValFact_mkVal(o, JVType_Object, 0) |
| Create a detached object node. More... | |
| #define | JValFact_mkArray(o) JValFact_mkVal(o, JVType_Array, 0) |
| Create a detached array node. More... | |
Typedefs | |
| typedef struct JEncoder | JEncoder |
| The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format. More... | |
| typedef struct JErr | JErr |
| The JSON error container object. More... | |
| typedef struct JParserVal | JParserVal |
| The parser sets a JParserVal before calling the parser callback JParserIntf. More... | |
| typedef U8 | JParserStackNode |
| The stack used internally by JParser. More... | |
| typedef struct JValFact | JValFact |
| The JValFact enables one to manually create a JVal syntax tree or add nodes to an existing syntax tree. More... | |
Enumerations | |
| enum | JVType { JVType_InvalidType , JVType_String , JVType_Double , JVType_Int , JVType_Long , JVType_Boolean , JVType_Null , JVType_Object , JVType_Array } |
| The JSON types. More... | |
| enum | JErrT { JErrT_NoErr =0 , JErrT_JsonErr , JErrT_WrongType , JErrT_InvalidMethodParams , JErrT_FmtValErr , JErrT_MemErr , JErrT_IOErr } |
| JSON error codes. More... | |
| enum | JParserT { JParserT_InvalidType =0 , JParserT_Null , JParserT_String = 's' , JParserT_Double = 'f' , JParserT_Int = 'd' , JParserT_Long = 'l' , JParserT_Boolean = 'b' , JParserT_BeginObject = '{' , JParserT_BeginArray = '[' , JParserT_EndObject = '}' , JParserT_EndArray = ']' } |
| Type 't' in JParserVal. More... | |
| enum | JParsStat { JParsStat_DoneEOS =1 , JParsStat_Done , JParsStat_NeedMoreData = 100 , JParsStat_ParseErr = 200 , JParsStat_IntfErr , JParsStat_MemErr , JParsStat_StackOverflow } |
| JSON Parser Status. More... | |
Functions | |
| BA_API void | JEncoder_constructor (JEncoder *o, JErr *err, BufPrint *out) |
| Initialize an encoder without writing output or resetting err. More... | |
| BA_API int | JEncoder_flush (JEncoder *o) |
| Flush the underlying writer if JErr is clear. More... | |
| BA_API int | JEncoder_commit (JEncoder *o) |
| Permit a new top-level value and flush the writer. More... | |
| BA_API int | JEncoder_setInt (JEncoder *o, S32 val) |
| Write a JSON number. More... | |
| BA_API int | JEncoder_setLong (JEncoder *o, S64 val) |
| Write a JSON number. More... | |
| BA_API int | JEncoder_setDouble (JEncoder *o, double val) |
| Write a JSON number. More... | |
| BA_API int | JEncoder_fmtString (JEncoder *o, const char *fmt,...) |
| Format text inside JSON quotation marks. More... | |
| BA_API int | JEncoder_vFmtString (JEncoder *o, const char *fmt, va_list argList) |
| Write formatted text between JSON quotes, or JSON null. More... | |
| BA_API int | JEncoder_setString (JEncoder *o, const char *val, size_t len) |
| Write a quoted JSON string through BufPrint::jsonString. More... | |
| BA_API int | JEncoder_b64enc (JEncoder *o, const void *source, S32 slen) |
| Write binary data as a quoted standard Base64 string. More... | |
| BA_API int | JEncoder_setBoolean (JEncoder *o, BaBool val) |
| Write a JSON boolean. More... | |
| BA_API int | JEncoder_setNull (JEncoder *o) |
| Write JSON null. More... | |
| BA_API int | JEncoder_setJV (JEncoder *o, struct JVal *val, BaBool iterateNext) |
| Serialize a JVal node and its child values. More... | |
| BA_API int | JEncoder_set (JEncoder *o, const char *fmt,...) |
| Encode/serialize C structs/data to JSON using formatted output. More... | |
| BA_API int | JEncoder_setName (JEncoder *o, const char *name) |
| Write an object member name before its value. More... | |
| BA_API int | JEncoder_beginObject (JEncoder *o) |
| Begin a JSON object value; call setName() before each member value. More... | |
| BA_API int | JEncoder_endObject (JEncoder *o) |
| Close the current object after its final complete member. More... | |
| BA_API int | JEncoder_beginArray (JEncoder *o) |
| Begin a JSON array value; array elements have no member-name call. More... | |
| BA_API int | JEncoder_endArray (JEncoder *o) |
| Close the current array after its final complete element. More... | |
| BA_API int | JErr_setTooFewParams (JErr *o) |
| Record JErrT_InvalidMethodParams with a static message if no error exists. More... | |
| BA_API int | JErr_setTypeErr (JErr *o, JVType expT, JVType recT) |
| Record JErrT_WrongType only when no earlier error exists. More... | |
| BA_API int | JErr_setError (JErr *o, JErrT err, const char *msg) |
| Record an error only when no earlier error exists. More... | |
| BA_API void | JParser_constructor (JParser *o, JParserIntf *intf, char *nameBuf, int namebufSize, AllocatorIntf *alloc, int extraStackLen) |
| Create a JSON parser object. More... | |
| BA_API int | JParser_parse (JParser *o, const U8 *buf, U32 size) |
| Feed or resume parsing a top-level JSON object or array. More... | |
| BA_API void | JParser_destructor (JParser *o) |
| Free internal assembly storage. More... | |
| BA_API JVal * | JVal_vget (JVal *o, JErr *err, const char **fmt, va_list *argList) |
| Extract values as for get(), using mutable format and argument cursors. More... | |
| BA_API JVal * | JVal_get (JVal *o, JErr *err, const char *fmt,...) |
| Get any type of value(s) from a JVal node or JVal tree. More... | |
| BA_API S32 | JVal_getInt (JVal *o, JErr *e) |
| Convert a numeric, boolean, or null node to S32. More... | |
| BA_API S64 | JVal_getLong (JVal *o, JErr *e) |
| Convert a numeric, boolean, or null node to S64. More... | |
| BA_API double | JVal_getDouble (JVal *o, JErr *e) |
| Convert a numeric, boolean, or null node to double. More... | |
| BA_API BaBool | JVal_getBoolean (JVal *o, JErr *e) |
| Read a boolean or null node. More... | |
| BA_API const char * | JVal_getString (JVal *o, JErr *e) |
| Access a string without transferring ownership. More... | |
| BA_API char * | JVal_manageString (JVal *o, JErr *e) |
| Detach a string's storage, leaving a String node with NULL data and zero length. More... | |
| BA_API const char * | JVal_getName (JVal *o) |
| BA_API char * | JVal_manageName (JVal *o) |
| Detach the member name and clear its pointer/length in the node. More... | |
| BA_API JVal * | JVal_getObject (JVal *o, JErr *e) |
| Access the first child of an object node. More... | |
| BA_API JVal * | JVal_getArray (JVal *o, JErr *e) |
| Access the first child of an array node. More... | |
| BA_API JVal * | JVal_getJ (JVal *o, JErr *e) |
| Access the first child of an object or array node. More... | |
| BA_API JVal * | JVal_manageJ (JVal *o, JErr *e) |
| Detach all children from an object/array, leaving the container empty. More... | |
| BA_API S32 | JVal_getLength (struct JVal *o, JErr *e) |
| Count immediate children of this object/array. More... | |
| BA_API int | JVal_unlink (JVal *o, JVal *child) |
| Remove one immediate child without destroying it. More... | |
| BA_API int | JVal_addMember (JVal *o, JErr *e, const char *memberName, JVal *child, AllocatorIntf *dAlloc) |
| Prepend a child to an object. More... | |
| BA_API int | JVal_add (JVal *o, JErr *e, JVal *child) |
| Prepend a child to an array. More... | |
| BA_API void | JVal_terminate (JVal *o, AllocatorIntf *vAlloc, AllocatorIntf *dAlloc) |
| Free this node, all descendants, and all following siblings recursively. More... | |
| BA_API void | JVal_setX (JVal *o, JErr *e, JVType t, void *v) |
| Low-level scalar replacement used by the typed setter macros. More... | |
| BA_API void | JValFact_constructor (JValFact *o, AllocatorIntf *vAlloc, AllocatorIntf *dAlloc) |
| Initialize a factory without allocating a tree. More... | |
| BA_API JVal * | JValFact_mkVal (JValFact *o, JVType t, const void *uv) |
| Allocate a detached typed value. More... | |
| JEncoder::JEncoder (JErr *err, BufPrint *out) | |
| Initialize an encoder without writing output or resetting err. More... | |
| JEncoder::~JEncoder () | |
| Call flush() without freeing the borrowed writer or error object. More... | |
| int | JEncoder::setInt (S32 val) |
| Write a JSON number. More... | |
| int | JEncoder::setLong (S64 val) |
| Write a JSON number. More... | |
| int | JEncoder::setDouble (double val) |
| Write a JSON number. More... | |
| int | JEncoder::fmtString (const char *fmt,...) |
| Format text inside JSON quotation marks. More... | |
| int | JEncoder::vFmtString (const char *fmt, va_list argList) |
| Write formatted text between JSON quotes, or JSON null. More... | |
| int | JEncoder::setString (const char *val, size_t len) |
| Write a quoted JSON string through BufPrint::jsonString. More... | |
| int | JEncoder::b64enc (const void *source, S32 slen) |
| Write binary data as a quoted standard Base64 string. More... | |
| int | JEncoder::setBoolean (bool val) |
| Write a JSON boolean. More... | |
| int | JEncoder::setNull () |
| Write JSON null. More... | |
| int | JEncoder::setJV (struct JVal *val, bool iterateNext=false) |
| Serialize a JVal node and its child values. More... | |
| int | JEncoder::set (const char *fmt,...) |
| Encode/serialize C structs/data to JSON using formatted output. More... | |
| int | JEncoder::setName (const char *name) |
| Write an object member name before its value. More... | |
| int | JEncoder::beginObject () |
| Begin a JSON object value; call setName() before each member value. More... | |
| int | JEncoder::endObject () |
| Close the current object after its final complete member. More... | |
| int | JEncoder::beginArray () |
| Begin a JSON array value; array elements have no member-name call. More... | |
| int | JEncoder::endArray () |
| Close the current array after its final complete element. More... | |
| JErr * | JEncoder::getErr () |
| Query error storage. More... | |
| int | JEncoder::flush () |
| Flush the underlying writer if JErr is clear. More... | |
| int | JEncoder::commit () |
| Permit a new top-level value and flush the writer. More... | |
| BufPrint * | JEncoder::getBufPrint () |
| Query output storage. More... | |
| JErr::JErr () | |
| Initialize only the error code to JErrT_NoErr. More... | |
| void | JErr::reset () |
| Clear the error code and message pointer so the object can be reused. More... | |
| bool | JErr::isError () |
| bool | JErr::noError () |
| JErrT | JErr::getErrT () |
| JVType | JErr::getExpT () |
| JVType | JErr::getRecT () |
| int | JErr::setTooFewParams () |
| Record JErrT_InvalidMethodParams with a static message if no error exists. More... | |
| int | JErr::setTypeErr (JVType expT, JVType recT) |
| Record JErrT_WrongType only when no earlier error exists. More... | |
| int | JErr::setError (JErrT err, const char *msg) |
| Record an error only when no earlier error exists. More... | |
| JParser::JParser (JParserIntf *intf, char *nameBuf, int namebufSize, AllocatorIntf *alloc, int extraStackLen=0) | |
| Create a JSON parser object. More... | |
| int | JParser::parse (const U8 *buf, U32 size) |
| Feed or resume parsing a top-level JSON object or array. More... | |
| JParser::~JParser () | |
| Free internal assembly storage. More... | |
| JParsStat | JParser::getStatus () |
| Inspect the last parser result without changing state. More... | |
| JVType | JVal::getType () |
| JVal * | JVal::vget (JErr *err, const char **fmt, va_list *argList) |
| Extract values as for get(), using mutable format and argument cursors. More... | |
| JVal * | JVal::get (JErr *err, const char *fmt,...) |
| Get any type of value(s) from a JVal node or JVal tree. More... | |
| S32 | JVal::getInt (JErr *e) |
| Convert a numeric, boolean, or null node to S32. More... | |
| S64 | JVal::getLong (JErr *e) |
| Convert a numeric, boolean, or null node to S64. More... | |
| double | JVal::getDouble (JErr *e) |
| Convert a numeric, boolean, or null node to double. More... | |
| BaBool | JVal::getBoolean (JErr *e) |
| Read a boolean or null node. More... | |
| const char * | JVal::getString (JErr *e) |
| Access a string without transferring ownership. More... | |
| size_t | JVal::getStringLen () |
| char * | JVal::manageString (JErr *e) |
| Detach a string's storage, leaving a String node with NULL data and zero length. More... | |
| const char * | JVal::getName () |
| size_t | JVal::getNameLen () |
| char * | JVal::manageName () |
| Detach the member name and clear its pointer/length in the node. More... | |
| JVal * | JVal::getNextElem () |
| JVal * | JVal::getObject (JErr *e) |
| Access the first child of an object node. More... | |
| JVal * | JVal::getArray (JErr *e) |
| Access the first child of an array node. More... | |
| JVal * | JVal::getJ (JErr *e) |
| Access the first child of an object or array node. More... | |
| JVal * | JVal::manageJ (JErr *e) |
| Detach all children from an object/array, leaving the container empty. More... | |
| S32 | JVal::getLength (JErr *e) |
| Count immediate children of this object/array. More... | |
| void | JVal::setInt (JErr *e, S32 v) |
| Replace a scalar value and set its type. More... | |
| void | JVal::setLong (JErr *e, S64 v) |
| Replace a scalar value and set its type. More... | |
| void | JVal::setDouble (JErr *e, double v) |
| Replace a scalar value and set its type. More... | |
| void | JVal::setBoolean (JErr *e, BaBool v) |
| Replace a scalar value and set its type. More... | |
| void | JVal::setNull (JErr *e) |
| Replace a scalar value with JSON null. More... | |
| void | JVal::setString (JErr *e, char *v) |
| Store a string pointer without copying it and set the node's type. More... | |
| bool | JVal::isObjectMember () |
| int | JVal::unlink (JVal *child) |
| Remove one immediate child without destroying it. More... | |
| int | JVal::addMember (JErr *e, const char *memberName, JVal *child, AllocatorIntf *dAlloc) |
| Prepend a child to an object. More... | |
| int | JVal::add (JErr *e, JVal *child) |
| Prepend a child to an array. More... | |
| void | JVal::terminate (AllocatorIntf *vAlloc, AllocatorIntf *dAlloc) |
| Free this node, all descendants, and all following siblings recursively. More... | |
| JValFact::JValFact (AllocatorIntf *vAlloc, AllocatorIntf *dAlloc) | |
| Initialize a factory without allocating a tree. More... | |
| JVal * | JValFact::mkString (const char *v) |
| Create a detached string node. More... | |
| JVal * | JValFact::mkDouble (double v) |
| Create a detached double node. More... | |
| JVal * | JValFact::mkInt (S32 v) |
| Create a detached int node. More... | |
| JVal * | JValFact::mkLong (S64 v) |
| Create a detached long node. More... | |
| JVal * | JValFact::mkBoolean (bool v) |
| Create a detached boolean node. More... | |
| JVal * | JValFact::mkNull () |
| Create a detached null node. More... | |
| JVal * | JValFact::mkObject () |
| Create a detached object node. More... | |
| JVal * | JValFact::mkArray () |
| Create a detached array node. More... | |
| #define JE_MEMBER | ( | o, | |
| m | |||
| ) | #m, (o)->m |
JEncoder::set helper macro, used when setting a value for an object.
encoder.set("{d}", JE_MEMBER(structval, membername));
| #define JEncoder_destructor | ( | o | ) | JEncoder_flush(o) |
Call flush() without freeing the borrowed writer or error object.
The C++ destructor cannot report a flush failure; call flush() explicitly and check its result before destruction when the outcome matters.
| o | Required initialized encoder. |
| #define JEncoder_getBufPrint | ( | o | ) | (o)->out |
Query output storage.
| o | Required initialized encoder. |
| #define JEncoder_getErr | ( | o | ) | (o)->err |
Query error storage.
| o | Required initialized encoder. |
| #define JErr_constructor | ( | o | ) | (o)->err=JErrT_NoErr |
Initialize only the error code to JErrT_NoErr.
Other fields are not initialized; use reset() if a cleared message pointer is needed.
| o | Required storage to initialize. |
| #define JErr_getErrT | ( | o | ) | (o)->err |
| o | Required initialized error object. |
| #define JErr_getExpT | ( | o | ) | (o)->expType |
| o | Required initialized error object. |
| #define JErr_getRecT | ( | o | ) | (o)->recType |
| o | Required initialized error object. |
| #define JErr_isError | ( | o | ) | ((o)->err!=JErrT_NoErr) |
| o | Required initialized error object. |
| #define JErr_noError | ( | o | ) | ((o)->err==JErrT_NoErr) |
| o | Required initialized error object. |
| #define JErr_reset | ( | o | ) | (o)->err=JErrT_NoErr, (o)->msg=0 |
Clear the error code and message pointer so the object can be reused.
Expected/received type fields are not reset.
| o | Required initialized error object. |
| #define JParser_getStatus | ( | o | ) | ((JParsStat)(o)->status) |
Inspect the last parser result without changing state.
| o | Required initialized parser. |
| #define JVal_getNameLen | ( | o | ) | (o)->memberNameLen |
| o | Required live node. |
| #define JVal_getNextElem | ( | o | ) | (o)->next |
| o | Required live node. |
| #define JVal_getStringLen | ( | o | ) | (o)->stringLen |
| o | Required live node. |
| #define JVal_getType | ( | o | ) | (o)->type |
| o | Required live node. |
| #define JVal_isObjectMember | ( | o | ) | ((o)->memberName != 0) |
| o | Required live node. |
| #define JVal_setBoolean | ( | o, | |
| e, | |||
| v | |||
| ) | JVal_setX(o, e, JVType_Boolean, &v) |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New BaBool value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| o | Required live node. The C macro requires v to be an addressable variable of the declared type. |
| #define JVal_setDouble | ( | o, | |
| e, | |||
| v | |||
| ) | JVal_setX(o, e, JVType_Double, &v) |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New double value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| o | Required live node. The C macro requires v to be an addressable variable of the declared type. |
| #define JVal_setInt | ( | o, | |
| e, | |||
| v | |||
| ) | JVal_setX(o, e, JVType_Int, &v) |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New S32 value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| o | Required live node. The C macro requires v to be an addressable variable of the declared type. |
| #define JVal_setLong | ( | o, | |
| e, | |||
| v | |||
| ) | JVal_setX(o, e, JVType_Long, &v) |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New S64 value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| o | Required live node. The C macro requires v to be an addressable variable of the declared type. |
| #define JVal_setNull | ( | o, | |
| e | |||
| ) | JVal_setX(o, e, JVType_Null, 0) |
Replace a scalar value with JSON null.
| e | Required initialized error container. Objects/arrays and attached strings cannot be replaced; detach/release a string first. Requires a live non-NULL node. No return value; inspect e. |
| o | Required live node. |
| #define JVal_setString | ( | o, | |
| e, | |||
| v | |||
| ) | JVal_setX(o, e, JVType_String, v) |
Store a string pointer without copying it and set the node's type.
| e | Required initialized error container receiving refusal details. |
| v | NUL-terminated string allocated compatibly with the tree's data allocator, or NULL for an empty stored pointer. strlen determines its length. On success the node takes ownership. On failure ownership stays with the caller. Detach/release an old string first; object/array nodes cannot be replaced. Requires a live node. No return value; inspect e. |
| o | Required live node. |
| #define JValFact_mkArray | ( | o | ) | JValFact_mkVal(o, JVType_Array, 0) |
Create a detached array node.
| o | Required initialized factory. |
| #define JValFact_mkBoolean | ( | o, | |
| v | |||
| ) | JValFact_mkVal(o, JVType_Boolean, &v) |
Create a detached boolean node.
| v | Value copied into the new node. |
| o | Required initialized factory. The C macro requires v to be an addressable variable of its exact numeric type. |
| #define JValFact_mkDouble | ( | o, | |
| v | |||
| ) | JValFact_mkVal(o, JVType_Double, &v) |
Create a detached double node.
| v | Value copied into the new node. |
| o | Required initialized factory. The C macro requires v to be an addressable variable of its exact numeric type. |
| #define JValFact_mkInt | ( | o, | |
| v | |||
| ) | JValFact_mkVal(o, JVType_Int, &v) |
Create a detached int node.
| v | Value copied into the new node. |
| o | Required initialized factory. The C macro requires v to be an addressable variable of its exact numeric type. |
| #define JValFact_mkLong | ( | o, | |
| v | |||
| ) | JValFact_mkVal(o, JVType_Long, &v) |
Create a detached long node.
| v | Value copied into the new node. |
| o | Required initialized factory. The C macro requires v to be an addressable variable of its exact numeric type. |
| #define JValFact_mkNull | ( | o | ) | JValFact_mkVal(o, JVType_Null, 0) |
Create a detached null node.
| o | Required initialized factory. |
| #define JValFact_mkObject | ( | o | ) | JValFact_mkVal(o, JVType_Object, 0) |
Create a detached object node.
| o | Required initialized factory. |
| #define JValFact_mkString | ( | o, | |
| v | |||
| ) | JValFact_mkVal(o, JVType_String, v) |
Create a detached string node.
| v | Required NUL-terminated string, copied with dAlloc. |
| o | Required initialized factory. |
The JEncoder can serialize a JSON JVAL syntax tree to the JSON text format.
The JEncoder can also be used for assembling JSON text by calling the primitive methods in this class. Supply a valid sequence of names, values, and matching container delimiters. Writes are incremental: failure can leave partial output and does not roll back prior bytes. The borrowed JErr retains errors until explicitly reset by the caller.
Example:
| typedef U8 JParserStackNode |
The stack used internally by JParser.
| typedef struct JParserVal JParserVal |
The parser sets a JParserVal before calling the parser callback JParserIntf.
The JValFact enables one to manually create a JVal syntax tree or add nodes to an existing syntax tree.
| enum JErrT |
JSON error codes.
| Enumerator | |
|---|---|
| JErrT_NoErr | No error. |
| JErrT_JsonErr | Generic error. |
| JErrT_WrongType | Unexpected JSON type when parsing.
|
| JErrT_InvalidMethodParams | Invalid RPC method parameters. |
| JErrT_FmtValErr | |
| JErrT_MemErr | Memory allocation error. |
| JErrT_IOErr | I/O error. |
| enum JParserT |
Type 't' in JParserVal.
| enum JParsStat |
JSON Parser Status.
| Enumerator | |
|---|---|
| JParsStat_DoneEOS | The parser completed parsing a new JSON object. |
| JParsStat_Done | The parser completed parsing a new JSON object, but found the unread bytes in the provided buffer. They have not yet been validated as another object or array. |
| JParsStat_NeedMoreData | The parser needs more data to complete. |
| JParsStat_ParseErr | JSON Parse error. |
| JParsStat_IntfErr | The JParserIntf interface reported a problem. |
| JParsStat_MemErr | Memory allocation error. |
| JParsStat_StackOverflow | Object nested too deep. Increase stack size in constructor |
| enum JVType |
The JSON types.
Prepend a child to an array.
| e | Required initialized error container. |
| child | Required detached node with next=NULL and no other owner. Use an unnamed node for an array element. No copy or allocation is performed. |
| int JVal::addMember | ( | JErr * | e, |
| const char * | memberName, | ||
| JVal * | child, | ||
| AllocatorIntf * | dAlloc | ||
| ) |
Prepend a child to an object.
| e | Required initialized error container. |
| memberName | Required NUL-terminated name used only if child has no name. An existing child name is retained; this does not rename it or check duplicates. |
| child | Required detached node with next=NULL and no other owning parent. |
| dAlloc | Allocator used to copy a missing member name. If NULL, the supplied pointer is stored directly; its eventual release must still match tree ownership. |
| int JEncoder::b64enc | ( | const void * | source, |
| S32 | slen | ||
| ) |
Write binary data as a quoted standard Base64 string.
| source | Readable bytes borrowed for this call; required for positive slen. |
| slen | Nonnegative source byte count. |
| int JEncoder::beginArray | ( | ) |
Begin a JSON array value; array elements have no member-name call.
| int JEncoder::beginObject | ( | ) |
| int JEncoder::commit | ( | ) |
| int JEncoder::endArray | ( | ) |
Close the current array after its final complete element.
| int JEncoder::endObject | ( | ) |
Close the current object after its final complete member.
| int JEncoder::flush | ( | ) |
Flush the underlying writer if JErr is clear.
| int JEncoder::fmtString | ( | const char * | fmt, |
| ... | |||
| ) |
Format text inside JSON quotation marks.
| fmt | BufPrint format string, or NULL to write JSON null. Variadic arguments must match its conversions. Formatted text is not automatically JSON-escaped; use appropriate BufPrint conversions or setString() instead. |
Get any type of value(s) from a JVal node or JVal tree.
The get method works in a similar fashion to the ANSI C function scanf; thus, it performs formatted input conversion.
The method internally uses the 'getXXX' methods in this class for extracting the actual value(s), where XXX is Int, Long, etc.. The following table shows the relationship between the format flags and the JSON value extracted:
| JSON type | Format flag | C type | Method |
|---|---|---|---|
| Number | d | S32* | JVal::getInt |
| Number | l | S64* | JVal::getLong |
| Number | f | double* | JVal::getDouble |
| boolean | b | BaBool* | JVal::getBoolean |
| string | s | const char** | JVal::getString |
| Any type | J | JVal** | All methods |
| Start object | { | n/a | JVal::getObject |
| End object | } | n/a | n/a |
| Start array | [ | n/a | JVal::getArray |
| End array | ] | n/a | n/a |
| Array | A followed by b/d/l/f/s/J | typed output array pointer, int count | All getXXX methods |
See the JSON tutorial, section Using JParserValFact , for examples on how to use JVal.
| err | Required initialized error container, preserved once set. |
| fmt | Required NUL-terminated format. Supply a member name before each object's output arguments. All output pointers must have the exact listed types. A is followed by b/d/l/f/s/J and takes an output array pointer then an int count. Extra source elements are ignored; output can be partially filled before failure. The current empty-array path can leave outputs unchanged without an error, so check the source length when requiring a fixed count. Strings and JVal pointers returned in outputs remain borrowed from the tree. |
Access the first child of an array node.
| e | Required initialized error container. |
Read a boolean or null node.
| e | Required initialized error container. |
| BufPrint * JEncoder::getBufPrint | ( | ) |
Query output storage.
| double JVal::getDouble | ( | JErr * | e | ) |
Convert a numeric, boolean, or null node to double.
| e | Required initialized error container. |
| JErr * JEncoder::getErr | ( | ) |
Query error storage.
| JErrT JErr::getErrT | ( | ) |
| JVType JErr::getExpT | ( | ) |
Convert a numeric, boolean, or null node to S32.
| e | Required initialized error container. |
Access the first child of an object or array node.
| e | Required initialized error container. |
Count immediate children of this object/array.
| e | Required initialized error container. |
Convert a numeric, boolean, or null node to S64.
| e | Required initialized error container. |
| const char * JVal::getName | ( | ) |
| size_t JVal::getNameLen | ( | ) |
| JVal * JVal::getNextElem | ( | ) |
Access the first child of an object node.
| e | Required initialized error container. |
| JVType JErr::getRecT | ( | ) |
| JParsStat JParser::getStatus | ( | ) |
| const char * JVal::getString | ( | JErr * | e | ) |
Access a string without transferring ownership.
| e | Required initialized error container. |
| size_t JVal::getStringLen | ( | ) |
| JVType JVal::getType | ( | ) |
| bool JErr::isError | ( | ) |
| bool JVal::isObjectMember | ( | ) |
Write binary data as a quoted standard Base64 string.
| source | Readable bytes borrowed for this call; required for positive slen. |
| slen | Nonnegative source byte count. |
| o | Required initialized encoder. |
| BA_API int JEncoder_beginArray | ( | JEncoder * | o | ) |
Begin a JSON array value; array elements have no member-name call.
| o | Required initialized encoder. |
| BA_API int JEncoder_beginObject | ( | JEncoder * | o | ) |
Begin a JSON object value; call setName() before each member value.
| o | Required initialized encoder. |
| BA_API int JEncoder_commit | ( | JEncoder * | o | ) |
Permit a new top-level value and flush the writer.
| o | Required initialized encoder. |
Initialize an encoder without writing output or resetting err.
| BA_API int JEncoder_endArray | ( | JEncoder * | o | ) |
Close the current array after its final complete element.
| o | Required initialized encoder. |
| BA_API int JEncoder_endObject | ( | JEncoder * | o | ) |
Close the current object after its final complete member.
| o | Required initialized encoder. |
| BA_API int JEncoder_flush | ( | JEncoder * | o | ) |
Flush the underlying writer if JErr is clear.
| o | Required initialized encoder. |
| BA_API int JEncoder_fmtString | ( | JEncoder * | o, |
| const char * | fmt, | ||
| ... | |||
| ) |
Format text inside JSON quotation marks.
| fmt | BufPrint format string, or NULL to write JSON null. Variadic arguments must match its conversions. Formatted text is not automatically JSON-escaped; use appropriate BufPrint conversions or setString() instead. |
| o | Required initialized encoder. |
| BA_API int JEncoder_set | ( | JEncoder * | o, |
| const char * | fmt, | ||
| ... | |||
| ) |
Encode/serialize C structs/data to JSON 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 setInt, setLong, etc.. The following table shows the relationship between the format flags and the JSON value:
| JSON type | Format flag | C type | Method |
|---|---|---|---|
| Number | d | S32 | JEncoder::setInt |
| Number | l | S64 | JEncoder::setLong |
| Number | f | double | JEncoder::setDouble |
| boolean | b | BaBool | JEncoder::setBoolean |
| string | s | const char* | JEncoder::setString |
| Any type | J | JVal* | All methods |
| Start object | { | n/a | JEncoder::beginObject |
| End object | } | n/a | JEncoder::endObject |
| Start array | [ | n/a | JEncoder::beginArray |
| End array | ] | n/a | JEncoder::endArray |
| Array | A followed by b, d, f, s, or J | int count, then typed array pointer | All setXXX methods |
| fmt | Required NUL-terminated format with matching braces/brackets. Pass an object member name (const char*) before each object's value arguments. Flag n writes null without a value argument. Flag A takes an int count and an array pointer of BaBool, S32, double, const char*, or JVal* elements, selected by its following flag; long-integer arrays are not supported. All strings, arrays, and JVal trees are borrowed only for this call. Floating and JVal support depend on the build configuration. |
| o | Required initialized encoder. |
Write a JSON boolean.
| val | boolean value. |
| o | Required initialized encoder. |
| BA_API int JEncoder_setDouble | ( | JEncoder * | o, |
| double | val | ||
| ) |
Write a JSON number.
| val | finite double value. Uses BufPrint floating formatting (%f), not a lossless round-trip conversion. Available only when double support is enabled. |
| o | Required initialized encoder. |
Write a JSON number.
| val | S32 signed integer value. |
| o | Required initialized encoder. |
Serialize a JVal node and its child values.
| val | Borrowed acyclic node/tree; NULL emits nothing. |
| iterateNext | False (default) handles this node only; true also serializes its next-linked siblings in the current container context. Member names are emitted for object-member nodes when needed. No tree storage is taken. |
| o | Required initialized encoder. |
Write a JSON number.
| val | S64 signed integer value. |
| o | Required initialized encoder. |
| BA_API int JEncoder_setName | ( | JEncoder * | o, |
| const char * | name | ||
| ) |
Write an object member name before its value.
| name | Required borrowed NUL-terminated UTF-8 name, escaped as JSON. Call only inside an object, once before each value. |
| o | Required initialized encoder. |
| BA_API int JEncoder_setNull | ( | JEncoder * | o | ) |
Write JSON null.
| o | Required initialized encoder. |
| BA_API int JEncoder_setString | ( | JEncoder * | o, |
| const char * | val, | ||
| size_t | len | ||
| ) |
Write a quoted JSON string through BufPrint::jsonString.
| val | Borrowed UTF-8 bytes, or NULL to write JSON null. |
| len | Byte count, excluding any terminator; ignored when val is NULL. Embedded NUL bytes are encoded as part of the string. |
| o | Required initialized encoder. |
| BA_API int JEncoder_vFmtString | ( | JEncoder * | o, |
| const char * | fmt, | ||
| va_list | argList | ||
| ) |
Write formatted text between JSON quotes, or JSON null.
| fmt | Borrowed BufPrint format string, or NULL for null. Ensure its output is properly escaped JSON string content; this function only adds outer quotes. |
| argList | Valid argument list matching fmt, consumed according to va_list rules. Use va_copy if the original list must be reused. |
| o | Required initialized encoder. |
| JErr::JErr | ( | ) |
Initialize only the error code to JErrT_NoErr.
Other fields are not initialized; use reset() if a cleared message pointer is needed.
Record an error only when no earlier error exists.
| err | Error type to store. |
| msg | Borrowed message pointer, or NULL. Retain its storage while the error is used. |
| o | Required initialized error object. |
| BA_API int JErr_setTooFewParams | ( | JErr * | o | ) |
Record JErrT_InvalidMethodParams with a static message if no error exists.
| o | Required initialized error object. |
Record JErrT_WrongType only when no earlier error exists.
| expT | Expected JVType. |
| recT | Received JVType. |
| o | Initialized error object, or NULL to return -1. |
| JParser::JParser | ( | JParserIntf * | intf, |
| char * | nameBuf, | ||
| int | namebufSize, | ||
| AllocatorIntf * | alloc, | ||
| int | extraStackLen = 0 |
||
| ) |
Create a JSON parser object.
| intf | Required borrowed interface with a non-NULL callback. Provide your own callback or use one of JParserValFact or JDecoder. |
| nameBuf | is a buffer required for temporarily storing parsed object member names. Required writable storage, retained for the parser lifetime; it must fit the longest name plus its terminator. |
| namebufSize | Positive capacity of nameBuf in bytes. This buffer does not grow; an oversized member name reports JParsStat_MemErr. |
| alloc | Borrowed allocator for string/number assembly, retained until destruction. Supply an allocation/reallocation/free-capable allocator for general input; NULL disables dynamic assembly rather than choosing a default. |
| extraStackLen | is an undocumented feature and must be set to 0. |
| BA_API void JParser_constructor | ( | JParser * | o, |
| JParserIntf * | intf, | ||
| char * | nameBuf, | ||
| int | namebufSize, | ||
| AllocatorIntf * | alloc, | ||
| int | extraStackLen | ||
| ) |
Create a JSON parser object.
| intf | Required borrowed interface with a non-NULL callback. Provide your own callback or use one of JParserValFact or JDecoder. |
| nameBuf | is a buffer required for temporarily storing parsed object member names. Required writable storage, retained for the parser lifetime; it must fit the longest name plus its terminator. |
| namebufSize | Positive capacity of nameBuf in bytes. This buffer does not grow; an oversized member name reports JParsStat_MemErr. |
| alloc | Borrowed allocator for string/number assembly, retained until destruction. Supply an allocation/reallocation/free-capable allocator for general input; NULL disables dynamic assembly rather than choosing a default. |
| extraStackLen | is an undocumented feature and must be set to 0. |
| o | Required storage to initialize. |
| BA_API void JParser_destructor | ( | JParser * | o | ) |
Free internal assembly storage.
The callback interface, nameBuf, allocator, and input chunks are borrowed and are not freed. Stop parsing first.
| o | Required initialized parser. |
Feed or resume parsing a top-level JSON object or array.
| buf | Borrowed input chunk. Supply a new chunk after DoneEOS or NeedMoreData. After Done, the parser retains unread bytes from the previous chunk and ignores these new arguments; call again before replacing that buffer. |
| size | Byte length of a new chunk, excluding any terminator. Retain its storage until the parser has consumed it. No NUL termination is needed. |
| o | Required initialized parser. |
Prepend a child to an array.
| e | Required initialized error container. |
| child | Required detached node with next=NULL and no other owner. Use an unnamed node for an array element. No copy or allocation is performed. |
| o | Required live node. |
| BA_API int JVal_addMember | ( | JVal * | o, |
| JErr * | e, | ||
| const char * | memberName, | ||
| JVal * | child, | ||
| AllocatorIntf * | dAlloc | ||
| ) |
Prepend a child to an object.
| e | Required initialized error container. |
| memberName | Required NUL-terminated name used only if child has no name. An existing child name is retained; this does not rename it or check duplicates. |
| child | Required detached node with next=NULL and no other owning parent. |
| dAlloc | Allocator used to copy a missing member name. If NULL, the supplied pointer is stored directly; its eventual release must still match tree ownership. |
| o | Required live node. |
Get any type of value(s) from a JVal node or JVal tree.
The get method works in a similar fashion to the ANSI C function scanf; thus, it performs formatted input conversion.
The method internally uses the 'getXXX' methods in this class for extracting the actual value(s), where XXX is Int, Long, etc.. The following table shows the relationship between the format flags and the JSON value extracted:
| JSON type | Format flag | C type | Method |
|---|---|---|---|
| Number | d | S32* | JVal::getInt |
| Number | l | S64* | JVal::getLong |
| Number | f | double* | JVal::getDouble |
| boolean | b | BaBool* | JVal::getBoolean |
| string | s | const char** | JVal::getString |
| Any type | J | JVal** | All methods |
| Start object | { | n/a | JVal::getObject |
| End object | } | n/a | n/a |
| Start array | [ | n/a | JVal::getArray |
| End array | ] | n/a | n/a |
| Array | A followed by b/d/l/f/s/J | typed output array pointer, int count | All getXXX methods |
See the JSON tutorial, section Using JParserValFact , for examples on how to use JVal.
| err | Required initialized error container, preserved once set. |
| fmt | Required NUL-terminated format. Supply a member name before each object's output arguments. All output pointers must have the exact listed types. A is followed by b/d/l/f/s/J and takes an output array pointer then an int count. Extra source elements are ignored; output can be partially filled before failure. The current empty-array path can leave outputs unchanged without an error, so check the source length when requiring a fixed count. Strings and JVal pointers returned in outputs remain borrowed from the tree. |
| o | Node pointer; see the operation contract for NULL handling. |
Access the first child of an array node.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Read a boolean or null node.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Convert a numeric, boolean, or null node to double.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Convert a numeric, boolean, or null node to S32.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Access the first child of an object or array node.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Count immediate children of this object/array.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Convert a numeric, boolean, or null node to S64.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
| BA_API const char * JVal_getName | ( | JVal * | o | ) |
| o | Node pointer; see the operation contract for NULL handling. |
Access the first child of an object node.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Access a string without transferring ownership.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Detach all children from an object/array, leaving the container empty.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
| BA_API char * JVal_manageName | ( | JVal * | o | ) |
Detach the member name and clear its pointer/length in the node.
| o | Node pointer; see the operation contract for NULL handling. |
Detach a string's storage, leaving a String node with NULL data and zero length.
| e | Required initialized error container. |
| o | Node pointer; see the operation contract for NULL handling. |
Low-level scalar replacement used by the typed setter macros.
| o | Required live scalar node; objects/arrays cannot be replaced. |
| e | Required initialized error container. An attached old string must first be detached; failure is reported here, with no return value. |
| t | New type: Int, Long, Double (when enabled), Boolean, String, or Null. |
| v | Pointer to S32, S64, double, or BaBool storage matching t; for String it is the string itself and ownership transfers on success. NULL is accepted for Null and as a stored String pointer. See the typed setters for lifetime. |
| BA_API void JVal_terminate | ( | JVal * | o, |
| AllocatorIntf * | vAlloc, | ||
| AllocatorIntf * | dAlloc | ||
| ) |
Free this node, all descendants, and all following siblings recursively.
| vAlloc | Required original allocator for node storage. |
| dAlloc | Required original allocator for strings/member names. Detach the chain from its owner before calling; every freed pointer becomes invalid. Allocators must accept all storage being released. The C function accepts NULL for an empty chain, but a C++ member call still requires a node. |
| o | Node pointer; see the operation contract for NULL handling. |
Remove one immediate child without destroying it.
| child | Required child to detach from this live object/array. |
| o | Required live node. |
Extract values as for get(), using mutable format and argument cursors.
| err | Required initialized error container. |
| fmt | Required pointer to a NUL-terminated format cursor, advanced by parsing. |
| argList | Required pointer to a matching initialized va_list, consumed. |
| o | Node pointer; see the operation contract for NULL handling. |
| JValFact::JValFact | ( | AllocatorIntf * | vAlloc, |
| AllocatorIntf * | dAlloc | ||
| ) |
Initialize a factory without allocating a tree.
| vAlloc | Required borrowed allocator for nodes. |
| dAlloc | Required borrowed allocator for strings. Allocators may be the same and must outlive all storage they allocate. NULL does not select a default. The caller owns created nodes; the factory does not track or free them. |
| BA_API void JValFact_constructor | ( | JValFact * | o, |
| AllocatorIntf * | vAlloc, | ||
| AllocatorIntf * | dAlloc | ||
| ) |
Initialize a factory without allocating a tree.
| vAlloc | Required borrowed allocator for nodes. |
| dAlloc | Required borrowed allocator for strings. Allocators may be the same and must outlive all storage they allocate. NULL does not select a default. The caller owns created nodes; the factory does not track or free them. |
| o | Required storage to initialize. |
Allocate a detached typed value.
| o | Required initialized factory. |
| t | Supported JVType, excluding InvalidType; Double requires double support. |
| uv | Pointer to the matching S32/S64/double/BaBool scalar, or the required NUL-terminated string itself for String. Ignored for Null/Object/Array. Values and strings are copied, not retained. |
Detach all children from an object/array, leaving the container empty.
| e | Required initialized error container. |
| char * JVal::manageName | ( | ) |
Detach the member name and clear its pointer/length in the node.
| char * JVal::manageString | ( | JErr * | e | ) |
Detach a string's storage, leaving a String node with NULL data and zero length.
| e | Required initialized error container. |
| JVal * JValFact::mkArray | ( | ) |
Create a detached array node.
| JVal * JValFact::mkBoolean | ( | bool | v | ) |
Create a detached boolean node.
| v | Value copied into the new node. |
| JVal * JValFact::mkDouble | ( | double | v | ) |
Create a detached double node.
| v | Value copied into the new node. |
Create a detached int node.
| v | Value copied into the new node. |
Create a detached long node.
| v | Value copied into the new node. |
| JVal * JValFact::mkNull | ( | ) |
Create a detached null node.
| JVal * JValFact::mkObject | ( | ) |
Create a detached object node.
| JVal * JValFact::mkString | ( | const char * | v | ) |
Create a detached string node.
| v | Required NUL-terminated string, copied with dAlloc. |
| bool JErr::noError | ( | ) |
Feed or resume parsing a top-level JSON object or array.
| buf | Borrowed input chunk. Supply a new chunk after DoneEOS or NeedMoreData. After Done, the parser retains unread bytes from the previous chunk and ignores these new arguments; call again before replacing that buffer. |
| size | Byte length of a new chunk, excluding any terminator. Retain its storage until the parser has consumed it. No NUL termination is needed. |
| void JErr::reset | ( | ) |
Clear the error code and message pointer so the object can be reused.
Expected/received type fields are not reset.
| int JEncoder::set | ( | const char * | fmt, |
| ... | |||
| ) |
Encode/serialize C structs/data to JSON 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 setInt, setLong, etc.. The following table shows the relationship between the format flags and the JSON value:
| JSON type | Format flag | C type | Method |
|---|---|---|---|
| Number | d | S32 | JEncoder::setInt |
| Number | l | S64 | JEncoder::setLong |
| Number | f | double | JEncoder::setDouble |
| boolean | b | BaBool | JEncoder::setBoolean |
| string | s | const char* | JEncoder::setString |
| Any type | J | JVal* | All methods |
| Start object | { | n/a | JEncoder::beginObject |
| End object | } | n/a | JEncoder::endObject |
| Start array | [ | n/a | JEncoder::beginArray |
| End array | ] | n/a | JEncoder::endArray |
| Array | A followed by b, d, f, s, or J | int count, then typed array pointer | All setXXX methods |
| fmt | Required NUL-terminated format with matching braces/brackets. Pass an object member name (const char*) before each object's value arguments. Flag n writes null without a value argument. Flag A takes an int count and an array pointer of BaBool, S32, double, const char*, or JVal* elements, selected by its following flag; long-integer arrays are not supported. All strings, arrays, and JVal trees are borrowed only for this call. Floating and JVal support depend on the build configuration. |
| int JEncoder::setBoolean | ( | bool | val | ) |
Write a JSON boolean.
| val | boolean value. |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New BaBool value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| int JEncoder::setDouble | ( | double | val | ) |
Write a JSON number.
| val | finite double value. Uses BufPrint floating formatting (%f), not a lossless round-trip conversion. Available only when double support is enabled. |
| void JVal::setDouble | ( | JErr * | e, |
| double | v | ||
| ) |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New double value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| int JErr::setError | ( | JErrT | err, |
| const char * | msg | ||
| ) |
Record an error only when no earlier error exists.
| err | Error type to store. |
| msg | Borrowed message pointer, or NULL. Retain its storage while the error is used. |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New S32 value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| int JEncoder::setInt | ( | S32 | val | ) |
Write a JSON number.
| val | S32 signed integer value. |
| int JEncoder::setJV | ( | struct JVal * | val, |
| bool | iterateNext = false |
||
| ) |
Serialize a JVal node and its child values.
| val | Borrowed acyclic node/tree; NULL emits nothing. |
| iterateNext | False (default) handles this node only; true also serializes its next-linked siblings in the current container context. Member names are emitted for object-member nodes when needed. No tree storage is taken. |
Replace a scalar value and set its type.
| e | Required initialized error container receiving refusal details. |
| v | New S64 value. No return value; inspect e. Cannot replace an object/array or an attached string. Detach and release an old string first. Requires a live non-NULL node; existing errors do not by themselves prevent this setter from changing the node. |
| int JEncoder::setLong | ( | S64 | val | ) |
Write a JSON number.
| val | S64 signed integer value. |
| int JEncoder::setName | ( | const char * | name | ) |
Write an object member name before its value.
| name | Required borrowed NUL-terminated UTF-8 name, escaped as JSON. Call only inside an object, once before each value. |
| int JEncoder::setNull | ( | ) |
Write JSON null.
| void JVal::setNull | ( | JErr * | e | ) |
Replace a scalar value with JSON null.
| e | Required initialized error container. Objects/arrays and attached strings cannot be replaced; detach/release a string first. Requires a live non-NULL node. No return value; inspect e. |
| int JEncoder::setString | ( | const char * | val, |
| size_t | len | ||
| ) |
Write a quoted JSON string through BufPrint::jsonString.
| val | Borrowed UTF-8 bytes, or NULL to write JSON null. |
| len | Byte count, excluding any terminator; ignored when val is NULL. Embedded NUL bytes are encoded as part of the string. |
| void JVal::setString | ( | JErr * | e, |
| char * | v | ||
| ) |
Store a string pointer without copying it and set the node's type.
| e | Required initialized error container receiving refusal details. |
| v | NUL-terminated string allocated compatibly with the tree's data allocator, or NULL for an empty stored pointer. strlen determines its length. On success the node takes ownership. On failure ownership stays with the caller. Detach/release an old string first; object/array nodes cannot be replaced. Requires a live node. No return value; inspect e. |
| int JErr::setTooFewParams | ( | ) |
Record JErrT_InvalidMethodParams with a static message if no error exists.
Record JErrT_WrongType only when no earlier error exists.
| expT | Expected JVType. |
| recT | Received JVType. |
| void JVal::terminate | ( | AllocatorIntf * | vAlloc, |
| AllocatorIntf * | dAlloc | ||
| ) |
Free this node, all descendants, and all following siblings recursively.
| vAlloc | Required original allocator for node storage. |
| dAlloc | Required original allocator for strings/member names. Detach the chain from its owner before calling; every freed pointer becomes invalid. Allocators must accept all storage being released. The C function accepts NULL for an empty chain, but a C++ member call still requires a node. |
| int JVal::unlink | ( | JVal * | child | ) |
Remove one immediate child without destroying it.
| child | Required child to detach from this live object/array. |
| int JEncoder::vFmtString | ( | const char * | fmt, |
| va_list | argList | ||
| ) |
Write formatted text between JSON quotes, or JSON null.
| fmt | Borrowed BufPrint format string, or NULL for null. Ensure its output is properly escaped JSON string content; this function only adds outer quotes. |
| argList | Valid argument list matching fmt, consumed according to va_list rules. Use va_copy if the original list must be reused. |
Extract values as for get(), using mutable format and argument cursors.
| err | Required initialized error container. |
| fmt | Required pointer to a NUL-terminated format cursor, advanced by parsing. |
| argList | Required pointer to a matching initialized va_list, consumed. |
| JEncoder::~JEncoder | ( | ) |
| JParser::~JParser | ( | ) |
Free internal assembly storage.
The callback interface, nameBuf, allocator, and input chunks are borrowed and are not freed. Stop parsing first.