![]() |
ucx
UAP Common Extensions
|
Interface for parsing data from JSON files. More...
#include "common.h"#include "allocator.h"#include "string.h"#include "buffer.h"#include "array_list.h"Go to the source code of this file.
Data Structures | |
| struct | cx_json_array_s |
| JSON array structure. More... | |
| struct | cx_json_object_s |
| JSON object structure. More... | |
| struct | cx_json_obj_value_s |
| Structure for a key/value entry in a JSON object. More... | |
| struct | cx_json_value_s |
| Structure for a JSON value. More... | |
| struct | cx_json_token_s |
| Internally used structure for a parsed token. More... | |
| struct | cx_json_s |
| The JSON parser interface. More... | |
| struct | cx_json_writer_s |
| The JSON writer settings. More... | |
Macros | |
| #define | cxJsonFill(json, str) |
| Fills the input buffer. | |
| #define | cxJsonCreateString(allocator, str) |
| Creates a new JSON string. | |
| #define | cxJsonObjPut(obj, name, child) |
| Adds or replaces a value within a JSON object. | |
| #define | cxJsonObjPutObj(obj, name) |
| Creates a new JSON object and adds it to an existing object. | |
| #define | cxJsonObjPutArr(obj, name) |
| Creates a new JSON array and adds it to an object. | |
| #define | cxJsonObjPutNumber(obj, name, num) |
| Creates a new JSON number and adds it to an object. | |
| #define | cxJsonObjPutInteger(obj, name, num) |
| Creates a new JSON number, based on an integer, and adds it to an object. | |
| #define | cxJsonObjPutString(obj, name, str) |
| Creates a new JSON string and adds it to an object. | |
| #define | cxJsonObjPutLiteral(obj, name, lit) |
| Creates a new JSON literal and adds it to an object. | |
| #define | cxJsonObjGet(value, name) |
| Returns a value corresponding to a key in a JSON object. | |
| #define | cxJsonObjRemove(value, name) |
| Removes and returns a value corresponding to a key in a JSON object. | |
Typedefs | |
| typedef enum cx_json_token_type | CxJsonTokenType |
| Type alias for the token type enum. | |
| typedef enum cx_json_value_type | CxJsonValueType |
| Type alias for the value type enum. | |
| typedef struct cx_json_s | CxJson |
| Type alias for the JSON parser interface. | |
| typedef struct cx_json_token_s | CxJsonToken |
| Type alias for the token struct. | |
| typedef struct cx_json_value_s | CxJsonValue |
| Type alias for the JSON value struct. | |
| typedef struct cx_json_array_s | CxJsonArray |
| Type alias for the JSON array struct. | |
| typedef struct cx_json_object_s | CxJsonObject |
| Type alias for the JSON object struct. | |
| typedef struct cx_mutstr_s | CxJsonString |
| Type alias for a JSON string. | |
| typedef int64_t | CxJsonInteger |
| Type alias for a number that can be represented as a 64-bit signed integer. | |
| typedef double | CxJsonNumber |
| Type alias for a number that is not an integer. | |
| typedef enum cx_json_literal | CxJsonLiteral |
| Type alias for a JSON literal. | |
| typedef struct cx_json_obj_value_s | CxJsonObjValue |
| Type alias for a key/value pair in a JSON object. | |
| typedef enum cx_json_status | CxJsonStatus |
| Typedef for the JSON status enum. | |
| typedef struct cx_json_writer_s | CxJsonWriter |
| Typedef for the JSON writer. | |
Enumerations | |
| enum | cx_json_token_type { CX_JSON_NO_TOKEN , CX_JSON_TOKEN_ERROR , CX_JSON_TOKEN_BEGIN_ARRAY , CX_JSON_TOKEN_BEGIN_OBJECT , CX_JSON_TOKEN_END_ARRAY , CX_JSON_TOKEN_END_OBJECT , CX_JSON_TOKEN_NAME_SEPARATOR , CX_JSON_TOKEN_VALUE_SEPARATOR , CX_JSON_TOKEN_STRING , CX_JSON_TOKEN_INTEGER , CX_JSON_TOKEN_NUMBER , CX_JSON_TOKEN_LITERAL , CX_JSON_TOKEN_SPACE } |
| The type of the parsed token. More... | |
| enum | cx_json_value_type { CX_JSON_NOTHING , CX_JSON_OBJECT , CX_JSON_ARRAY , CX_JSON_STRING , CX_JSON_INTEGER , CX_JSON_NUMBER , CX_JSON_LITERAL } |
| The type of some JSON value. More... | |
| enum | cx_json_literal { CX_JSON_NULL , CX_JSON_TRUE , CX_JSON_FALSE } |
| JSON literal types. More... | |
| enum | cx_json_status { CX_JSON_NO_ERROR , CX_JSON_NO_DATA , CX_JSON_INCOMPLETE_DATA , CX_JSON_OK , CX_JSON_NULL_DATA , CX_JSON_BUFFER_ALLOC_FAILED , CX_JSON_VALUE_ALLOC_FAILED , CX_JSON_FORMAT_ERROR_NUMBER , CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN } |
| Status codes for the JSON interface. More... | |
Functions | |
| CxJsonWriter | cxJsonWriterCompact (void) |
| Creates a default writer configuration for compact output. | |
| CxJsonWriter | cxJsonWriterPretty (bool use_spaces) |
| Creates a default writer configuration for pretty output. | |
| int | cxJsonWrite (void *target, const CxJsonValue *value, cx_write_func wfunc, const CxJsonWriter *settings) |
| Writes a JSON value to a buffer or stream. | |
| void | cxJsonInit (CxJson *json, const CxAllocator *allocator) |
| Initializes the JSON interface. | |
| void | cxJsonDestroy (CxJson *json) |
| Destroys the JSON interface. | |
| void | cxJsonReset (CxJson *json) |
| Destroys and re-initializes the JSON interface. | |
| int | cxJsonFilln (CxJson *json, const char *buf, size_t len) |
| Fills the input buffer. | |
| static int | cx_json_fill (CxJson *json, cxstring str) |
| Internal function, do not use. | |
| CxJsonValue * | cxJsonCreateObj (const CxAllocator *allocator) |
| Creates a new (empty) JSON object. | |
| CxJsonValue * | cxJsonCreateArr (const CxAllocator *allocator) |
| Creates a new (empty) JSON array. | |
| CxJsonValue * | cxJsonCreateNumber (const CxAllocator *allocator, double num) |
| Creates a new JSON number value. | |
| CxJsonValue * | cxJsonCreateInteger (const CxAllocator *allocator, int64_t num) |
| Creates a new JSON number value based on an integer. | |
| CxJsonValue * | cx_json_create_string (const CxAllocator *allocator, cxstring str) |
| Creates a new JSON string. | |
| CxJsonValue * | cxJsonCreateLiteral (const CxAllocator *allocator, CxJsonLiteral lit) |
| Creates a new JSON literal. | |
| int | cxJsonArrAddNumbers (CxJsonValue *arr, const double *num, size_t count) |
| Adds number values to a JSON array. | |
| int | cxJsonArrAddIntegers (CxJsonValue *arr, const int64_t *num, size_t count) |
| Adds number values, of which all are integers, to a JSON array. | |
| int | cxJsonArrAddStrings (CxJsonValue *arr, const char *const *str, size_t count) |
| Adds strings to a JSON array. | |
| int | cxJsonArrAddCxStrings (CxJsonValue *arr, const cxstring *str, size_t count) |
| Adds strings to a JSON array. | |
| int | cxJsonArrAddLiterals (CxJsonValue *arr, const CxJsonLiteral *lit, size_t count) |
| Adds literals to a JSON array. | |
| int | cxJsonArrAddValues (CxJsonValue *arr, CxJsonValue *const *val, size_t count) |
| Add arbitrary values to a JSON array. | |
| int | cx_json_obj_put (CxJsonValue *obj, cxstring name, CxJsonValue *child) |
| Adds or replaces a value within a JSON object. | |
| CxJsonValue * | cx_json_obj_put_obj (CxJsonValue *obj, cxstring name) |
| Creates a new JSON object and adds it to an existing object. | |
| CxJsonValue * | cx_json_obj_put_arr (CxJsonValue *obj, cxstring name) |
| Creates a new JSON array and adds it to an object. | |
| CxJsonValue * | cx_json_obj_put_number (CxJsonValue *obj, cxstring name, double num) |
| Creates a new JSON number and adds it to an object. | |
| CxJsonValue * | cx_json_obj_put_integer (CxJsonValue *obj, cxstring name, int64_t num) |
| Creates a new JSON number, based on an integer, and adds it to an object. | |
| CxJsonValue * | cx_json_obj_put_string (CxJsonValue *obj, cxstring name, cxstring str) |
| Creates a new JSON string and adds it to an object. | |
| CxJsonValue * | cx_json_obj_put_literal (CxJsonValue *obj, cxstring name, CxJsonLiteral lit) |
| Creates a new JSON literal and adds it to an object. | |
| void | cxJsonValueFree (CxJsonValue *value) |
| Recursively deallocates the memory of a JSON value. | |
| CxJsonStatus | cxJsonNext (CxJson *json, CxJsonValue **value) |
| Tries to obtain the next JSON value. | |
| static bool | cxJsonIsObject (const CxJsonValue *value) |
| Checks if the specified value is a JSON object. | |
| static bool | cxJsonIsArray (const CxJsonValue *value) |
| Checks if the specified value is a JSON array. | |
| static bool | cxJsonIsString (const CxJsonValue *value) |
| Checks if the specified value is a string. | |
| static bool | cxJsonIsNumber (const CxJsonValue *value) |
| Checks if the specified value is a JSON number. | |
| static bool | cxJsonIsInteger (const CxJsonValue *value) |
| Checks if the specified value is an integer number. | |
| static bool | cxJsonIsLiteral (const CxJsonValue *value) |
| Checks if the specified value is a JSON literal. | |
| static bool | cxJsonIsBool (const CxJsonValue *value) |
| Checks if the specified value is a Boolean literal. | |
| static bool | cxJsonIsTrue (const CxJsonValue *value) |
Checks if the specified value is true. | |
| static bool | cxJsonIsFalse (const CxJsonValue *value) |
Checks if the specified value is false. | |
| static bool | cxJsonIsNull (const CxJsonValue *value) |
Checks if the specified value is null. | |
| char * | cxJsonAsString (const CxJsonValue *value) |
| Obtains a C string from the given JSON value. | |
| cxstring | cxJsonAsCxString (const CxJsonValue *value) |
| Obtains a UCX string from the given JSON value. | |
| cxmutstr | cxJsonAsCxMutStr (const CxJsonValue *value) |
| Obtains a mutable UCX string from the given JSON value. | |
| double | cxJsonAsDouble (const CxJsonValue *value) |
| Obtains a double-precision floating-point value from the given JSON value. | |
| int64_t | cxJsonAsInteger (const CxJsonValue *value) |
| Obtains a 64-bit signed integer from the given JSON value. | |
| static bool | cxJsonAsBool (const CxJsonValue *value) |
| Obtains a Boolean value from the given JSON value. | |
| static size_t | cxJsonArrSize (const CxJsonValue *value) |
| Returns the size of a JSON array. | |
| CxJsonValue * | cxJsonArrGet (const CxJsonValue *value, size_t index) |
| Returns an element from a JSON array. | |
| CxJsonValue * | cxJsonArrRemove (CxJsonValue *value, size_t index) |
| Removes an element from a JSON array. | |
| CxIterator | cxJsonArrIter (const CxJsonValue *value) |
| Returns an iterator over the JSON array elements. | |
| static size_t | cxJsonObjSize (const CxJsonValue *value) |
| Returns the size of a JSON object. | |
| CxIterator | cxJsonObjIter (const CxJsonValue *value) |
| Returns an iterator over the JSON object members. | |
| CxJsonValue * | cx_json_obj_get (const CxJsonValue *value, cxstring name) |
| Internal function, do not use. | |
| CxJsonValue * | cx_json_obj_remove (CxJsonValue *value, cxstring name) |
| Internal function, do not use. | |
Interface for parsing data from JSON files.
| #define cxJsonCreateString | ( | allocator, | |
| str ) |
Creates a new JSON string.
| allocator | (CxAllocator*) the allocator to use |
| str | the string |
CxJsonValue*) the new JSON value or NULL if allocation fails | #define cxJsonFill | ( | json, | |
| str ) |
Fills the input buffer.
The JSON interface tries to avoid copying the input data. When you use this function and cxJsonNext() interleaving, no copies are performed. However, you must not free the pointer to the data in that case. When you invoke the fill function more than once before calling cxJsonNext(), the additional data is appended - inevitably leading to an allocation of a new buffer and copying the previous contents.
| json | the JSON interface |
| str | the source string |
| zero | success |
| non-zero | internal allocation error |
| #define cxJsonObjGet | ( | value, | |
| name ) |
Returns a value corresponding to a key in a JSON object.
If the value is not a JSON object, the behavior is undefined.
This function guarantees to return a JSON value. If the object does not contain name, the returned JSON value will be of type CX_JSON_NOTHING, but never NULL.
| value | the JSON object |
| name | the key to look up |
| #define cxJsonObjPut | ( | obj, | |
| name, | |||
| child ) |
Adds or replaces a value within a JSON object.
The value will be directly added and not copied.
name already exists, it will be (recursively) freed with its own allocator.| obj | (CxJsonValue*) the JSON object |
| name | (any string) the name of the value |
| child | (CxJsonValue*) the value |
| zero | success |
| non-zero | allocation failure |
| #define cxJsonObjPutArr | ( | obj, | |
| name ) |
Creates a new JSON array and adds it to an object.
| obj | (CxJsonValue*) the target JSON object |
| name | (any string) the name of the new value |
CxJsonValue*) the new value or NULL if allocation fails | #define cxJsonObjPutInteger | ( | obj, | |
| name, | |||
| num ) |
Creates a new JSON number, based on an integer, and adds it to an object.
| obj | (CxJsonValue*) the target JSON object |
| name | (any string) the name of the new value |
| num | (int64_t) the numeric value |
CxJsonValue*) the new value or NULL if allocation fails | #define cxJsonObjPutLiteral | ( | obj, | |
| name, | |||
| lit ) |
Creates a new JSON literal and adds it to an object.
| obj | (CxJsonValue*) the target JSON object |
| name | (any string) the name of the new value |
| lit | (CxJsonLiteral) the type of literal |
CxJsonValue*) the new value or NULL if allocation fails | #define cxJsonObjPutNumber | ( | obj, | |
| name, | |||
| num ) |
Creates a new JSON number and adds it to an object.
| obj | (CxJsonValue*) the target JSON object |
| name | (any string) the name of the new value |
| num | (double) the numeric value |
CxJsonValue*) the new value or NULL if allocation fails | #define cxJsonObjPutObj | ( | obj, | |
| name ) |
Creates a new JSON object and adds it to an existing object.
| obj | (CxJsonValue*) the target JSON object |
| name | (any string) the name of the new value |
CxJsonValue*) the new value or NULL if allocation fails | #define cxJsonObjPutString | ( | obj, | |
| name, | |||
| str ) |
Creates a new JSON string and adds it to an object.
The string data is copied.
| obj | (CxJsonValue*) the target JSON object |
| name | (any string) the name of the new value |
| str | (any string) the string data |
CxJsonValue*) the new value or NULL if allocation fails | #define cxJsonObjRemove | ( | value, | |
| name ) |
Removes and returns a value corresponding to a key in a JSON object.
If the value is not a JSON object, the behavior is undefined.
This function, in contrast to cxJsonObjGet() returns NULL when the object does not contain name.
| value | the JSON object |
| name | the key to look up |
NULL when the key is not part of the object | enum cx_json_literal |
| enum cx_json_status |
Status codes for the JSON interface.
| Enumerator | |
|---|---|
| CX_JSON_NO_ERROR | Everything is fine. |
| CX_JSON_NO_DATA | The input buffer does not contain more data. |
| CX_JSON_INCOMPLETE_DATA | The input ends unexpectedly. Refill the buffer with cxJsonFill() to complete the JSON data. |
| CX_JSON_OK | Not used as a status and never returned by any function. You can use this enumerator to check for all "good" status results by checking if the status is less than A "good" status means that you can refill data and continue parsing. |
| CX_JSON_NULL_DATA | The input buffer has never been filled. |
| CX_JSON_BUFFER_ALLOC_FAILED | Allocating memory for the internal buffer failed. |
| CX_JSON_VALUE_ALLOC_FAILED | Allocating memory for a JSON value failed. |
| CX_JSON_FORMAT_ERROR_NUMBER | A number value is incorrectly formatted. |
| CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN | The tokenizer found something unexpected. |
| enum cx_json_token_type |
The type of the parsed token.
| enum cx_json_value_type |
| CxJsonValue * cx_json_create_string | ( | const CxAllocator * | allocator, |
| cxstring | str ) |
Creates a new JSON string.
Internal function - use cxJsonCreateString() instead.
| allocator | the allocator to use |
| str | the string data |
NULL if allocation fails Internal function, do not use.
| json | the JSON interface |
| str | the string |
| zero | success |
| non-zero | internal allocation error |
| CxJsonValue * cx_json_obj_get | ( | const CxJsonValue * | value, |
| cxstring | name ) |
Internal function, do not use.
| value | the JSON object |
| name | the key to look up |
| int cx_json_obj_put | ( | CxJsonValue * | obj, |
| cxstring | name, | ||
| CxJsonValue * | child ) |
Adds or replaces a value within a JSON object.
Internal function - use cxJsonObjPut().
| obj | the JSON object |
| name | the name of the value |
| child | the value |
| zero | success |
| non-zero | allocation failure |
| CxJsonValue * cx_json_obj_put_arr | ( | CxJsonValue * | obj, |
| cxstring | name ) |
Creates a new JSON array and adds it to an object.
Internal function - use cxJsonObjPutArr().
| obj | the target JSON object |
| name | the name of the new value |
NULL if allocation fails | CxJsonValue * cx_json_obj_put_integer | ( | CxJsonValue * | obj, |
| cxstring | name, | ||
| int64_t | num ) |
Creates a new JSON number, based on an integer, and adds it to an object.
Internal function - use cxJsonObjPutInteger().
| obj | the target JSON object |
| name | the name of the new value |
| num | the numeric value |
NULL if allocation fails | CxJsonValue * cx_json_obj_put_literal | ( | CxJsonValue * | obj, |
| cxstring | name, | ||
| CxJsonLiteral | lit ) |
Creates a new JSON literal and adds it to an object.
Internal function - use cxJsonObjPutLiteral().
| obj | the target JSON object |
| name | the name of the new value |
| lit | the type of literal |
NULL if allocation fails | CxJsonValue * cx_json_obj_put_number | ( | CxJsonValue * | obj, |
| cxstring | name, | ||
| double | num ) |
Creates a new JSON number and adds it to an object.
Internal function - use cxJsonObjPutNumber().
| obj | the target JSON object |
| name | the name of the new value |
| num | the numeric value |
NULL if allocation fails | CxJsonValue * cx_json_obj_put_obj | ( | CxJsonValue * | obj, |
| cxstring | name ) |
Creates a new JSON object and adds it to an existing object.
Internal function - use cxJsonObjPutObj().
| obj | the target JSON object |
| name | the name of the new value |
NULL if allocation fails | CxJsonValue * cx_json_obj_put_string | ( | CxJsonValue * | obj, |
| cxstring | name, | ||
| cxstring | str ) |
Creates a new JSON string and adds it to an object.
Internal function - use cxJsonObjPutString()
| obj | the target JSON object |
| name | the name of the new value |
| str | the string data |
NULL if allocation fails | CxJsonValue * cx_json_obj_remove | ( | CxJsonValue * | value, |
| cxstring | name ) |
Internal function, do not use.
| value | the JSON object |
| name | the key to look up |
NULL when the key is not part of the object | int cxJsonArrAddCxStrings | ( | CxJsonValue * | arr, |
| const cxstring * | str, | ||
| size_t | count ) |
Adds strings to a JSON array.
The strings will be copied with the allocator of the array.
| arr | the JSON array |
| str | the array of strings |
| count | the number of elements |
| zero | success |
| non-zero | allocation failure |
| int cxJsonArrAddIntegers | ( | CxJsonValue * | arr, |
| const int64_t * | num, | ||
| size_t | count ) |
Adds number values, of which all are integers, to a JSON array.
| arr | the JSON array |
| num | the array of values |
| count | the number of elements |
| zero | success |
| non-zero | allocation failure |
| int cxJsonArrAddLiterals | ( | CxJsonValue * | arr, |
| const CxJsonLiteral * | lit, | ||
| size_t | count ) |
Adds literals to a JSON array.
| arr | the JSON array |
| lit | the array of literal types |
| count | the number of elements |
| zero | success |
| non-zero | allocation failure |
| int cxJsonArrAddNumbers | ( | CxJsonValue * | arr, |
| const double * | num, | ||
| size_t | count ) |
Adds number values to a JSON array.
| arr | the JSON array |
| num | the array of values |
| count | the number of elements |
| zero | success |
| non-zero | allocation failure |
| int cxJsonArrAddStrings | ( | CxJsonValue * | arr, |
| const char *const * | str, | ||
| size_t | count ) |
Adds strings to a JSON array.
The strings will be copied with the allocator of the array.
| arr | the JSON array |
| str | the array of strings |
| count | the number of elements |
| zero | success |
| non-zero | allocation failure |
| int cxJsonArrAddValues | ( | CxJsonValue * | arr, |
| CxJsonValue *const * | val, | ||
| size_t | count ) |
Add arbitrary values to a JSON array.
| arr | the JSON array |
| val | the values |
| count | the number of elements |
| zero | success |
| non-zero | allocation failure |
| CxJsonValue * cxJsonArrGet | ( | const CxJsonValue * | value, |
| size_t | index ) |
Returns an element from a JSON array.
If the value is not a JSON array, the behavior is undefined.
This function guarantees to return a value. If the index is out of bounds, the returned value will be of type CX_JSON_NOTHING, but never NULL.
| value | the JSON value |
| index | the index in the array |
| CxIterator cxJsonArrIter | ( | const CxJsonValue * | value | ) |
Returns an iterator over the JSON array elements.
The iterator yields values of type CxJsonValue* .
If the value is not a JSON array, the behavior is undefined.
| value | the JSON value |
| CxJsonValue * cxJsonArrRemove | ( | CxJsonValue * | value, |
| size_t | index ) |
Removes an element from a JSON array.
If the value is not a JSON array, the behavior is undefined.
This function, in contrast to cxJsonArrayGet(), returns NULL when the index is out of bounds.
| value | the JSON value |
| index | the index in the array |
NULL when the index was out of bounds
|
inlinestatic |
Returns the size of a JSON array.
If the value is not a JSON array, the behavior is undefined.
| value | the JSON value |
|
inlinestatic |
Obtains a Boolean value from the given JSON value.
If the value is not a JSON literal, the behavior is undefined. The null literal is interpreted as false.
| value | the JSON value |
| cxmutstr cxJsonAsCxMutStr | ( | const CxJsonValue * | value | ) |
Obtains a mutable UCX string from the given JSON value.
If the value is not a string, the behavior is undefined.
| value | the JSON value |
| cxstring cxJsonAsCxString | ( | const CxJsonValue * | value | ) |
Obtains a UCX string from the given JSON value.
If the value is not a string, the behavior is undefined.
| value | the JSON value |
| double cxJsonAsDouble | ( | const CxJsonValue * | value | ) |
Obtains a double-precision floating-point value from the given JSON value.
If the value is not a JSON number, the behavior is undefined.
| value | the JSON value |
| int64_t cxJsonAsInteger | ( | const CxJsonValue * | value | ) |
Obtains a 64-bit signed integer from the given JSON value.
If the value is not a JSON number, the behavior is undefined. If it is a JSON number, but not an integer, the value will be converted to an integer, possibly losing precision.
| value | the JSON value |
| char * cxJsonAsString | ( | const CxJsonValue * | value | ) |
Obtains a C string from the given JSON value.
If the value is not a string, the behavior is undefined.
| value | the JSON value |
| CxJsonValue * cxJsonCreateArr | ( | const CxAllocator * | allocator | ) |
Creates a new (empty) JSON array.
| allocator | the allocator to use |
NULL if allocation fails | CxJsonValue * cxJsonCreateInteger | ( | const CxAllocator * | allocator, |
| int64_t | num ) |
Creates a new JSON number value based on an integer.
| allocator | the allocator to use |
| num | the numeric value |
NULL if allocation fails | CxJsonValue * cxJsonCreateLiteral | ( | const CxAllocator * | allocator, |
| CxJsonLiteral | lit ) |
Creates a new JSON literal.
| allocator | the allocator to use |
| lit | the type of literal |
NULL if allocation fails | CxJsonValue * cxJsonCreateNumber | ( | const CxAllocator * | allocator, |
| double | num ) |
Creates a new JSON number value.
| allocator | the allocator to use |
| num | the numeric value |
NULL if allocation fails | CxJsonValue * cxJsonCreateObj | ( | const CxAllocator * | allocator | ) |
Creates a new (empty) JSON object.
| allocator | the allocator to use |
NULL if allocation fails | void cxJsonDestroy | ( | CxJson * | json | ) |
| int cxJsonFilln | ( | CxJson * | json, |
| const char * | buf, | ||
| size_t | len ) |
Fills the input buffer.
| json | the JSON interface |
| buf | the source buffer |
| len | the length of the source buffer |
| zero | success |
| non-zero | internal allocation error |
| void cxJsonInit | ( | CxJson * | json, |
| const CxAllocator * | allocator ) |
Initializes the JSON interface.
| json | the JSON interface |
| allocator | the allocator that shall be used for the produced values |
|
inlinestatic |
Checks if the specified value is a JSON array.
| value | a pointer to the value |
| true | the value is a JSON array |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is a Boolean literal.
| value | a pointer to the value |
| true | the value is either true or false |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is false.
!cxJsonIsTrue(v).| value | a pointer to the value |
| true | the value is false |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is an integer number.
| value | a pointer to the value |
| true | the value is an integer number |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is a JSON literal.
JSON literals are true, false, and null.
| value | a pointer to the value |
| true | the value is a JSON literal |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is null.
| value | a pointer to the value |
| true | the value is null |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is a JSON number.
This function will return true for both floating-point and integer numbers.
| value | a pointer to the value |
| true | the value is a JSON number |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is a JSON object.
| value | a pointer to the value |
| true | the value is a JSON object |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is a string.
| value | a pointer to the value |
| true | the value is a string |
| false | otherwise |
|
inlinestatic |
Checks if the specified value is true.
!cxJsonIsFalse(v).| value | a pointer to the value |
| true | the value is true |
| false | otherwise |
| CxJsonStatus cxJsonNext | ( | CxJson * | json, |
| CxJsonValue ** | value ) |
Tries to obtain the next JSON value.
Before this function can be called, the input buffer needs to be filled with cxJsonFill().
When this function returns CX_JSON_INCOMPLETE_DATA, you can add the missing data with another invocation of cxJsonFill() and then repeat the call to cxJsonNext().
| json | the JSON interface |
| value | a pointer where the next value shall be stored |
| CX_JSON_NO_ERROR | successfully retrieve the value |
| CX_JSON_NO_DATA | there is no (more) data in the buffer to read from |
| CX_JSON_INCOMPLETE_DATA | an incomplete value was read and more data needs to be filled |
| CX_JSON_NULL_DATA | the buffer was never initialized |
| CX_JSON_BUFFER_ALLOC_FAILED | allocating internal buffer space failed |
| CX_JSON_VALUE_ALLOC_FAILED | allocating memory for a CxJsonValue failed |
| CX_JSON_FORMAT_ERROR_NUMBER | the JSON text contains an illegally formatted number |
| CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN | JSON syntax error |
| CxIterator cxJsonObjIter | ( | const CxJsonValue * | value | ) |
Returns an iterator over the JSON object members.
The iterator yields values of type CxJsonObjValue* which contain the name and value of the member.
If the value is not a JSON object, the behavior is undefined.
| value | the JSON value |
|
inlinestatic |
Returns the size of a JSON object.
If the value is not a JSON object, the behavior is undefined.
| value | the JSON value |
| void cxJsonReset | ( | CxJson * | json | ) |
Destroys and re-initializes the JSON interface.
You might want to use this to reset the parser after encountering a syntax error.
| json | the JSON interface |
| void cxJsonValueFree | ( | CxJsonValue * | value | ) |
Recursively deallocates the memory of a JSON value.
| value | the value |
| int cxJsonWrite | ( | void * | target, |
| const CxJsonValue * | value, | ||
| cx_write_func | wfunc, | ||
| const CxJsonWriter * | settings ) |
Writes a JSON value to a buffer or stream.
This function blocks until either all data is written, or an error occurs. The write operation is not atomic in the sense that it might happen that the data is only partially written when an error occurs with no way to indicate how much data was written. To avoid this problem, you can use a CxBuffer as target which is unlikely to fail a write operation. You can, for example, use the buffer's flush feature to relay the data.
| target | the buffer or stream where to write to |
| value | the value that shall be written |
| wfunc | the write function to use |
| settings | formatting settings (or NULL to use a compact default) |
| zero | success |
| non-zero | when no or not all data could be written |
| CxJsonWriter cxJsonWriterCompact | ( | void | ) |
Creates a default writer configuration for compact output.
| CxJsonWriter cxJsonWriterPretty | ( | bool | use_spaces | ) |
Creates a default writer configuration for pretty output.
| use_spaces | false if you want tabs, true if you want four spaces instead |