Mstdlib-1.24.0
|
Typedefs | |
typedef struct M_json_node | M_json_node_t |
Functions | |
M_json_node_t * | M_json_node_create (M_json_type_t type) M_MALLOC |
void | M_json_node_destroy (M_json_node_t *node) M_FREE(1) |
M_json_node_t * | M_json_read (const char *data, size_t data_len, M_uint32 flags, size_t *processed_len, M_json_error_t *error, size_t *error_line, size_t *error_pos) M_MALLOC |
M_json_node_t * | M_json_read_file (const char *path, M_uint32 flags, size_t max_read, M_json_error_t *error, size_t *error_line, size_t *error_pos) M_MALLOC |
char * | M_json_write (const M_json_node_t *node, M_uint32 flags, size_t *len) M_WARN_UNUSED_RESULT M_MALLOC |
M_fs_error_t | M_json_write_file (const M_json_node_t *node, const char *path, M_uint32 flags) |
const char * | M_json_errcode_to_str (M_json_error_t err) |
M_json_type_t | M_json_node_type (const M_json_node_t *node) |
M_json_node_t ** | M_json_jsonpath (const M_json_node_t *node, const char *search, size_t *num_matches) M_MALLOC |
M_json_node_t * | M_json_get_parent (const M_json_node_t *node) |
void | M_json_take_from_parent (M_json_node_t *node) |
M_json_node_t * | M_json_object_value (const M_json_node_t *node, const char *key) |
const char * | M_json_object_value_string (const M_json_node_t *node, const char *key) |
M_int64 | M_json_object_value_int (const M_json_node_t *node, const char *key) |
const M_decimal_t * | M_json_object_value_decimal (const M_json_node_t *node, const char *key) |
M_bool | M_json_object_value_bool (const M_json_node_t *node, const char *key) |
M_list_str_t * | M_json_object_keys (const M_json_node_t *node) |
size_t | M_json_object_num_children (const M_json_node_t *node) |
M_bool | M_json_object_insert (M_json_node_t *node, const char *key, M_json_node_t *value) |
M_bool | M_json_object_insert_string (M_json_node_t *node, const char *key, const char *value) |
M_bool | M_json_object_insert_int (M_json_node_t *node, const char *key, M_int64 value) |
M_bool | M_json_object_insert_decimal (M_json_node_t *node, const char *key, const M_decimal_t *value) |
M_bool | M_json_object_insert_bool (M_json_node_t *node, const char *key, M_bool value) |
size_t | M_json_array_len (const M_json_node_t *node) |
M_json_node_t * | M_json_array_at (const M_json_node_t *node, size_t idx) |
const char * | M_json_array_at_string (const M_json_node_t *node, size_t idx) |
M_int64 | M_json_array_at_int (const M_json_node_t *node, size_t idx) |
const M_decimal_t * | M_json_array_at_decimal (const M_json_node_t *node, size_t idx) |
M_bool | M_json_array_at_bool (const M_json_node_t *node, size_t idx) |
M_bool | M_json_array_insert (M_json_node_t *node, M_json_node_t *value) |
M_bool | M_json_array_insert_string (M_json_node_t *node, const char *value) |
M_bool | M_json_array_insert_int (M_json_node_t *node, M_int64 value) |
M_bool | M_json_array_insert_decimal (M_json_node_t *node, const M_decimal_t *value) |
M_bool | M_json_array_insert_bool (M_json_node_t *node, M_bool value) |
M_bool | M_json_array_insert_at (M_json_node_t *node, M_json_node_t *value, size_t idx) |
M_bool | M_json_array_insert_at_string (M_json_node_t *node, const char *value, size_t idx) |
M_bool | M_json_array_insert_at_int (M_json_node_t *node, M_int64 value, size_t idx) |
M_bool | M_json_array_insert_at_decimal (M_json_node_t *node, const M_decimal_t *value, size_t idx) |
M_bool | M_json_array_insert_at_bool (M_json_node_t *node, M_bool value, size_t idx) |
const char * | M_json_get_string (const M_json_node_t *node) |
M_bool | M_json_set_string (M_json_node_t *node, const char *value) |
M_int64 | M_json_get_int (const M_json_node_t *node) |
M_bool | M_json_set_int (M_json_node_t *node, M_int64 value) |
const M_decimal_t * | M_json_get_decimal (const M_json_node_t *node) |
M_bool | M_json_set_decimal (M_json_node_t *node, const M_decimal_t *value) |
M_bool | M_json_get_bool (const M_json_node_t *node) |
M_bool | M_json_set_bool (M_json_node_t *node, M_bool value) |
M_bool | M_json_set_null (M_json_node_t *node) |
M_bool | M_json_get_value (const M_json_node_t *node, char *buf, size_t buf_len) |
char * | M_json_get_value_dup (const M_json_node_t *node) |
Mostly EMCA-404 compliant JSON manipulation.
Number handling while writing:
Additional Features:
Also supports most of Stefan Gössner's JSONPath for searching. Not supported are features considered redundant or potential security risks (script expressions).
Example:
typedef struct M_json_node M_json_node_t |
enum M_json_type_t |
Types of JSON nodes.
Flags to control the behavior of the JSON reader.
Flags to control the behavior of the JSON writer.
enum M_json_error_t |
Error codes.
M_json_node_t * M_json_node_create | ( | M_json_type_t | type | ) |
Create a JSON node.
[in] | type | The type of the node to create. |
void M_json_node_destroy | ( | M_json_node_t * | node | ) |
Destory a JSON node.
Destroying a node will destroy every node under it and remove it from it's parent node if it is a child.
[in] | node | The node to destroy. |
M_json_node_t * M_json_read | ( | const char * | data, |
size_t | data_len, | ||
M_uint32 | flags, | ||
size_t * | processed_len, | ||
M_json_error_t * | error, | ||
size_t * | error_line, | ||
size_t * | error_pos | ||
) |
Parse a string into a JSON object.
[in] | data | The data to parse. |
[in] | data_len | The length of the data to parse. |
[in] | flags | M_json_reader_flags_t flags to control the behavior of the reader. |
[out] | processed_len | Length of data processed. Useful if you could have multiple JSON documents in a stream. Optional pass NULL if not needed. |
[out] | error | On error this will be populated with an error reason. Optional, pass NULL if not needed. |
[out] | error_line | The line the error occurred. Optional, pass NULL if not needed. |
[out] | error_pos | The column the error occurred if error_line is not NULL, otherwise the position in the stream the error occurred. Optional, pass NULL if not needed. |
M_json_node_t * M_json_read_file | ( | const char * | path, |
M_uint32 | flags, | ||
size_t | max_read, | ||
M_json_error_t * | error, | ||
size_t * | error_line, | ||
size_t * | error_pos | ||
) |
Parse a file into a JSON object.
[in] | path | The file to read. |
[in] | flags | M_json_reader_flags_t flags to control the behavior of the reader. |
[in] | max_read | The maximum number of bytes to read from the file. If the data in the file is larger than max_read an error will most likely result. Optional pass 0 to read all data. |
[out] | error | On error this will be populated with an error reason. Optional, pass NULL if not needed. |
[out] | error_line | The line the error occurred. Optional, pass NULL if not needed. |
[out] | error_pos | The column the error occurred if error_line is not NULL, otherwise the position in the stream the error occurred. Optional, pass NULL if not needed. |
char * M_json_write | ( | const M_json_node_t * | node, |
M_uint32 | flags, | ||
size_t * | len | ||
) |
Write JSON to a string.
This writes nodes to a string. The string may not be directly usable by M_json_read. E.g. If you are only writing a string node.
[in] | node | The node to write. This will write the node and any nodes under it. |
[in] | flags | M_json_writer_flags_t flags to control writing. |
[out] | len | The length of the string that was returned. Optional, pass NULL if not needed. |
M_fs_error_t M_json_write_file | ( | const M_json_node_t * | node, |
const char * | path, | ||
M_uint32 | flags | ||
) |
Write JSON to a file.
This writes nodes to a string. The string may not be directly usable by M_json_read_file (for example) if you are only writing a string node (for example).
[in] | node | The node to write. This will write the node and any nodes under it. |
[in] | path | The filename and path to write the data to. |
[in] | flags | M_json_writer_flags_t flags to control writing. |
const char * M_json_errcode_to_str | ( | M_json_error_t | err | ) |
Convert a JSON error code to a string.
[in] | err | error code |
M_json_type_t M_json_node_type | ( | const M_json_node_t * | node | ) |
Get the type of node.
[in] | node | The node. |
M_json_node_t ** M_json_jsonpath | ( | const M_json_node_t * | node, |
const char * | search, | ||
size_t * | num_matches | ||
) |
Using JSONPath expressions, scan for matches.
Note: that full JSONPath support does not yet exist.
Search expressions must start with $. They can use . to refer to the first element or .. to search for the first matching element.
Supports:
Does not Support:
[in] | node | The node. |
[in] | search | search expression |
[out] | num_matches | Number of matches found |
M_json_node_t * M_json_get_parent | ( | const M_json_node_t * | node | ) |
Get the parent node of a given node.
[in] | node | The node. |
void M_json_take_from_parent | ( | M_json_node_t * | node | ) |
Take the node from the parent but does not destroy it.
This allows a node to be moved between different parents.
[in,out] | node | The node. |
M_json_node_t * M_json_object_value | ( | const M_json_node_t * | node, |
const char * | key | ||
) |
Get the value of an object node for a given key.
The object still owns the returned node. You can use M_json_take_from_parent to remove the ownership. At which point you will need to either insert it into another object/array or destroy it.
[in] | node | The node. |
[in] | key | The key. |
const char * M_json_object_value_string | ( | const M_json_node_t * | node, |
const char * | key | ||
) |
Get the string value of an object node for a given key.
[in] | node | The node. |
[in] | key | The key. |
M_int64 M_json_object_value_int | ( | const M_json_node_t * | node, |
const char * | key | ||
) |
Get the integer value of an object node for a given key.
If the node is not an M_JSON_TYPE_INTEGER auto conversion will be attempted.
[in] | node | The node. |
[in] | key | The key. |
const M_decimal_t * M_json_object_value_decimal | ( | const M_json_node_t * | node, |
const char * | key | ||
) |
Get the decimal value of an object node for a given key.
[in] | node | The node. |
[in] | key | The key. |
M_bool M_json_object_value_bool | ( | const M_json_node_t * | node, |
const char * | key | ||
) |
Get the bool value of an object node for a given key.
If the node is not a M_JSON_TYPE_BOOL auto conversion will be attempted.
[in] | node | The node. |
[in] | key | The key. |
M_list_str_t * M_json_object_keys | ( | const M_json_node_t * | node | ) |
Get a list of all keys for the object.
[in] | node | The node. |
size_t M_json_object_num_children | ( | const M_json_node_t * | node | ) |
Get the number of child nodes in this object.
This corresponds to the number of gets.
[in] | node | The node. |
M_bool M_json_object_insert | ( | M_json_node_t * | node, |
const char * | key, | ||
M_json_node_t * | value | ||
) |
Insert a node into the object.
The object node will take ownership of the value node.
[in,out] | node | The node. |
[in] | key | The key. If the key already exists the existing node will be destroyed and replaced with the new value node. |
[in] | value | The node to add to the object. |
M_bool M_json_object_insert_string | ( | M_json_node_t * | node, |
const char * | key, | ||
const char * | value | ||
) |
Insert a string into the object.
[in,out] | node | The node. |
[in] | key | The key. If the key already exists the existing node will be destroyed and replaced with the new value node. |
[in] | value | The string to add to the object. |
M_bool M_json_object_insert_int | ( | M_json_node_t * | node, |
const char * | key, | ||
M_int64 | value | ||
) |
Insert an integer into the object.
[in,out] | node | The node. |
[in] | key | The key. If the key already exists the existing node will be destroyed and replaced with the new value node. |
[in] | value | The integer to add to the object. |
M_bool M_json_object_insert_decimal | ( | M_json_node_t * | node, |
const char * | key, | ||
const M_decimal_t * | value | ||
) |
Insert an decimal into the object.
[in,out] | node | The node. |
[in] | key | The key. If the key already exists the existing node will be destroyed and replaced with the new value node. |
[in] | value | The decimal to add to the object. |
M_bool M_json_object_insert_bool | ( | M_json_node_t * | node, |
const char * | key, | ||
M_bool | value | ||
) |
Insert an bool into the object.
[in,out] | node | The node. |
[in] | key | The key. If the key already exists the existing node will be destroyed and replaced with the new value node. |
[in] | value | The bool to add to the object. |
size_t M_json_array_len | ( | const M_json_node_t * | node | ) |
Get the number of items in an array node.
[in] | node | The node. |
M_json_node_t * M_json_array_at | ( | const M_json_node_t * | node, |
size_t | idx | ||
) |
Get the item in the array at a given index.
The array still owns the returned node. You can use M_json_take_from_parent to remove the ownership. At which point you will need to either insert it into another object/array or destroy it.
[in] | node | The node. |
[in] | idx | The index. |
const char * M_json_array_at_string | ( | const M_json_node_t * | node, |
size_t | idx | ||
) |
Get the string value of given index in an array.
[in] | node | The node. |
[in] | idx | The index. |
M_int64 M_json_array_at_int | ( | const M_json_node_t * | node, |
size_t | idx | ||
) |
Get the integer value of given index in an array.
If the node is not an M_JSON_TYPE_INTEGER auto conversion will be attempted.
[in] | node | The node. |
[in] | idx | The index. |
const M_decimal_t * M_json_array_at_decimal | ( | const M_json_node_t * | node, |
size_t | idx | ||
) |
Get the decimal value of given index in an array.
[in] | node | The node. |
[in] | idx | The index. |
M_bool M_json_array_at_bool | ( | const M_json_node_t * | node, |
size_t | idx | ||
) |
Get the string value of given index in an array.
If the node is not a M_JSON_TYPE_BOOL auto conversion will be attempted.
[in] | node | The node. |
[in] | idx | The index. |
M_bool M_json_array_insert | ( | M_json_node_t * | node, |
M_json_node_t * | value | ||
) |
Append a node into an array node.
[in,out] | node | The node. |
[in] | value | The value node to append. |
M_bool M_json_array_insert_string | ( | M_json_node_t * | node, |
const char * | value | ||
) |
Append a string into an array node.
[in,out] | node | The node. |
[in] | value | The value to append. |
M_bool M_json_array_insert_int | ( | M_json_node_t * | node, |
M_int64 | value | ||
) |
Append a integer into an array node.
[in,out] | node | The node. |
[in] | value | The value to append. |
M_bool M_json_array_insert_decimal | ( | M_json_node_t * | node, |
const M_decimal_t * | value | ||
) |
Append a decimal into an array node.
[in,out] | node | The node. |
[in] | value | The value to append. |
M_bool M_json_array_insert_bool | ( | M_json_node_t * | node, |
M_bool | value | ||
) |
Append a bool into an array node.
[in,out] | node | The node. |
[in] | value | The value to append. |
M_bool M_json_array_insert_at | ( | M_json_node_t * | node, |
M_json_node_t * | value, | ||
size_t | idx | ||
) |
Insert a node into an array node at a given index.
[in,out] | node | The node. |
[in] | value | The value node to append. |
[in] | idx | The index to insert at. |
M_bool M_json_array_insert_at_string | ( | M_json_node_t * | node, |
const char * | value, | ||
size_t | idx | ||
) |
Insert a string into an array node at a given index.
[in,out] | node | The node. |
[in] | value | The value to append. |
[in] | idx | The index to insert at. |
M_bool M_json_array_insert_at_int | ( | M_json_node_t * | node, |
M_int64 | value, | ||
size_t | idx | ||
) |
Insert a integer into an array node at a given index.
[in,out] | node | The node. |
[in] | value | The value to append. |
[in] | idx | The index to insert at. |
M_bool M_json_array_insert_at_decimal | ( | M_json_node_t * | node, |
const M_decimal_t * | value, | ||
size_t | idx | ||
) |
Insert a decimal into an array node at a given index.
[in,out] | node | The node. |
[in] | value | The value to append. |
[in] | idx | The index to insert at. |
M_bool M_json_array_insert_at_bool | ( | M_json_node_t * | node, |
M_bool | value, | ||
size_t | idx | ||
) |
Insert a bool into an array node at a given index.
[in,out] | node | The node. |
[in] | value | The value to append. |
[in] | idx | The index to insert at. |
const char * M_json_get_string | ( | const M_json_node_t * | node | ) |
Get the value from a string node.
[in] | node | The node. |
M_bool M_json_set_string | ( | M_json_node_t * | node, |
const char * | value | ||
) |
Make the node a string node and set the value.
[in,out] | node | The node. |
[in] | value | The value to set. |
M_int64 M_json_get_int | ( | const M_json_node_t * | node | ) |
Get the value from an integer node.
If the node is not an M_JSON_TYPE_INTEGER auto conversion will be attempted.
[in] | node | The node. |
M_bool M_json_set_int | ( | M_json_node_t * | node, |
M_int64 | value | ||
) |
Make the node a integer node and set the value.
[in,out] | node | The node. |
[in] | value | The value. |
const M_decimal_t * M_json_get_decimal | ( | const M_json_node_t * | node | ) |
Get the value from a decimal node.
[in] | node | The node. |
M_bool M_json_set_decimal | ( | M_json_node_t * | node, |
const M_decimal_t * | value | ||
) |
Make the node a decimal node and set the value.
[in,out] | node | The node. |
[in] | value | The value. |
M_bool M_json_get_bool | ( | const M_json_node_t * | node | ) |
Get the value from a bool node.
If the node is not a M_JSON_TYPE_BOOL auto conversion will be attempted.
[in] | node | The node. |
M_bool M_json_set_bool | ( | M_json_node_t * | node, |
M_bool | value | ||
) |
Make the node a bool node and set the value.
[in,out] | node | The node. |
[in] | value | The value. |
M_bool M_json_set_null | ( | M_json_node_t * | node | ) |
Make the node a null node.
[in] | node | The node. |
M_bool M_json_get_value | ( | const M_json_node_t * | node, |
char * | buf, | ||
size_t | buf_len | ||
) |
Get the node value as a string.
This will only work on value type nodes (string, integer, decimal, book, null). Other node types (object, array) will fail.
[in] | node | The node. |
[out] | buf | An allocated buffer to write the value as a string to. The result will be null terminated on success. |
[in] | buf_len | The length of the buffer. |
char * M_json_get_value_dup | ( | const M_json_node_t * | node | ) |
Get the node value as a string.
This will only work on value type nodes (string, integer, decimal, book, null). Other node types (object, array) will fail.
[in] | node | The node. |