![]() |
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 | cxJsonObjGet(value, name) |
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 64-bit signed integer. | |
typedef double | CxJsonNumber |
Type alias for 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. | |
static 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_cxstr (CxJson *json, cxstring str) |
Fills the input buffer. | |
static int | cx_json_fill_mutstr (CxJson *json, cxmutstr str) |
Fills the input buffer. | |
static int | cx_json_fill_str (CxJson *json, const char *str) |
Fills the input buffer. | |
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 * | cxJsonCreateString (const CxAllocator *allocator, const char *str) |
Creates a new JSON string. | |
CxJsonValue * | cxJsonCreateCxString (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 | cxJsonObjPut (CxJsonValue *obj, cxstring name, CxJsonValue *child) |
Adds or replaces a value within a JSON object. | |
CxJsonValue * | cxJsonObjPutObj (CxJsonValue *obj, cxstring name) |
Creates a new JSON object and adds it to an existing object. | |
CxJsonValue * | cxJsonObjPutArr (CxJsonValue *obj, cxstring name) |
Creates a new JSON array and adds it to an object. | |
CxJsonValue * | cxJsonObjPutNumber (CxJsonValue *obj, cxstring name, double num) |
Creates a new JSON number and adds it to an object. | |
CxJsonValue * | cxJsonObjPutInteger (CxJsonValue *obj, cxstring name, int64_t num) |
Creates a new JSON number, based on an integer, and adds it to an object. | |
CxJsonValue * | cxJsonObjPutString (CxJsonValue *obj, cxstring name, const char *str) |
Creates a new JSON string and adds it to an object. | |
CxJsonValue * | cxJsonObjPutCxString (CxJsonValue *obj, cxstring name, cxstring str) |
Creates a new JSON string and adds it to an object. | |
CxJsonValue * | cxJsonObjPutLiteral (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 . | |
static char * | cxJsonAsString (const CxJsonValue *value) |
Obtains a C string from the given JSON value. | |
static cxstring | cxJsonAsCxString (const CxJsonValue *value) |
Obtains a UCX string from the given JSON value. | |
static cxmutstr | cxJsonAsCxMutStr (const CxJsonValue *value) |
Obtains a mutable UCX string from the given JSON value. | |
static double | cxJsonAsDouble (const CxJsonValue *value) |
Obtains a double-precision floating point value from the given JSON value. | |
static 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. | |
CxIterator | cxJsonArrIter (const CxJsonValue *value) |
Returns an iterator over the JSON array elements. | |
CxIterator | cxJsonObjIter (const CxJsonValue *value) |
Returns an iterator over the JSON object members. | |
CxJsonValue * | cx_json_obj_get_cxstr (const CxJsonValue *value, cxstring name) |
Returns a value corresponding to a key in a JSON object. | |
static CxJsonValue * | cx_json_obj_get_mutstr (const CxJsonValue *value, cxmutstr name) |
Returns a value corresponding to a key in a JSON object. | |
static CxJsonValue * | cx_json_obj_get_str (const CxJsonValue *value, const char *name) |
Returns a value corresponding to a key in a JSON object. | |
Interface for parsing data from JSON files.
#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 |
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 |
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 |
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 |
|
inlinestatic |
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 |
CxJsonValue * cx_json_obj_get_cxstr | ( | const CxJsonValue * | value, |
cxstring | 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 |
|
inlinestatic |
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 |
|
inlinestatic |
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 |
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 |
|
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 |
|
inlinestatic |
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 |
|
inlinestatic |
Obtains a UCX string from the given JSON value.
If the value
is not a string, the behavior is undefined.
value | the JSON value |
|
inlinestatic |
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 |
|
inlinestatic |
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 |
|
inlinestatic |
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 * cxJsonCreateCxString | ( | const CxAllocator * | allocator, |
cxstring | str ) |
Creates a new JSON string.
allocator | the allocator to use |
str | the string data |
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 CxJsonValue * cxJsonCreateString | ( | const CxAllocator * | allocator, |
const char * | str ) |
Creates a new JSON string.
allocator | the allocator to use |
str | the string data |
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 |
int cxJsonObjPut | ( | CxJsonValue * | obj, |
cxstring | name, | ||
CxJsonValue * | 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 | the JSON object |
name | the name of the value |
child | the value |
zero | success |
non-zero | allocation failure |
CxJsonValue * cxJsonObjPutArr | ( | CxJsonValue * | obj, |
cxstring | name ) |
Creates a new JSON array and adds it to an object.
obj | the target JSON object |
name | the name of the new value |
NULL
if allocation fails CxJsonValue * cxJsonObjPutCxString | ( | CxJsonValue * | obj, |
cxstring | name, | ||
cxstring | str ) |
Creates a new JSON string and adds it to an object.
The string data is copied.
obj | the target JSON object |
name | the name of the new value |
str | the string data |
NULL
if allocation fails CxJsonValue * cxJsonObjPutInteger | ( | CxJsonValue * | obj, |
cxstring | name, | ||
int64_t | num ) |
Creates a new JSON number, based on an integer, and adds it to an object.
obj | the target JSON object |
name | the name of the new value |
num | the numeric value |
NULL
if allocation fails CxJsonValue * cxJsonObjPutLiteral | ( | CxJsonValue * | obj, |
cxstring | name, | ||
CxJsonLiteral | lit ) |
Creates a new JSON literal and adds it to an object.
obj | the target JSON object |
name | the name of the new value |
lit | the type of literal |
NULL
if allocation fails CxJsonValue * cxJsonObjPutNumber | ( | CxJsonValue * | obj, |
cxstring | name, | ||
double | num ) |
Creates a new JSON number and adds it to an object.
obj | the target JSON object |
name | the name of the new value |
num | the numeric value |
NULL
if allocation fails CxJsonValue * cxJsonObjPutObj | ( | CxJsonValue * | obj, |
cxstring | name ) |
Creates a new JSON object and adds it to an existing object.
obj | the target JSON object |
name | the name of the new value |
NULL
if allocation fails CxJsonValue * cxJsonObjPutString | ( | CxJsonValue * | obj, |
cxstring | name, | ||
const char * | str ) |
Creates a new JSON string and adds it to an object.
The string data is copied.
obj | the target JSON object |
name | the name of the new value |
str | the string data |
NULL
if allocation fails
|
inlinestatic |
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 and either use the buffer's flush feature to relay the data or use the data in the buffer manually to write it to the actual target.
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 |