Mstdlib-1.24.0

Typedefs

typedef struct M_json_node M_json_node_t
 

Enumerations

enum  M_json_type_t {
  M_JSON_TYPE_UNKNOWN = 0 ,
  M_JSON_TYPE_OBJECT ,
  M_JSON_TYPE_ARRAY ,
  M_JSON_TYPE_STRING ,
  M_JSON_TYPE_INTEGER ,
  M_JSON_TYPE_DECIMAL ,
  M_JSON_TYPE_BOOL ,
  M_JSON_TYPE_NULL
}
 
enum  M_json_reader_flags_t {
  M_JSON_READER_NONE = 0 ,
  M_JSON_READER_ALLOW_DECIMAL_TRUNCATION = 1 << 0 ,
  M_JSON_READER_DISALLOW_COMMENTS = 1 << 1 ,
  M_JSON_READER_OBJECT_UNIQUE_KEYS = 1 << 2 ,
  M_JSON_READER_DONT_DECODE_UNICODE = 1 << 3 ,
  M_JSON_READER_REPLACE_BAD_CHARS = 1 << 4
}
 
enum  M_json_writer_flags_t {
  M_JSON_WRITER_NONE = 0 ,
  M_JSON_WRITER_PRETTYPRINT_SPACE = 1 << 0 ,
  M_JSON_WRITER_PRETTYPRINT_TAB = 1 << 1 ,
  M_JSON_WRITER_PRETTYPRINT_WINLINEEND = 1 << 2 ,
  M_JSON_WRITER_DONT_ENCODE_UNICODE = 1 << 3 ,
  M_JSON_WRITER_REPLACE_BAD_CHARS = 1 << 4 ,
  M_JSON_WRITER_NUMBER_NOCOMPAT = 1 << 5
}
 
enum  M_json_error_t {
  M_JSON_ERROR_SUCCESS = 0 ,
  M_JSON_ERROR_GENERIC ,
  M_JSON_ERROR_MISUSE ,
  M_JSON_ERROR_INVALID_START ,
  M_JSON_ERROR_EXPECTED_END ,
  M_JSON_ERROR_MISSING_COMMENT_CLOSE ,
  M_JSON_ERROR_UNEXPECTED_COMMENT_START ,
  M_JSON_ERROR_INVALID_PAIR_START ,
  M_JSON_ERROR_DUPLICATE_KEY ,
  M_JSON_ERROR_MISSING_PAIR_SEPARATOR ,
  M_JSON_ERROR_OBJECT_UNEXPECTED_CHAR ,
  M_JSON_ERROR_EXPECTED_VALUE ,
  M_JSON_ERROR_UNCLOSED_OBJECT ,
  M_JSON_ERROR_ARRAY_UNEXPECTED_CHAR ,
  M_JSON_ERROR_UNCLOSED_ARRAY ,
  M_JSON_ERROR_UNEXPECTED_NEWLINE ,
  M_JSON_ERROR_UNEXPECTED_CONTROL_CHAR ,
  M_JSON_ERROR_INVALID_UNICODE_ESACPE ,
  M_JSON_ERROR_UNEXPECTED_ESCAPE ,
  M_JSON_ERROR_UNCLOSED_STRING ,
  M_JSON_ERROR_INVALID_BOOL ,
  M_JSON_ERROR_INVALID_NULL ,
  M_JSON_ERROR_INVALID_NUMBER ,
  M_JSON_ERROR_UNEXPECTED_TERMINATION ,
  M_JSON_ERROR_INVALID_IDENTIFIER ,
  M_JSON_ERROR_UNEXPECTED_END
}
 

Functions

M_json_node_tM_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_tM_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_tM_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_tM_json_get_parent (const M_json_node_t *node)
 
void M_json_take_from_parent (M_json_node_t *node)
 
M_json_node_tM_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_tM_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_tM_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_tM_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_tM_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_tM_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)
 

Detailed Description

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:

size_t num_matches;
size_t i;
const char *s = "{ \"a\" :\n[1, \"abc\",2 ]\n}";
char *out;
j = M_json_read(s, M_str_len(s), M_JSON_READER_NONE, NULL, NULL, NULL, NULL);
if (j == NULL) {
M_printf("Could not parse json\n");
return M_FALSE;
}
M_json_object_insert_string(j, "b", "string");
n = M_json_jsonpath(j, "$.a[1::3]", &num_matches);
for (i=0; i<num_matches; i++) {
}
}
M_free(n);
M_printf(out=\n%s\n", out);
M_free(out);
M_json_node_destroy(j);
ssize_t M_printf(const char *fmt,...)
M_bool M_json_object_insert_string(M_json_node_t *node, const char *key, const char *value)
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
struct M_json_node M_json_node_t
Definition: m_json.h:95
const char * M_json_get_string(const M_json_node_t *node)
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
char * M_json_write(const M_json_node_t *node, M_uint32 flags, size_t *len) M_WARN_UNUSED_RESULT M_MALLOC
@ M_JSON_READER_NONE
Definition: m_json.h:115
@ M_JSON_TYPE_STRING
Definition: m_json.h:105
@ M_JSON_WRITER_PRETTYPRINT_SPACE
Definition: m_json.h:135
void M_free(void *ptr) M_FREE(1)
size_t M_str_len(const char *s) M_WARN_UNUSED_RESULT

Typedef Documentation

◆ M_json_node_t

typedef struct M_json_node M_json_node_t

Enumeration Type Documentation

◆ M_json_type_t

Types of JSON nodes.

Enumerator
M_JSON_TYPE_UNKNOWN 

An invalid node type.

M_JSON_TYPE_OBJECT 

Object (hashtable).

M_JSON_TYPE_ARRAY 

Array (list).

M_JSON_TYPE_STRING 

String.

M_JSON_TYPE_INTEGER 

Number.

M_JSON_TYPE_DECIMAL 

Floating point number.

M_JSON_TYPE_BOOL 

Boolean.

M_JSON_TYPE_NULL 

JSON null type.

◆ M_json_reader_flags_t

Flags to control the behavior of the JSON reader.

Enumerator
M_JSON_READER_NONE 

Normal operation. Treat decimal truncation as error and ignore comments.

M_JSON_READER_ALLOW_DECIMAL_TRUNCATION 

Allow decimal truncation. A decimal read and truncated will not be treated as an error.

M_JSON_READER_DISALLOW_COMMENTS 

Treat comments as an error.

M_JSON_READER_OBJECT_UNIQUE_KEYS 

Return a parse error when an object has repeating keys. By default the later key in the object will be the one used and earlier keys ignored. This requires all keys in the object to be unique.

M_JSON_READER_DONT_DECODE_UNICODE 

By default unicode escapes will be decoded into their utf-8 byte sequence. Use this with care because "\u" will be put in the string. Writing will produce "\\u" because the writer will not understand this is a non-decoded unicode escape.

M_JSON_READER_REPLACE_BAD_CHARS 

Replace bad characters (invalid utf-8 sequences with "?").

◆ M_json_writer_flags_t

Flags to control the behavior of the JSON writer.

Enumerator
M_JSON_WRITER_NONE 

No indent. All data on a single line.

M_JSON_WRITER_PRETTYPRINT_SPACE 

2 space indent.

M_JSON_WRITER_PRETTYPRINT_TAB 

Tab indent.

M_JSON_WRITER_PRETTYPRINT_WINLINEEND 

Windows line ending "\r\n" instead of Unix line ending "\n". Requires space or tab pretty printing.

M_JSON_WRITER_DONT_ENCODE_UNICODE 

By default utf-8 characters will be enocded into unicode escapes.

M_JSON_WRITER_REPLACE_BAD_CHARS 

Replace bad characters (invalid utf-8 sequences with "?").

M_JSON_WRITER_NUMBER_NOCOMPAT 

Write numbers as they are instead of limiting to Java Script minimum and maximum sizes.

◆ M_json_error_t

Error codes.

Enumerator
M_JSON_ERROR_SUCCESS 

success

M_JSON_ERROR_GENERIC 

generic error

M_JSON_ERROR_MISUSE 

API missuse

M_JSON_ERROR_INVALID_START 

expected Object or Array to start

M_JSON_ERROR_EXPECTED_END 

expected end but more data found

M_JSON_ERROR_MISSING_COMMENT_CLOSE 

close comment not found

M_JSON_ERROR_UNEXPECTED_COMMENT_START 

unexpected /

M_JSON_ERROR_INVALID_PAIR_START 

expected string as first half of pair

M_JSON_ERROR_DUPLICATE_KEY 

duplicate key

M_JSON_ERROR_MISSING_PAIR_SEPARATOR 

expected ':' separator in pair

M_JSON_ERROR_OBJECT_UNEXPECTED_CHAR 

unexpected character in object

M_JSON_ERROR_EXPECTED_VALUE 

expected value after ','

M_JSON_ERROR_UNCLOSED_OBJECT 

expected '}' to close object

M_JSON_ERROR_ARRAY_UNEXPECTED_CHAR 

unexpected character in array

M_JSON_ERROR_UNCLOSED_ARRAY 

expected ']' to close array

M_JSON_ERROR_UNEXPECTED_NEWLINE 

unexpected newline

M_JSON_ERROR_UNEXPECTED_CONTROL_CHAR 

unexpected control character

M_JSON_ERROR_INVALID_UNICODE_ESACPE 

invalid unicode escape

M_JSON_ERROR_UNEXPECTED_ESCAPE 

unexpected escape

M_JSON_ERROR_UNCLOSED_STRING 

unclosed string

M_JSON_ERROR_INVALID_BOOL 

invalid bool value

M_JSON_ERROR_INVALID_NULL 

invalid null value

M_JSON_ERROR_INVALID_NUMBER 

invalid number value

M_JSON_ERROR_UNEXPECTED_TERMINATION 

unexpected termination of string data. \0 in data.

M_JSON_ERROR_INVALID_IDENTIFIER 

invalid identifier

M_JSON_ERROR_UNEXPECTED_END 

unexpected end of data

Function Documentation

◆ M_json_node_create()

M_json_node_t * M_json_node_create ( M_json_type_t  type)

Create a JSON node.

Parameters
[in]typeThe type of the node to create.
Returns
A JSON node on success. NULL on failure (an invalid type was requested).
See also
M_json_node_destroy

◆ M_json_node_destroy()

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.

Parameters
[in]nodeThe node to destroy.

◆ M_json_read()

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.

Parameters
[in]dataThe data to parse.
[in]data_lenThe length of the data to parse.
[in]flagsM_json_reader_flags_t flags to control the behavior of the reader.
[out]processed_lenLength of data processed. Useful if you could have multiple JSON documents in a stream. Optional pass NULL if not needed.
[out]errorOn error this will be populated with an error reason. Optional, pass NULL if not needed.
[out]error_lineThe line the error occurred. Optional, pass NULL if not needed.
[out]error_posThe 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.
Returns
The root JSON node of the parsed data, or NULL on error.

◆ M_json_read_file()

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.

Parameters
[in]pathThe file to read.
[in]flagsM_json_reader_flags_t flags to control the behavior of the reader.
[in]max_readThe 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]errorOn error this will be populated with an error reason. Optional, pass NULL if not needed.
[out]error_lineThe line the error occurred. Optional, pass NULL if not needed.
[out]error_posThe 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.
Returns
The root JSON node of the parsed data, or NULL on error.

◆ M_json_write()

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.

Parameters
[in]nodeThe node to write. This will write the node and any nodes under it.
[in]flagsM_json_writer_flags_t flags to control writing.
[out]lenThe length of the string that was returned. Optional, pass NULL if not needed.
Returns
A string with data or NULL on error.

◆ M_json_write_file()

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

Parameters
[in]nodeThe node to write. This will write the node and any nodes under it.
[in]pathThe filename and path to write the data to.
[in]flagsM_json_writer_flags_t flags to control writing.
Returns
Result.

◆ M_json_errcode_to_str()

const char * M_json_errcode_to_str ( M_json_error_t  err)

Convert a JSON error code to a string.

Parameters
[in]errerror code
Returns
name of error code (not a description, just the enum name, like M_JSON_ERROR_SUCCESS)

◆ M_json_node_type()

M_json_type_t M_json_node_type ( const M_json_node_t node)

Get the type of node.

Parameters
[in]nodeThe node.
Returns
The type.

◆ M_json_jsonpath()

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:

  • Patterns containing ".", "*", "..".
  • Array offsets using [*]/[]/[,]/[start:end:step].
    • Positive offsets [0], [0,2].
    • Negative offsets [-1] (last item). [-2] (second to last item).
    • Positive and negative steps. [0:4:2]. [4:0:-1].
      • When counting up start is inclusive and end is exclusive. [0:3] is equivalent to [0,1,2].
      • When counting down start is exclusive and end is inclusive. [3:0:-1] is equivalent to [2,1,0].

Does not Support:

  • Braket notation ['x'].
  • Filter/script expressions. [?(exp)]/[(exp)].
Parameters
[in]nodeThe node.
[in]searchsearch expression
[out]num_matchesNumber of matches found
Returns
array of M_json_node_t pointers on success (must free array, but not internal pointers), NULL on failure
See also
M_free

◆ M_json_get_parent()

M_json_node_t * M_json_get_parent ( const M_json_node_t node)

Get the parent node of a given node.

Parameters
[in]nodeThe node.
Returns
The parent node or NULL if there is no parent.

◆ M_json_take_from_parent()

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.

Parameters
[in,out]nodeThe node.

◆ M_json_object_value()

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.

Parameters
[in]nodeThe node.
[in]keyThe key.
Returns
The node under key. Otherwise NULL if the key does not exist.

◆ M_json_object_value_string()

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.

Parameters
[in]nodeThe node.
[in]keyThe key.
Returns
The string value under the key. NULL if not a string or key does not exist.

◆ M_json_object_value_int()

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.

Parameters
[in]nodeThe node.
[in]keyThe key.
Returns
The value. 0 on error. The only way to know if there was an error or the return is the value is to check the type.

◆ M_json_object_value_decimal()

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.

Parameters
[in]nodeThe node.
[in]keyThe key.
Returns
The string value under the key. NULL if not a decimal or key does not exist.

◆ M_json_object_value_bool()

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.

Parameters
[in]nodeThe node.
[in]keyThe key.
Returns
The value. M_FALSE on error. The only way to know if there was an error or the return is the value is to check the type.

◆ M_json_object_keys()

M_list_str_t * M_json_object_keys ( const M_json_node_t node)

Get a list of all keys for the object.

Parameters
[in]nodeThe node.
Returns
A list of keys.

◆ M_json_object_num_children()

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.

Parameters
[in]nodeThe node.
Returns
Count of objects.

◆ M_json_object_insert()

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.

Parameters
[in,out]nodeThe node.
[in]keyThe key. If the key already exists the existing node will be destroyed and replaced with the new value node.
[in]valueThe node to add to the object.
Returns
M_TRUE on success otherwise M_FALSE.

◆ M_json_object_insert_string()

M_bool M_json_object_insert_string ( M_json_node_t node,
const char *  key,
const char *  value 
)

Insert a string into the object.

Parameters
[in,out]nodeThe node.
[in]keyThe key. If the key already exists the existing node will be destroyed and replaced with the new value node.
[in]valueThe string to add to the object.
Returns
M_TRUE on success otherwise M_FALSE.

◆ M_json_object_insert_int()

M_bool M_json_object_insert_int ( M_json_node_t node,
const char *  key,
M_int64  value 
)

Insert an integer into the object.

Parameters
[in,out]nodeThe node.
[in]keyThe key. If the key already exists the existing node will be destroyed and replaced with the new value node.
[in]valueThe integer to add to the object.
Returns
M_TRUE on success otherwise M_FALSE.

◆ M_json_object_insert_decimal()

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.

Parameters
[in,out]nodeThe node.
[in]keyThe key. If the key already exists the existing node will be destroyed and replaced with the new value node.
[in]valueThe decimal to add to the object.
Returns
M_TRUE on success otherwise M_FALSE.

◆ M_json_object_insert_bool()

M_bool M_json_object_insert_bool ( M_json_node_t node,
const char *  key,
M_bool  value 
)

Insert an bool into the object.

Parameters
[in,out]nodeThe node.
[in]keyThe key. If the key already exists the existing node will be destroyed and replaced with the new value node.
[in]valueThe bool to add to the object.
Returns
M_TRUE on success otherwise M_FALSE.

◆ M_json_array_len()

size_t M_json_array_len ( const M_json_node_t node)

Get the number of items in an array node.

Parameters
[in]nodeThe node.
Returns
The number of items in the array.

◆ M_json_array_at()

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.

Parameters
[in]nodeThe node.
[in]idxThe index.
Returns
The node at the given index or NULL if the index is invalid.

◆ M_json_array_at_string()

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.

Parameters
[in]nodeThe node.
[in]idxThe index.
Returns
The string value at the location. NULL if not a string or key does not exist.

◆ M_json_array_at_int()

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.

Parameters
[in]nodeThe node.
[in]idxThe index.
Returns
The value. 0 on error. The only way to know if there was an error or the return is the value is to check the type.

◆ M_json_array_at_decimal()

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.

Parameters
[in]nodeThe node.
[in]idxThe index.
Returns
The string value under the key. NULL if not a decimal or index does not exist.

◆ M_json_array_at_bool()

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.

Parameters
[in]nodeThe node.
[in]idxThe index.
Returns
The value. M_FALSE on error. The only way to know if there was an error or the return is the value is to check the type.

◆ M_json_array_insert()

M_bool M_json_array_insert ( M_json_node_t node,
M_json_node_t value 
)

Append a node into an array node.

Parameters
[in,out]nodeThe node.
[in]valueThe value node to append.
Returns
M_TRUE if the value was appended otherwise M_FALSE.

◆ M_json_array_insert_string()

M_bool M_json_array_insert_string ( M_json_node_t node,
const char *  value 
)

Append a string into an array node.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
Returns
M_TRUE if the value was appended otherwise M_FALSE.

◆ M_json_array_insert_int()

M_bool M_json_array_insert_int ( M_json_node_t node,
M_int64  value 
)

Append a integer into an array node.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
Returns
M_TRUE if the value was appended otherwise M_FALSE.

◆ M_json_array_insert_decimal()

M_bool M_json_array_insert_decimal ( M_json_node_t node,
const M_decimal_t value 
)

Append a decimal into an array node.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
Returns
M_TRUE if the value was appended otherwise M_FALSE.

◆ M_json_array_insert_bool()

M_bool M_json_array_insert_bool ( M_json_node_t node,
M_bool  value 
)

Append a bool into an array node.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
Returns
M_TRUE if the value was appended otherwise M_FALSE.

◆ M_json_array_insert_at()

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.

Parameters
[in,out]nodeThe node.
[in]valueThe value node to append.
[in]idxThe index to insert at.
Returns
M_TRUE if the value was inserted otherwise M_FALSE.

◆ M_json_array_insert_at_string()

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.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
[in]idxThe index to insert at.
Returns
M_TRUE if the value was inserted otherwise M_FALSE.

◆ M_json_array_insert_at_int()

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.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
[in]idxThe index to insert at.
Returns
M_TRUE if the value was inserted otherwise M_FALSE.

◆ M_json_array_insert_at_decimal()

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.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
[in]idxThe index to insert at.
Returns
M_TRUE if the value was inserted otherwise M_FALSE.

◆ M_json_array_insert_at_bool()

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.

Parameters
[in,out]nodeThe node.
[in]valueThe value to append.
[in]idxThe index to insert at.
Returns
M_TRUE if the value was inserted otherwise M_FALSE.

◆ M_json_get_string()

const char * M_json_get_string ( const M_json_node_t node)

Get the value from a string node.

Parameters
[in]nodeThe node.
Returns
The value.

◆ M_json_set_string()

M_bool M_json_set_string ( M_json_node_t node,
const char *  value 
)

Make the node a string node and set the value.

Parameters
[in,out]nodeThe node.
[in]valueThe value to set.
Returns
M_TRUE if the node updated.

◆ M_json_get_int()

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.

Parameters
[in]nodeThe node.
Returns
The value. 0 on error. The only way to know if there was an error or the return is the value is to check the type.

◆ M_json_set_int()

M_bool M_json_set_int ( M_json_node_t node,
M_int64  value 
)

Make the node a integer node and set the value.

Parameters
[in,out]nodeThe node.
[in]valueThe value.
Returns
M_TRUE if the node updated.

◆ M_json_get_decimal()

const M_decimal_t * M_json_get_decimal ( const M_json_node_t node)

Get the value from a decimal node.

Parameters
[in]nodeThe node.
Returns
The value.

◆ M_json_set_decimal()

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.

Parameters
[in,out]nodeThe node.
[in]valueThe value.
Returns
M_TRUE if the node updated.

◆ M_json_get_bool()

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.

Parameters
[in]nodeThe node.
Returns
The value. M_FALSE on error. The only way to know if there was an error or the return is the value is to check the type.
See also
M_json_node_type

◆ M_json_set_bool()

M_bool M_json_set_bool ( M_json_node_t node,
M_bool  value 
)

Make the node a bool node and set the value.

Parameters
[in,out]nodeThe node.
[in]valueThe value.
Returns
M_TRUE if the node updated.

◆ M_json_set_null()

M_bool M_json_set_null ( M_json_node_t node)

Make the node a null node.

Parameters
[in]nodeThe node.
Returns
M_TRUE if the node updated.

◆ M_json_get_value()

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.

Parameters
[in]nodeThe node.
[out]bufAn allocated buffer to write the value as a string to. The result will be null terminated on success.
[in]buf_lenThe length of the buffer.
Returns
M_TRUE on success. Otherwise M_FALSE.

◆ M_json_get_value_dup()

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.

Parameters
[in]nodeThe node.
Returns
The value or NULL on error.