625#define cxJsonFill(json, str) _Generic((str), \
626 cxstring: cx_json_fill_cxstr, \
627 cxmutstr: cx_json_fill_mutstr, \
628 char*: cx_json_fill_str, \
629 const char*: cx_json_fill_str) \
1358#define cxJsonObjGet(value, name) _Generic((name), \
1359 cxstring: cx_json_obj_get_cxstr, \
1360 cxmutstr: cx_json_obj_get_mutstr, \
1361 char*: cx_json_obj_get_str, \
1362 const char*: cx_json_obj_get_str) \
Interface for custom allocators.
Array list implementation.
#define CX_ARRAY_DECLARE_SIZED(type, name, size_type)
Declares variables for an array that can be used with the convenience macros.
Definition array_list.h:79
#define CX_ARRAY_DECLARE(type, name)
Declares variables for an array that can be used with the convenience macros.
Definition array_list.h:108
Advanced buffer implementation.
Common definitions and feature checks.
#define cx_attr_returns_nonnull
The returned value is guaranteed to be non-NULL.
Definition common.h:146
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
size_t(* cx_write_func)(const void *, size_t, size_t, void *)
Function pointer compatible with fwrite-like functions.
Definition common.h:294
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_access_w(...)
Specifies that the function will only write through the given pointer.
Definition common.h:241
#define cx_attr_access_r(...)
Specifies that the function will only read through the given pointer.
Definition common.h:225
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:141
#define cx_attr_cstr_arg(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:206
enum cx_json_status CxJsonStatus
Typedef for the json status enum.
Definition json.h:431
int cxJsonFilln(CxJson *json, const char *buf, size_t len)
Fills the input buffer.
static bool cxJsonIsArray(const CxJsonValue *value)
Checks if the specified value is a JSON array.
Definition json.h:1033
static int cx_json_fill_cxstr(CxJson *json, cxstring str)
Fills the input buffer.
Definition json.h:636
int cxJsonObjPut(CxJsonValue *obj, cxstring name, CxJsonValue *child)
Adds or replaces a value within a JSON object.
double CxJsonNumber
Type alias for number that is not an integer.
Definition json.h:205
static cxstring cxJsonAsCxString(const CxJsonValue *value)
Obtains a UCX string from the given JSON value.
Definition json.h:1181
CxJsonWriter cxJsonWriterPretty(bool use_spaces)
Creates a default writer configuration for pretty output.
int cxJsonArrAddCxStrings(CxJsonValue *arr, const cxstring *str, size_t count)
Adds strings to a JSON array.
CxJsonValue * cxJsonCreateLiteral(const CxAllocator *allocator, CxJsonLiteral lit)
Creates a new JSON literal.
#define cxJsonFill(json, str)
Fills the input buffer.
Definition json.h:625
CxJsonValue * cxJsonObjPutObj(CxJsonValue *obj, cxstring name)
Creates a new JSON object and adds it to an existing object.
int cxJsonArrAddLiterals(CxJsonValue *arr, const CxJsonLiteral *lit, size_t count)
Adds literals to a JSON array.
static bool cxJsonIsFalse(const CxJsonValue *value)
Checks if the specified value is false.
Definition json.h:1139
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 * cxJsonObjPutNumber(CxJsonValue *obj, cxstring name, double num)
Creates a new JSON number and adds it to an object.
CxJsonValue * cxJsonCreateObj(const CxAllocator *allocator)
Creates a new (empty) JSON object.
static int cx_json_fill_str(CxJson *json, const char *str)
Fills the input buffer.
Definition json.h:659
CxJsonValue * cxJsonObjPutArr(CxJsonValue *obj, cxstring name)
Creates a new JSON array and adds it to an object.
int cxJsonArrAddValues(CxJsonValue *arr, CxJsonValue *const *val, size_t count)
Add arbitrary values to a JSON array.
CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value)
Tries to obtain the next JSON value.
static size_t cxJsonArrSize(const CxJsonValue *value)
Returns the size of a JSON array.
Definition json.h:1263
enum cx_json_literal CxJsonLiteral
Type alias for a JSON literal.
Definition json.h:209
CxJsonValue * cxJsonCreateArr(const CxAllocator *allocator)
Creates a new (empty) JSON array.
int cxJsonArrAddStrings(CxJsonValue *arr, const char *const *str, size_t count)
Adds strings to a JSON array.
CxJsonValue * cxJsonObjPutLiteral(CxJsonValue *obj, cxstring name, CxJsonLiteral lit)
Creates a new JSON literal and adds it to an object.
static void cxJsonReset(CxJson *json)
Destroys and re-initializes the json interface.
Definition json.h:549
CxJsonValue * cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name)
Returns a value corresponding to a key in a JSON object.
static int cx_json_fill_mutstr(CxJson *json, cxmutstr str)
Fills the input buffer.
Definition json.h:647
static double cxJsonAsDouble(const CxJsonValue *value)
Obtains a double-precision floating point value from the given JSON value.
Definition json.h:1209
static bool cxJsonIsBool(const CxJsonValue *value)
Checks if the specified value is a Boolean literal.
Definition json.h:1105
static bool cxJsonIsTrue(const CxJsonValue *value)
Checks if the specified value is true.
Definition json.h:1122
static char * cxJsonAsString(const CxJsonValue *value)
Obtains a C string from the given JSON value.
Definition json.h:1167
int cxJsonArrAddNumbers(CxJsonValue *arr, const double *num, size_t count)
Adds number values to a JSON array.
cx_json_token_type
The type of the parsed token.
Definition json.h:55
@ CX_JSON_TOKEN_ERROR
The presumed token contains syntactical errors.
Definition json.h:63
@ CX_JSON_TOKEN_END_OBJECT
An "end of object" '}' token.
Definition json.h:79
@ CX_JSON_NO_TOKEN
No complete token parsed, yet.
Definition json.h:59
@ CX_JSON_TOKEN_END_ARRAY
An "end of array" ']' token.
Definition json.h:75
@ CX_JSON_TOKEN_STRING
A string token.
Definition json.h:91
@ CX_JSON_TOKEN_BEGIN_ARRAY
A "begin of array" '[' token.
Definition json.h:67
@ CX_JSON_TOKEN_NUMBER
A number token that cannot be represented as integer.
Definition json.h:99
@ CX_JSON_TOKEN_SPACE
A white-space token.
Definition json.h:107
@ CX_JSON_TOKEN_LITERAL
A literal token.
Definition json.h:103
@ CX_JSON_TOKEN_NAME_SEPARATOR
A colon ':' token separating names and values.
Definition json.h:83
@ CX_JSON_TOKEN_VALUE_SEPARATOR
A comma ',' token separating object entries or array elements.
Definition json.h:87
@ CX_JSON_TOKEN_INTEGER
A number token that can be represented as integer.
Definition json.h:95
@ CX_JSON_TOKEN_BEGIN_OBJECT
A "begin of object" '{' token.
Definition json.h:71
CxJsonValue * cxJsonArrGet(const CxJsonValue *value, size_t index)
Returns an element from a JSON array.
CxJsonValue * cxJsonCreateNumber(const CxAllocator *allocator, double num)
Creates a new JSON number value.
CxJsonValue * cxJsonObjPutCxString(CxJsonValue *obj, cxstring name, cxstring str)
Creates a new JSON string and adds it to an object.
static bool cxJsonIsLiteral(const CxJsonValue *value)
Checks if the specified value is a JSON literal.
Definition json.h:1091
static bool cxJsonIsObject(const CxJsonValue *value)
Checks if the specified value is a JSON object.
Definition json.h:1021
void cxJsonInit(CxJson *json, const CxAllocator *allocator)
Initializes the json interface.
cx_json_value_type
The type of some JSON value.
Definition json.h:113
@ CX_JSON_LITERAL
A literal (true, false, null).
Definition json.h:141
@ CX_JSON_NUMBER
A number, not necessarily an integer.
Definition json.h:137
@ CX_JSON_NOTHING
Reserved.
Definition json.h:117
@ CX_JSON_ARRAY
A JSON array.
Definition json.h:125
@ CX_JSON_INTEGER
A number that contains an integer.
Definition json.h:133
@ CX_JSON_OBJECT
A JSON object.
Definition json.h:121
@ CX_JSON_STRING
A string.
Definition json.h:129
CxJsonWriter cxJsonWriterCompact(void)
Creates a default writer configuration for compact output.
int cxJsonArrAddIntegers(CxJsonValue *arr, const int64_t *num, size_t count)
Adds number values, of which all are integers, to a JSON array.
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
enum cx_json_token_type CxJsonTokenType
Type alias for the token type enum.
Definition json.h:165
static cxmutstr cxJsonAsCxMutStr(const CxJsonValue *value)
Obtains a mutable UCX string from the given JSON value.
Definition json.h:1195
static bool cxJsonIsString(const CxJsonValue *value)
Checks if the specified value is a string.
Definition json.h:1045
cx_json_status
Status codes for the json interface.
Definition json.h:382
@ CX_JSON_FORMAT_ERROR_NUMBER
A number value is incorrectly formatted.
Definition json.h:421
@ CX_JSON_NO_DATA
The input buffer does not contain more data.
Definition json.h:390
@ CX_JSON_BUFFER_ALLOC_FAILED
Allocating memory for the internal buffer failed.
Definition json.h:413
@ CX_JSON_OK
Not used as a status and never returned by any function.
Definition json.h:405
@ CX_JSON_INCOMPLETE_DATA
The input ends unexpectedly.
Definition json.h:396
@ CX_JSON_NO_ERROR
Everything is fine.
Definition json.h:386
@ CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN
The tokenizer found something unexpected.
Definition json.h:425
@ CX_JSON_NULL_DATA
The input buffer has never been filled.
Definition json.h:409
@ CX_JSON_VALUE_ALLOC_FAILED
Allocating memory for a json value failed.
Definition json.h:417
int64_t CxJsonInteger
Type alias for a number that can be represented as 64-bit signed integer.
Definition json.h:201
enum cx_json_value_type CxJsonValueType
Type alias for the value type enum.
Definition json.h:169
static bool cxJsonIsNull(const CxJsonValue *value)
Checks if the specified value is null.
Definition json.h:1152
static bool cxJsonAsBool(const CxJsonValue *value)
Obtains a Boolean value from the given JSON value.
Definition json.h:1249
static bool cxJsonIsNumber(const CxJsonValue *value)
Checks if the specified value is a JSON number.
Definition json.h:1061
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.
int cxJsonWrite(void *target, const CxJsonValue *value, cx_write_func wfunc, const CxJsonWriter *settings)
Writes a JSON value to a buffer or stream.
CxJsonValue * cxJsonCreateInteger(const CxAllocator *allocator, int64_t num)
Creates a new JSON number value based on an integer.
cx_json_literal
JSON literal types.
Definition json.h:147
@ CX_JSON_TRUE
The true literal.
Definition json.h:155
@ CX_JSON_NULL
The null literal.
Definition json.h:151
@ CX_JSON_FALSE
The false literal.
Definition json.h:159
#define cxJsonObjGet(value, name)
Returns a value corresponding to a key in a JSON object.
Definition json.h:1358
void cxJsonDestroy(CxJson *json)
Destroys the json interface.
static bool cxJsonIsInteger(const CxJsonValue *value)
Checks if the specified value is an integer number.
Definition json.h:1074
void cxJsonValueFree(CxJsonValue *value)
Recursively deallocates the memory of a JSON value.
static int64_t cxJsonAsInteger(const CxJsonValue *value)
Obtains a 64-bit signed integer from the given JSON value.
Definition json.h:1230
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 * cxJsonCreateString(const CxAllocator *allocator, const char *str)
Creates a new JSON string.
CxJsonValue * cxJsonCreateCxString(const CxAllocator *allocator, cxstring str)
Creates a new JSON string.
Strings that know their length.
cxstring cx_str(const char *cstring)
Wraps a string that must be zero-terminated.
#define cx_strcast(str)
Casts a mutable string to an immutable string.
Definition string.h:295
Structure holding the data for an allocator.
Definition allocator.h:84
Structure for the UCX buffer data.
Definition buffer.h:157
Internal iterator struct - use CxIterator.
Definition iterator.h:91
JSON array structure.
Definition json.h:219
size_t array_size
Array size.
Definition json.h:223
CxJsonValue ** array
The array data.
Definition json.h:223
Structure for a key/value entry in a JSON object.
Definition json.h:243
CxJsonValue * value
The value.
Definition json.h:251
cxmutstr name
The key (or name in JSON terminology) of the value.
Definition json.h:247
JSON object structure.
Definition json.h:229
CxJsonObjValue * values
The key/value entries.
Definition json.h:233
size_t * indices
The original indices to reconstruct the order in which the members were added.
Definition json.h:237
The JSON parser interface.
Definition json.h:327
int states_internal[8]
Internally reserved memory for the state stack.
Definition json.h:371
CxJsonObjValue uncompleted_member
A pointer to an intermediate state of a currently parsed object member.
Definition json.h:356
int * states
State stack.
Definition json.h:361
const CxAllocator * allocator
The allocator used for produced JSON values.
Definition json.h:331
CxBuffer buffer
The input buffer.
Definition json.h:335
CxJsonValue ** vbuf
Value buffer stack.
Definition json.h:366
CxJsonValue * vbuf_internal[8]
Internally reserved memory for the value buffer stack.
Definition json.h:376
CxJsonToken uncompleted
Used internally.
Definition json.h:342
CxJsonValue * parsed
A pointer to an intermediate state of the currently parsed value.
Definition json.h:349
Internally used structure for a parsed token.
Definition json.h:306
cxmutstr content
The token text, if any.
Definition json.h:321
CxJsonTokenType tokentype
The token type.
Definition json.h:310
bool allocated
True, if the content must be passed to cx_strfree().
Definition json.h:314
Structure for a JSON value.
Definition json.h:257
const CxAllocator * allocator
The allocator with which the value was allocated.
Definition json.h:263
CxJsonArray array
The array data if type is CX_JSON_ARRAY.
Definition json.h:277
CxJsonLiteral literal
The literal type if type is CX_JSON_LITERAL.
Definition json.h:297
CxJsonNumber number
The number if type is CX_JSON_NUMBER.
Definition json.h:293
union cx_json_value_s::@3 value
The value data.
CxJsonString string
The string data if type is CX_JSON_STRING.
Definition json.h:285
CxJsonValueType type
The type of this value.
Definition json.h:269
CxJsonInteger integer
The integer if type is CX_JSON_INTEGER.
Definition json.h:289
CxJsonObject object
The object data if type is CX_JSON_OBJECT.
Definition json.h:281
The JSON writer settings.
Definition json.h:436
bool sort_members
Set false to output the members in the order in which they were added.
Definition json.h:444
bool indent_space
Set true to use spaces instead of tab characters.
Definition json.h:455
bool escape_slash
Set true to enable escaping of the slash character (solidus).
Definition json.h:464
uint8_t indent
If indent_space is true, this is the number of spaces per tab.
Definition json.h:460
uint8_t frac_max_digits
The maximum number of fractional digits in a number value.
Definition json.h:450
bool pretty
Set true to enable pretty output.
Definition json.h:440
The UCX string structure.
Definition string.h:51
char * ptr
A pointer to the string.
Definition string.h:56
size_t length
The length of the string.
Definition string.h:58
The UCX string structure for immutable (constant) strings.
Definition string.h:69
size_t length
The length of the string.
Definition string.h:76
const char * ptr
A pointer to the immutable string.
Definition string.h:74