ucx
UAP Common Extensions
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Enumerations | Functions
json.h File Reference

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.
 
CxJsonValuecxJsonCreateObj (const CxAllocator *allocator)
 Creates a new (empty) JSON object.
 
CxJsonValuecxJsonCreateArr (const CxAllocator *allocator)
 Creates a new (empty) JSON array.
 
CxJsonValuecxJsonCreateNumber (const CxAllocator *allocator, double num)
 Creates a new JSON number value.
 
CxJsonValuecxJsonCreateInteger (const CxAllocator *allocator, int64_t num)
 Creates a new JSON number value based on an integer.
 
CxJsonValuecxJsonCreateString (const CxAllocator *allocator, const char *str)
 Creates a new JSON string.
 
CxJsonValuecxJsonCreateCxString (const CxAllocator *allocator, cxstring str)
 Creates a new JSON string.
 
CxJsonValuecxJsonCreateLiteral (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.
 
CxJsonValuecxJsonObjPutObj (CxJsonValue *obj, cxstring name)
 Creates a new JSON object and adds it to an existing object.
 
CxJsonValuecxJsonObjPutArr (CxJsonValue *obj, cxstring name)
 Creates a new JSON array and adds it to an object.
 
CxJsonValuecxJsonObjPutNumber (CxJsonValue *obj, cxstring name, double num)
 Creates a new JSON number and adds it to an object.
 
CxJsonValuecxJsonObjPutInteger (CxJsonValue *obj, cxstring name, int64_t num)
 Creates a new JSON number, based on an integer, and adds it to an object.
 
CxJsonValuecxJsonObjPutString (CxJsonValue *obj, cxstring name, const char *str)
 Creates a new JSON string and adds it to an object.
 
CxJsonValuecxJsonObjPutCxString (CxJsonValue *obj, cxstring name, cxstring str)
 Creates a new JSON string and adds it to an object.
 
CxJsonValuecxJsonObjPutLiteral (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.
 
CxJsonValuecxJsonArrGet (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.
 
CxJsonValuecx_json_obj_get_cxstr (const CxJsonValue *value, cxstring name)
 Returns a value corresponding to a key in a JSON object.
 
static CxJsonValuecx_json_obj_get_mutstr (const CxJsonValue *value, cxmutstr name)
 Returns a value corresponding to a key in a JSON object.
 
static CxJsonValuecx_json_obj_get_str (const CxJsonValue *value, const char *name)
 Returns a value corresponding to a key in a JSON object.
 

Detailed Description

Interface for parsing data from JSON files.

Author
Mike Becker
Olaf Wintermann

Macro Definition Documentation

◆ cxJsonFill

#define cxJsonFill ( json,
str )
Value:
_Generic((str), \
char*: cx_json_fill_str, \
const char*: cx_json_fill_str) \
(json, str)
static int cx_json_fill_cxstr(CxJson *json, cxstring str)
Fills the input buffer.
Definition json.h:636
static int cx_json_fill_str(CxJson *json, const char *str)
Fills the input buffer.
Definition json.h:659
static int cx_json_fill_mutstr(CxJson *json, cxmutstr str)
Fills the input buffer.
Definition json.h:647
The UCX string structure.
Definition string.h:51
The UCX string structure for immutable (constant) strings.
Definition string.h:69

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.

Parameters
jsonthe json interface
strthe source string
Return values
zerosuccess
non-zerointernal allocation error
See also
cxJsonFilln()

◆ cxJsonObjGet

#define cxJsonObjGet ( value,
name )
Value:
_Generic((name), \
const char*: cx_json_obj_get_str) \
(value, name)
static CxJsonValue * cx_json_obj_get_str(const CxJsonValue *value, const char *name)
Returns a value corresponding to a key in a JSON object.
Definition json.h:1380
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.
Definition json.h:1370

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.

Parameters
valuethe JSON object
namethe key to look up
Returns
the value corresponding to the key
See also
cxJsonIsObject()

Enumeration Type Documentation

◆ cx_json_literal

JSON literal types.

Enumerator
CX_JSON_NULL 

The null literal.

CX_JSON_TRUE 

The true literal.

CX_JSON_FALSE 

The false literal.

◆ 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 CX_JSON_OK.

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.

◆ cx_json_token_type

The type of the parsed token.

Enumerator
CX_JSON_NO_TOKEN 

No complete token parsed, yet.

CX_JSON_TOKEN_ERROR 

The presumed token contains syntactical errors.

CX_JSON_TOKEN_BEGIN_ARRAY 

A "begin of array" '[' token.

CX_JSON_TOKEN_BEGIN_OBJECT 

A "begin of object" '{' token.

CX_JSON_TOKEN_END_ARRAY 

An "end of array" ']' token.

CX_JSON_TOKEN_END_OBJECT 

An "end of object" '}' token.

CX_JSON_TOKEN_NAME_SEPARATOR 

A colon ':' token separating names and values.

CX_JSON_TOKEN_VALUE_SEPARATOR 

A comma ',' token separating object entries or array elements.

CX_JSON_TOKEN_STRING 

A string token.

CX_JSON_TOKEN_INTEGER 

A number token that can be represented as integer.

CX_JSON_TOKEN_NUMBER 

A number token that cannot be represented as integer.

CX_JSON_TOKEN_LITERAL 

A literal token.

CX_JSON_TOKEN_SPACE 

A white-space token.

◆ cx_json_value_type

The type of some JSON value.

Enumerator
CX_JSON_NOTHING 

Reserved.

CX_JSON_OBJECT 

A JSON object.

CX_JSON_ARRAY 

A JSON array.

CX_JSON_STRING 

A string.

CX_JSON_INTEGER 

A number that contains an integer.

CX_JSON_NUMBER 

A number, not necessarily an integer.

CX_JSON_LITERAL 

A literal (true, false, null).

Function Documentation

◆ cx_json_fill_cxstr()

static int cx_json_fill_cxstr ( CxJson * json,
cxstring str )
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.

Parameters
jsonthe json interface
strthe source string
Return values
zerosuccess
non-zerointernal allocation error
See also
cxJsonFilln()

◆ cx_json_fill_mutstr()

static int cx_json_fill_mutstr ( CxJson * json,
cxmutstr str )
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.

Parameters
jsonthe json interface
strthe source string
Return values
zerosuccess
non-zerointernal allocation error
See also
cxJsonFilln()

◆ cx_json_fill_str()

static int cx_json_fill_str ( CxJson * json,
const char * str )
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.

Parameters
jsonthe json interface
strthe source string
Return values
zerosuccess
non-zerointernal allocation error
See also
cxJsonFilln()

◆ cx_json_obj_get_cxstr()

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.

Parameters
valuethe JSON object
namethe key to look up
Returns
the value corresponding to the key
See also
cxJsonIsObject()

◆ cx_json_obj_get_mutstr()

static CxJsonValue * cx_json_obj_get_mutstr ( const CxJsonValue * value,
cxmutstr name )
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.

Parameters
valuethe JSON object
namethe key to look up
Returns
the value corresponding to the key
See also
cxJsonIsObject()

◆ cx_json_obj_get_str()

static CxJsonValue * cx_json_obj_get_str ( const CxJsonValue * value,
const char * name )
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.

Parameters
valuethe JSON object
namethe key to look up
Returns
the value corresponding to the key
See also
cxJsonIsObject()

◆ cxJsonArrAddCxStrings()

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.

Parameters
arrthe JSON array
strthe array of strings
countthe number of elements
Return values
zerosuccess
non-zeroallocation failure
See also
cxJsonArrAddStrings()

◆ cxJsonArrAddIntegers()

int cxJsonArrAddIntegers ( CxJsonValue * arr,
const int64_t * num,
size_t count )

Adds number values, of which all are integers, to a JSON array.

Parameters
arrthe JSON array
numthe array of values
countthe number of elements
Return values
zerosuccess
non-zeroallocation failure

◆ cxJsonArrAddLiterals()

int cxJsonArrAddLiterals ( CxJsonValue * arr,
const CxJsonLiteral * lit,
size_t count )

Adds literals to a JSON array.

Parameters
arrthe JSON array
litthe array of literal types
countthe number of elements
Return values
zerosuccess
non-zeroallocation failure

◆ cxJsonArrAddNumbers()

int cxJsonArrAddNumbers ( CxJsonValue * arr,
const double * num,
size_t count )

Adds number values to a JSON array.

Parameters
arrthe JSON array
numthe array of values
countthe number of elements
Return values
zerosuccess
non-zeroallocation failure

◆ cxJsonArrAddStrings()

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.

Parameters
arrthe JSON array
strthe array of strings
countthe number of elements
Return values
zerosuccess
non-zeroallocation failure
See also
cxJsonArrAddCxStrings()

◆ cxJsonArrAddValues()

int cxJsonArrAddValues ( CxJsonValue * arr,
CxJsonValue *const * val,
size_t count )

Add arbitrary values to a JSON array.

Attention
In contrast to all other add functions, this function adds the values directly to the array instead of copying them.
Parameters
arrthe JSON array
valthe values
countthe number of elements
Return values
zerosuccess
non-zeroallocation failure

◆ cxJsonArrGet()

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.

Parameters
valuethe JSON value
indexthe index in the array
Returns
the value at the specified index
See also
cxJsonIsArray()

◆ cxJsonArrIter()

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.

Parameters
valuethe JSON value
Returns
an iterator over the array elements
See also
cxJsonIsArray()

◆ cxJsonArrSize()

static size_t cxJsonArrSize ( const CxJsonValue * value)
inlinestatic

Returns the size of a JSON array.

If the value is not a JSON array, the behavior is undefined.

Parameters
valuethe JSON value
Returns
the size of the array
See also
cxJsonIsArray()

◆ cxJsonAsBool()

static bool cxJsonAsBool ( const CxJsonValue * 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.

Parameters
valuethe JSON value
Returns
the value represented as double
See also
cxJsonIsLiteral()

◆ cxJsonAsCxMutStr()

static cxmutstr cxJsonAsCxMutStr ( const CxJsonValue * value)
inlinestatic

Obtains a mutable UCX string from the given JSON value.

If the value is not a string, the behavior is undefined.

Parameters
valuethe JSON value
Returns
the value represented as mutable UCX string
See also
cxJsonIsString()

◆ cxJsonAsCxString()

static cxstring cxJsonAsCxString ( const CxJsonValue * value)
inlinestatic

Obtains a UCX string from the given JSON value.

If the value is not a string, the behavior is undefined.

Parameters
valuethe JSON value
Returns
the value represented as UCX string
See also
cxJsonIsString()

◆ cxJsonAsDouble()

static double cxJsonAsDouble ( const CxJsonValue * 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.

Parameters
valuethe JSON value
Returns
the value represented as double
See also
cxJsonIsNumber()

◆ cxJsonAsInteger()

static int64_t cxJsonAsInteger ( const CxJsonValue * 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.

Parameters
valuethe JSON value
Returns
the value represented as double
See also
cxJsonIsNumber()
cxJsonIsInteger()

◆ cxJsonAsString()

static char * cxJsonAsString ( const CxJsonValue * value)
inlinestatic

Obtains a C string from the given JSON value.

If the value is not a string, the behavior is undefined.

Parameters
valuethe JSON value
Returns
the value represented as C string
See also
cxJsonIsString()

◆ cxJsonCreateArr()

CxJsonValue * cxJsonCreateArr ( const CxAllocator * allocator)

Creates a new (empty) JSON array.

Parameters
allocatorthe allocator to use
Returns
the new JSON array or NULL if allocation fails
See also
cxJsonObjPutArr()
cxJsonArrAddValues()

◆ cxJsonCreateCxString()

CxJsonValue * cxJsonCreateCxString ( const CxAllocator * allocator,
cxstring str )

Creates a new JSON string.

Parameters
allocatorthe allocator to use
strthe string data
Returns
the new JSON value or NULL if allocation fails
See also
cxJsonCreateCxString()
cxJsonObjPutCxString()
cxJsonArrAddCxStrings()

◆ cxJsonCreateInteger()

CxJsonValue * cxJsonCreateInteger ( const CxAllocator * allocator,
int64_t num )

Creates a new JSON number value based on an integer.

Parameters
allocatorthe allocator to use
numthe numeric value
Returns
the new JSON value or NULL if allocation fails
See also
cxJsonObjPutInteger()
cxJsonArrAddIntegers()

◆ cxJsonCreateLiteral()

CxJsonValue * cxJsonCreateLiteral ( const CxAllocator * allocator,
CxJsonLiteral lit )

Creates a new JSON literal.

Parameters
allocatorthe allocator to use
litthe type of literal
Returns
the new JSON value or NULL if allocation fails
See also
cxJsonObjPutLiteral()
cxJsonArrAddLiterals()

◆ cxJsonCreateNumber()

CxJsonValue * cxJsonCreateNumber ( const CxAllocator * allocator,
double num )

Creates a new JSON number value.

Parameters
allocatorthe allocator to use
numthe numeric value
Returns
the new JSON value or NULL if allocation fails
See also
cxJsonObjPutNumber()
cxJsonArrAddNumbers()

◆ cxJsonCreateObj()

CxJsonValue * cxJsonCreateObj ( const CxAllocator * allocator)

Creates a new (empty) JSON object.

Parameters
allocatorthe allocator to use
Returns
the new JSON object or NULL if allocation fails
See also
cxJsonObjPutObj()
cxJsonArrAddValues()

◆ cxJsonCreateString()

CxJsonValue * cxJsonCreateString ( const CxAllocator * allocator,
const char * str )

Creates a new JSON string.

Parameters
allocatorthe allocator to use
strthe string data
Returns
the new JSON value or NULL if allocation fails
See also
cxJsonCreateString()
cxJsonObjPutString()
cxJsonArrAddStrings()

◆ cxJsonDestroy()

void cxJsonDestroy ( CxJson * json)

Destroys the json interface.

Parameters
jsonthe json interface
See also
cxJsonInit()

◆ cxJsonFilln()

int cxJsonFilln ( CxJson * json,
const char * buf,
size_t len )

Fills the input buffer.

Remarks
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.
Parameters
jsonthe json interface
bufthe source buffer
lenthe length of the source buffer
Return values
zerosuccess
non-zerointernal allocation error
See also
cxJsonFill()

◆ cxJsonInit()

void cxJsonInit ( CxJson * json,
const CxAllocator * allocator )

Initializes the json interface.

Parameters
jsonthe json interface
allocatorthe allocator that shall be used for the produced values
See also
cxJsonDestroy()

◆ cxJsonIsArray()

static bool cxJsonIsArray ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is a JSON array.

Parameters
valuea pointer to the value
Return values
truethe value is a JSON array
falseotherwise

◆ cxJsonIsBool()

static bool cxJsonIsBool ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is a Boolean literal.

Parameters
valuea pointer to the value
Return values
truethe value is either true or false
falseotherwise
See also
cxJsonIsTrue()
cxJsonIsFalse()

◆ cxJsonIsFalse()

static bool cxJsonIsFalse ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is false.

Remarks
Be advised, that this is not the same as testing !cxJsonIsTrue(v).
Parameters
valuea pointer to the value
Return values
truethe value is false
falseotherwise
See also
cxJsonIsBool()
cxJsonIsTrue()

◆ cxJsonIsInteger()

static bool cxJsonIsInteger ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is an integer number.

Parameters
valuea pointer to the value
Return values
truethe value is an integer number
falseotherwise
See also
cxJsonIsNumber()

◆ cxJsonIsLiteral()

static bool cxJsonIsLiteral ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is a JSON literal.

JSON literals are true, false, and null.

Parameters
valuea pointer to the value
Return values
truethe value is a JSON literal
falseotherwise
See also
cxJsonIsTrue()
cxJsonIsFalse()
cxJsonIsNull()

◆ cxJsonIsNull()

static bool cxJsonIsNull ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is null.

Parameters
valuea pointer to the value
Return values
truethe value is null
falseotherwise
See also
cxJsonIsLiteral()

◆ cxJsonIsNumber()

static bool cxJsonIsNumber ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is a JSON number.

This function will return true for both floating point and integer numbers.

Parameters
valuea pointer to the value
Return values
truethe value is a JSON number
falseotherwise
See also
cxJsonIsInteger()

◆ cxJsonIsObject()

static bool cxJsonIsObject ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is a JSON object.

Parameters
valuea pointer to the value
Return values
truethe value is a JSON object
falseotherwise

◆ cxJsonIsString()

static bool cxJsonIsString ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is a string.

Parameters
valuea pointer to the value
Return values
truethe value is a string
falseotherwise

◆ cxJsonIsTrue()

static bool cxJsonIsTrue ( const CxJsonValue * value)
inlinestatic

Checks if the specified value is true.

Remarks
Be advised, that this is not the same as testing !cxJsonIsFalse(v).
Parameters
valuea pointer to the value
Return values
truethe value is true
falseotherwise
See also
cxJsonIsBool()
cxJsonIsFalse()

◆ cxJsonNext()

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().

Parameters
jsonthe json interface
valuea pointer where the next value shall be stored
Return values
CX_JSON_NO_ERRORsuccessfully retrieve the value
CX_JSON_NO_DATAthere is no (more) data in the buffer to read from
CX_JSON_INCOMPLETE_DATAan incomplete value was read and more data needs to be filled
CX_JSON_NULL_DATAthe buffer was never initialized
CX_JSON_BUFFER_ALLOC_FAILEDallocating internal buffer space failed
CX_JSON_VALUE_ALLOC_FAILEDallocating memory for a CxJsonValue failed
CX_JSON_FORMAT_ERROR_NUMBERthe JSON text contains an illegally formatted number
CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKENJSON syntax error

◆ cxJsonObjIter()

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.

Parameters
valuethe JSON value
Returns
an iterator over the object members
See also
cxJsonIsObject()

◆ cxJsonObjPut()

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.

Note
If a value with the specified name already exists, it will be (recursively) freed with its own allocator.
Parameters
objthe JSON object
namethe name of the value
childthe value
Return values
zerosuccess
non-zeroallocation failure

◆ cxJsonObjPutArr()

CxJsonValue * cxJsonObjPutArr ( CxJsonValue * obj,
cxstring name )

Creates a new JSON array and adds it to an object.

Parameters
objthe target JSON object
namethe name of the new value
Returns
the new value or NULL if allocation fails
See also
cxJsonObjPut()
cxJsonCreateArr()

◆ cxJsonObjPutCxString()

CxJsonValue * cxJsonObjPutCxString ( CxJsonValue * obj,
cxstring name,
cxstring str )

Creates a new JSON string and adds it to an object.

The string data is copied.

Parameters
objthe target JSON object
namethe name of the new value
strthe string data
Returns
the new value or NULL if allocation fails
See also
cxJsonObjPut()
cxJsonCreateCxString()

◆ cxJsonObjPutInteger()

CxJsonValue * cxJsonObjPutInteger ( CxJsonValue * obj,
cxstring name,
int64_t num )

Creates a new JSON number, based on an integer, and adds it to an object.

Parameters
objthe target JSON object
namethe name of the new value
numthe numeric value
Returns
the new value or NULL if allocation fails
See also
cxJsonObjPut()
cxJsonCreateInteger()

◆ cxJsonObjPutLiteral()

CxJsonValue * cxJsonObjPutLiteral ( CxJsonValue * obj,
cxstring name,
CxJsonLiteral lit )

Creates a new JSON literal and adds it to an object.

Parameters
objthe target JSON object
namethe name of the new value
litthe type of literal
Returns
the new value or NULL if allocation fails
See also
cxJsonObjPut()
cxJsonCreateLiteral()

◆ cxJsonObjPutNumber()

CxJsonValue * cxJsonObjPutNumber ( CxJsonValue * obj,
cxstring name,
double num )

Creates a new JSON number and adds it to an object.

Parameters
objthe target JSON object
namethe name of the new value
numthe numeric value
Returns
the new value or NULL if allocation fails
See also
cxJsonObjPut()
cxJsonCreateNumber()

◆ cxJsonObjPutObj()

CxJsonValue * cxJsonObjPutObj ( CxJsonValue * obj,
cxstring name )

Creates a new JSON object and adds it to an existing object.

Parameters
objthe target JSON object
namethe name of the new value
Returns
the new value or NULL if allocation fails
See also
cxJsonObjPut()
cxJsonCreateObj()

◆ cxJsonObjPutString()

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.

Parameters
objthe target JSON object
namethe name of the new value
strthe string data
Returns
the new value or NULL if allocation fails
See also
cxJsonObjPut()
cxJsonCreateString()

◆ cxJsonReset()

static void cxJsonReset ( CxJson * json)
inlinestatic

Destroys and re-initializes the json interface.

You might want to use this, to reset the parser after encountering a syntax error.

Parameters
jsonthe json interface

◆ cxJsonValueFree()

void cxJsonValueFree ( CxJsonValue * value)

Recursively deallocates the memory of a JSON value.

Remarks
The type of each deallocated value will be changed to CX_JSON_NOTHING and values of such type will be skipped by the de-allocation. That means, this function protects you from double-frees when you are accidentally freeing a nested value and then the parent value (or vice versa).
Parameters
valuethe value

◆ cxJsonWrite()

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.

Parameters
targetthe buffer or stream where to write to
valuethe value that shall be written
wfuncthe write function to use
settingsformatting settings (or NULL to use a compact default)
Return values
zerosuccess
non-zerowhen no or not all data could be written

◆ cxJsonWriterCompact()

CxJsonWriter cxJsonWriterCompact ( void )

Creates a default writer configuration for compact output.

Returns
new JSON writer settings

◆ cxJsonWriterPretty()

CxJsonWriter cxJsonWriterPretty ( bool use_spaces)

Creates a default writer configuration for pretty output.

Parameters
use_spacesfalse if you want tabs, true if you want four spaces instead
Returns
new JSON writer settings