Include this header file to use coreJSON in your application. More...
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
Go to the source code of this file.
Data Structures | |
struct | JSONPair_t |
Structure to represent a key-value pair. More... | |
Macros | |
#define | coreJSON_ASSERT(expr) assert( expr ) |
By default, has the stand behavior of assert() for parameter checking. To swap out the assert(), define this macro with the desired behavior. | |
#define | JSON_Search(buf, max, query, queryLength, outValue, outValueLength) JSON_SearchT( buf, max, query, queryLength, outValue, outValueLength, NULL ) |
Find a key or array index in a JSON document and output the pointer outValue to its value. | |
#define | MAX_INDEX_VALUE ( 0x7FFFFFF7 ) /* 2^31 - 9 */ |
The largest value usable as an array index in a query for JSON_Search(), ~2 billion. | |
Enumerations | |
enum | JSONStatus_t { JSONPartial = 0 , JSONSuccess , JSONIllegalDocument , JSONMaxDepthExceeded , JSONNotFound , JSONNullParameter , JSONBadParameter } |
Return codes from coreJSON library functions. More... | |
enum | JSONTypes_t { JSONInvalid = 0 , JSONString , JSONNumber , JSONTrue , JSONFalse , JSONNull , JSONObject , JSONArray } |
Value types from the JSON standard. More... | |
Functions | |
JSONStatus_t | JSON_Validate (const char *buf, size_t max) |
Parse a buffer to determine if it contains a valid JSON document. | |
JSONStatus_t | JSON_SearchT (char *buf, size_t max, const char *query, size_t queryLength, char **outValue, size_t *outValueLength, JSONTypes_t *outType) |
Same as JSON_Search(), but also outputs a type for the value found. | |
JSONStatus_t | JSON_SearchConst (const char *buf, size_t max, const char *query, size_t queryLength, const char **outValue, size_t *outValueLength, JSONTypes_t *outType) |
Same as JSON_SearchT(), but with const qualified buf and outValue arguments. | |
JSONStatus_t | JSON_Iterate (const char *buf, size_t max, size_t *start, size_t *next, JSONPair_t *outPair) |
Output the next key-value pair or value from a collection. | |
Include this header file to use coreJSON in your application.
#define JSON_Search | ( | buf, | |
max, | |||
query, | |||
queryLength, | |||
outValue, | |||
outValueLength | |||
) | JSON_SearchT( buf, max, query, queryLength, outValue, outValueLength, NULL ) |
Find a key or array index in a JSON document and output the pointer outValue
to its value.
Any value may also be an object or an array to a maximum depth. A search may descend through nested objects or arrays when the query contains matching key strings or array indexes joined by a separator.
For example, if the provided buffer contains {"foo":"abc","bar":{"foo":"xyz"}}
, then a search for 'foo' would output abc
, 'bar' would output {"foo":"xyz"}
, and a search for 'bar.foo' would output xyz
.
If the provided buffer contains [123,456,{"foo":"abc","bar":[88,99]}]
, then a search for '[1]' would output 456
, '[2].foo' would output abc
, and '[2].bar[0]' would output 88
.
On success, the pointer outValue
points to a location in buf. No null termination is done for the value. For valid JSON it is safe to place a null character at the end of the value, so long as the character replaced is put back before running another search.
[in] | buf | The buffer to search. |
[in] | max | size of the buffer. |
[in] | query | The object keys and array indexes to search for. |
[in] | queryLength | Length of the key. |
[out] | outValue | A pointer to receive the address of the value found. |
[out] | outValueLength | A pointer to receive the length of the value found. |
Example
JSONStatus_t JSON_Validate | ( | const char * | buf, |
size_t | max | ||
) |
Parse a buffer to determine if it contains a valid JSON document.
[in] | buf | The buffer to parse. |
[in] | max | The size of the buffer. |
Example
See core_json.h for docs.
Verify that the entire buffer contains exactly one scalar or collection within optional whitespace.
JSONStatus_t JSON_SearchT | ( | char * | buf, |
size_t | max, | ||
const char * | query, | ||
size_t | queryLength, | ||
char ** | outValue, | ||
size_t * | outValueLength, | ||
JSONTypes_t * | outType | ||
) |
Same as JSON_Search(), but also outputs a type for the value found.
See JSON_Search for documentation of common behavior.
[in] | buf | The buffer to search. |
[in] | max | size of the buffer. |
[in] | query | The object keys and array indexes to search for. |
[in] | queryLength | Length of the key. |
[out] | outValue | A pointer to receive the address of the value found. |
[out] | outValueLength | A pointer to receive the length of the value found. |
[out] | outType | An enum indicating the JSON-specific type of the value. |
See core_json.h for docs.
JSONStatus_t JSON_SearchConst | ( | const char * | buf, |
size_t | max, | ||
const char * | query, | ||
size_t | queryLength, | ||
const char ** | outValue, | ||
size_t * | outValueLength, | ||
JSONTypes_t * | outType | ||
) |
Same as JSON_SearchT(), but with const qualified buf and outValue arguments.
See JSON_Search for documentation of common behavior.
[in] | buf | The buffer to search. |
[in] | max | size of the buffer. |
[in] | query | The object keys and array indexes to search for. |
[in] | queryLength | Length of the key. |
[out] | outValue | A pointer to receive the address of the value found. |
[out] | outValueLength | A pointer to receive the length of the value found. |
[out] | outType | An enum indicating the JSON-specific type of the value. |
See core_json.h for docs.
JSONStatus_t JSON_Iterate | ( | const char * | buf, |
size_t | max, | ||
size_t * | start, | ||
size_t * | next, | ||
JSONPair_t * | outPair | ||
) |
Output the next key-value pair or value from a collection.
This function may be used in a loop to output each key-value pair from an object, or each value from an array. For the first invocation, the integers pointed to by start and next should be initialized to 0. These will be updated by the function. If another key-value pair or value is present, the output structure is populated and JSONSuccess is returned; otherwise the structure is unchanged and JSONNotFound is returned.
[in] | buf | The buffer to search. |
[in] | max | size of the buffer. |
[in,out] | start | The index at which the collection begins. |
[in,out] | next | The index at which to seek the next value. |
[out] | outPair | A pointer to receive the next key-value pair. |
Example
See core_json.h for docs.