Zephyr API Documentation
3.7.0
A Scalable Open Source RTOS
|
Modules | |
JSON Web Token (JWT) | |
JSON Web Token (JWT) | |
Data Structures | |
struct | json_token |
struct | json_lexer |
struct | json_obj |
struct | json_obj_token |
struct | json_obj_descr |
Macros | |
#define | JSON_OBJ_DESCR_PRIM(struct_, field_name_, type_) |
Helper macro to declare a descriptor for supported primitive values. | |
#define | JSON_OBJ_DESCR_OBJECT(struct_, field_name_, sub_descr_) |
Helper macro to declare a descriptor for an object value. | |
#define | JSON_OBJ_DESCR_ARRAY(struct_, field_name_, max_len_, len_field_, elem_type_) |
Helper macro to declare a descriptor for an array of primitives. | |
#define | JSON_OBJ_DESCR_OBJ_ARRAY(struct_, field_name_, max_len_, len_field_, elem_descr_, elem_descr_len_) |
Helper macro to declare a descriptor for an array of objects. | |
#define | JSON_OBJ_DESCR_ARRAY_ARRAY(struct_, field_name_, max_len_, len_field_, elem_descr_, elem_descr_len_) |
Helper macro to declare a descriptor for an array of array. | |
#define | JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, max_len_, len_field_, elem_descr_, elem_descr_len_) |
Variant of JSON_OBJ_DESCR_ARRAY_ARRAY that can be used when the structure and JSON field names differ. | |
#define | JSON_OBJ_DESCR_PRIM_NAMED(struct_, json_field_name_, struct_field_name_, type_) |
Variant of JSON_OBJ_DESCR_PRIM that can be used when the structure and JSON field names differ. | |
#define | JSON_OBJ_DESCR_OBJECT_NAMED(struct_, json_field_name_, struct_field_name_, sub_descr_) |
Variant of JSON_OBJ_DESCR_OBJECT that can be used when the structure and JSON field names differ. | |
#define | JSON_OBJ_DESCR_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, max_len_, len_field_, elem_type_) |
Variant of JSON_OBJ_DESCR_ARRAY that can be used when the structure and JSON field names differ. | |
#define | JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct_, json_field_name_, struct_field_name_, max_len_, len_field_, elem_descr_, elem_descr_len_) |
Variant of JSON_OBJ_DESCR_OBJ_ARRAY that can be used when the structure and JSON field names differ. | |
Typedefs | |
typedef int(* | json_append_bytes_t) (const char *bytes, size_t len, void *data) |
Function pointer type to append bytes to a buffer while encoding JSON data. | |
Enumerations | |
enum | json_tokens { JSON_TOK_NONE = '_' , JSON_TOK_OBJECT_START = '{' , JSON_TOK_OBJECT_END = '}' , JSON_TOK_ARRAY_START = '[' , JSON_TOK_ARRAY_END = ']' , JSON_TOK_STRING = '"' , JSON_TOK_COLON = ':' , JSON_TOK_COMMA = ',' , JSON_TOK_NUMBER = '0' , JSON_TOK_FLOAT = '1' , JSON_TOK_OPAQUE = '2' , JSON_TOK_OBJ_ARRAY = '3' , JSON_TOK_ENCODED_OBJ = '4' , JSON_TOK_TRUE = 't' , JSON_TOK_FALSE = 'f' , JSON_TOK_NULL = 'n' , JSON_TOK_ERROR = '!' , JSON_TOK_EOF = '\0' } |
Functions | |
int64_t | json_obj_parse (char *json, size_t len, const struct json_obj_descr *descr, size_t descr_len, void *val) |
Parses the JSON-encoded object pointed to by json, with size len, according to the descriptor pointed to by descr. | |
int | json_arr_parse (char *json, size_t len, const struct json_obj_descr *descr, void *val) |
Parses the JSON-encoded array pointed to by json, with size len, according to the descriptor pointed to by descr. | |
int | json_arr_separate_object_parse_init (struct json_obj *json, char *payload, size_t len) |
Initialize single-object array parsing. | |
int | json_arr_separate_parse_object (struct json_obj *json, const struct json_obj_descr *descr, size_t descr_len, void *val) |
Parse a single object from array. | |
ssize_t | json_escape (char *str, size_t *len, size_t buf_size) |
Escapes the string so it can be used to encode JSON objects. | |
size_t | json_calc_escaped_len (const char *str, size_t len) |
Calculates the JSON-escaped string length. | |
ssize_t | json_calc_encoded_len (const struct json_obj_descr *descr, size_t descr_len, const void *val) |
Calculates the string length to fully encode an object. | |
ssize_t | json_calc_encoded_arr_len (const struct json_obj_descr *descr, const void *val) |
Calculates the string length to fully encode an array. | |
int | json_obj_encode_buf (const struct json_obj_descr *descr, size_t descr_len, const void *val, char *buffer, size_t buf_size) |
Encodes an object in a contiguous memory location. | |
int | json_arr_encode_buf (const struct json_obj_descr *descr, const void *val, char *buffer, size_t buf_size) |
Encodes an array in a contiguous memory location. | |
int | json_obj_encode (const struct json_obj_descr *descr, size_t descr_len, const void *val, json_append_bytes_t append_bytes, void *data) |
Encodes an object using an arbitrary writer function. | |
int | json_arr_encode (const struct json_obj_descr *descr, const void *val, json_append_bytes_t append_bytes, void *data) |
Encodes an array using an arbitrary writer function. | |
#define JSON_OBJ_DESCR_ARRAY | ( | struct_, | |
field_name_, | |||
max_len_, | |||
len_field_, | |||
elem_type_ | |||
) |
#include <zephyr/data/json.h>
Helper macro to declare a descriptor for an array of primitives.
struct_ | Struct packing the values |
field_name_ | Field name in the struct |
max_len_ | Maximum number of elements in array |
len_field_ | Field name in the struct for the number of elements in the array |
elem_type_ | Element type, must be a primitive type |
Here's an example of use:
struct example { int32_t foo[10]; size_t foo_len; }; struct json_obj_descr array[] = { JSON_OBJ_DESCR_ARRAY(struct example, foo, 10, foo_len, JSON_TOK_NUMBER) };
#define JSON_OBJ_DESCR_ARRAY_ARRAY | ( | struct_, | |
field_name_, | |||
max_len_, | |||
len_field_, | |||
elem_descr_, | |||
elem_descr_len_ | |||
) |
#include <zephyr/data/json.h>
Helper macro to declare a descriptor for an array of array.
struct_ | Struct packing the values |
field_name_ | Field name in the struct containing the array |
max_len_ | Maximum number of elements in the array |
len_field_ | Field name in the struct for the number of elements in the array |
elem_descr_ | Element descriptor, pointer to a descriptor array |
elem_descr_len_ | Number of elements in elem_descr_ |
Here's an example of use:
struct person_height { const char *name; int32_t height; }; struct person_heights_array { struct person_height heights; } struct people_heights { struct person_height_array heights[10]; size_t heights_len; }; struct json_obj_descr person_height_descr[] = { JSON_OBJ_DESCR_PRIM(struct person_height, name, JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct person_height, height, JSON_TOK_NUMBER), }; struct json_obj_descr person_height_array_descr[] = { JSON_OBJ_DESCR_OBJECT(struct person_heights_array, heights, person_height_descr), }; struct json_obj_descr array_array[] = { JSON_OBJ_DESCR_ARRAY_ARRAY(struct people_heights, heights, 10, heights_len, person_height_array_descr, ARRAY_SIZE(person_height_array_descr)), };
#define JSON_OBJ_DESCR_ARRAY_ARRAY_NAMED | ( | struct_, | |
json_field_name_, | |||
struct_field_name_, | |||
max_len_, | |||
len_field_, | |||
elem_descr_, | |||
elem_descr_len_ | |||
) |
#include <zephyr/data/json.h>
Variant of JSON_OBJ_DESCR_ARRAY_ARRAY that can be used when the structure and JSON field names differ.
This is useful when the JSON field is not a valid C identifier.
struct_ | Struct packing the values |
json_field_name_ | String, field name in JSON strings |
struct_field_name_ | Field name in the struct containing the array |
max_len_ | Maximum number of elements in the array |
len_field_ | Field name in the struct for the number of elements in the array |
elem_descr_ | Element descriptor, pointer to a descriptor array |
elem_descr_len_ | Number of elements in elem_descr_ |
#define JSON_OBJ_DESCR_ARRAY_NAMED | ( | struct_, | |
json_field_name_, | |||
struct_field_name_, | |||
max_len_, | |||
len_field_, | |||
elem_type_ | |||
) |
#include <zephyr/data/json.h>
Variant of JSON_OBJ_DESCR_ARRAY that can be used when the structure and JSON field names differ.
This is useful when the JSON field is not a valid C identifier.
struct_ | Struct packing the values |
json_field_name_ | String, field name in JSON strings |
struct_field_name_ | Field name in the struct |
max_len_ | Maximum number of elements in array |
len_field_ | Field name in the struct for the number of elements in the array |
elem_type_ | Element type, must be a primitive type |
#define JSON_OBJ_DESCR_OBJ_ARRAY | ( | struct_, | |
field_name_, | |||
max_len_, | |||
len_field_, | |||
elem_descr_, | |||
elem_descr_len_ | |||
) |
#include <zephyr/data/json.h>
Helper macro to declare a descriptor for an array of objects.
struct_ | Struct packing the values |
field_name_ | Field name in the struct containing the array |
max_len_ | Maximum number of elements in the array |
len_field_ | Field name in the struct for the number of elements in the array |
elem_descr_ | Element descriptor, pointer to a descriptor array |
elem_descr_len_ | Number of elements in elem_descr_ |
Here's an example of use:
struct person_height { const char *name; int32_t height; }; struct people_heights { struct person_height heights[10]; size_t heights_len; }; struct json_obj_descr person_height_descr[] = { JSON_OBJ_DESCR_PRIM(struct person_height, name, JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct person_height, height, JSON_TOK_NUMBER), }; struct json_obj_descr array[] = { JSON_OBJ_DESCR_OBJ_ARRAY(struct people_heights, heights, 10, heights_len, person_height_descr, ARRAY_SIZE(person_height_descr)), };
#define JSON_OBJ_DESCR_OBJ_ARRAY_NAMED | ( | struct_, | |
json_field_name_, | |||
struct_field_name_, | |||
max_len_, | |||
len_field_, | |||
elem_descr_, | |||
elem_descr_len_ | |||
) |
#include <zephyr/data/json.h>
Variant of JSON_OBJ_DESCR_OBJ_ARRAY that can be used when the structure and JSON field names differ.
This is useful when the JSON field is not a valid C identifier.
struct_ | Struct packing the values |
json_field_name_ | String, field name of the array in JSON strings |
struct_field_name_ | Field name in the struct containing the array |
max_len_ | Maximum number of elements in the array |
len_field_ | Field name in the struct for the number of elements in the array |
elem_descr_ | Element descriptor, pointer to a descriptor array |
elem_descr_len_ | Number of elements in elem_descr_ |
Here's an example of use:
struct person_height { const char *name; int32_t height; }; struct people_heights { struct person_height heights[10]; size_t heights_len; }; struct json_obj_descr person_height_descr[] = { JSON_OBJ_DESCR_PRIM(struct person_height, name, JSON_TOK_STRING), JSON_OBJ_DESCR_PRIM(struct person_height, height, JSON_TOK_NUMBER), }; struct json_obj_descr array[] = { JSON_OBJ_DESCR_OBJ_ARRAY_NAMED(struct people_heights, "people-heights", heights, 10, heights_len, person_height_descr, ARRAY_SIZE(person_height_descr)), };
#define JSON_OBJ_DESCR_OBJECT | ( | struct_, | |
field_name_, | |||
sub_descr_ | |||
) |
#include <zephyr/data/json.h>
Helper macro to declare a descriptor for an object value.
struct_ | Struct packing the values |
field_name_ | Field name in the struct |
sub_descr_ | Array of json_obj_descr describing the subobject |
Here's an example of use:
struct nested { int32_t foo; struct { int32_t baz; } bar; }; struct json_obj_descr nested_bar[] = { { ... declare bar.baz descriptor ... }, }; struct json_obj_descr nested[] = { { ... declare foo descriptor ... }, JSON_OBJ_DESCR_OBJECT(struct nested, bar, nested_bar), };
#define JSON_OBJ_DESCR_OBJECT_NAMED | ( | struct_, | |
json_field_name_, | |||
struct_field_name_, | |||
sub_descr_ | |||
) |
#include <zephyr/data/json.h>
Variant of JSON_OBJ_DESCR_OBJECT that can be used when the structure and JSON field names differ.
This is useful when the JSON field is not a valid C identifier.
struct_ | Struct packing the values |
json_field_name_ | String, field name in JSON strings |
struct_field_name_ | Field name in the struct |
sub_descr_ | Array of json_obj_descr describing the subobject |
#define JSON_OBJ_DESCR_PRIM | ( | struct_, | |
field_name_, | |||
type_ | |||
) |
#include <zephyr/data/json.h>
Helper macro to declare a descriptor for supported primitive values.
struct_ | Struct packing the values |
field_name_ | Field name in the struct |
type_ | Token type for JSON value corresponding to a primitive type. Must be one of: JSON_TOK_STRING for strings, JSON_TOK_NUMBER for numbers, JSON_TOK_TRUE (or JSON_TOK_FALSE) for booleans. |
Here's an example of use:
struct foo { int32_t some_int; }; struct json_obj_descr foo[] = { JSON_OBJ_DESCR_PRIM(struct foo, some_int, JSON_TOK_NUMBER), };
#define JSON_OBJ_DESCR_PRIM_NAMED | ( | struct_, | |
json_field_name_, | |||
struct_field_name_, | |||
type_ | |||
) |
#include <zephyr/data/json.h>
Variant of JSON_OBJ_DESCR_PRIM that can be used when the structure and JSON field names differ.
This is useful when the JSON field is not a valid C identifier.
struct_ | Struct packing the values. |
json_field_name_ | String, field name in JSON strings |
struct_field_name_ | Field name in the struct |
type_ | Token type for JSON value corresponding to a primitive type. |
typedef int(* json_append_bytes_t) (const char *bytes, size_t len, void *data) |
#include <zephyr/data/json.h>
Function pointer type to append bytes to a buffer while encoding JSON data.
bytes | Contents to write to the output |
len | Number of bytes to append to output |
data | User-provided pointer |
enum json_tokens |
#include <zephyr/data/json.h>
int json_arr_encode | ( | const struct json_obj_descr * | descr, |
const void * | val, | ||
json_append_bytes_t | append_bytes, | ||
void * | data | ||
) |
#include <zephyr/data/json.h>
Encodes an array using an arbitrary writer function.
descr | Pointer to the descriptor array |
val | Struct holding the values |
append_bytes | Function to append bytes to the output |
data | Data pointer to be passed to the append_bytes callback function. |
int json_arr_encode_buf | ( | const struct json_obj_descr * | descr, |
const void * | val, | ||
char * | buffer, | ||
size_t | buf_size | ||
) |
#include <zephyr/data/json.h>
Encodes an array in a contiguous memory location.
descr | Pointer to the descriptor array |
val | Struct holding the values |
buffer | Buffer to store the JSON data |
buf_size | Size of buffer, in bytes, with space for the terminating NUL character |
int json_arr_parse | ( | char * | json, |
size_t | len, | ||
const struct json_obj_descr * | descr, | ||
void * | val | ||
) |
#include <zephyr/data/json.h>
Parses the JSON-encoded array pointed to by json, with size len, according to the descriptor pointed to by descr.
Values are stored in a struct pointed to by val. Set up the descriptor like this:
struct s { int32_t foo; char *bar; } struct json_obj_descr descr[] = { JSON_OBJ_DESCR_PRIM(struct s, foo, JSON_TOK_NUMBER), JSON_OBJ_DESCR_PRIM(struct s, bar, JSON_TOK_STRING), }; struct a { struct s baz[10]; size_t count; } struct json_obj_descr array[] = { JSON_OBJ_DESCR_OBJ_ARRAY(struct a, baz, 10, count, descr, ARRAY_SIZE(descr)), };
Since this parser is designed for machine-to-machine communications, some liberties were taken to simplify the design: (1) strings are not unescaped (but only valid escape sequences are accepted); (2) no UTF-8 validation is performed; and (3) only integer numbers are supported (no strtod() in the minimal libc).
json | Pointer to JSON-encoded array to be parsed |
len | Length of JSON-encoded array |
descr | Pointer to the descriptor array |
val | Pointer to the struct to hold the decoded values |
#include <zephyr/data/json.h>
Initialize single-object array parsing.
JSON-encoded array data is going to be parsed one object at a time. Data is provided by payload with the size of len bytes.
Function validate that Json Array start is detected and initialize json object for Json object parsing separately.
json | Provide storage for parser states. To be used when parsing the array. |
payload | Pointer to JSON-encoded array to be parsed |
len | Length of JSON-encoded array |
int json_arr_separate_parse_object | ( | struct json_obj * | json, |
const struct json_obj_descr * | descr, | ||
size_t | descr_len, | ||
void * | val | ||
) |
#include <zephyr/data/json.h>
Parse a single object from array.
Parses the JSON-encoded object pointed to by json object array, with size len, according to the descriptor pointed to by descr.
json | Pointer to JSON-object message state |
descr | Pointer to the descriptor array |
descr_len | Number of elements in the descriptor array. Must be less than 31. |
val | Pointer to the struct to hold the decoded values |
ssize_t json_calc_encoded_arr_len | ( | const struct json_obj_descr * | descr, |
const void * | val | ||
) |
#include <zephyr/data/json.h>
Calculates the string length to fully encode an array.
descr | Pointer to the descriptor array |
val | Struct holding the values |
ssize_t json_calc_encoded_len | ( | const struct json_obj_descr * | descr, |
size_t | descr_len, | ||
const void * | val | ||
) |
#include <zephyr/data/json.h>
Calculates the string length to fully encode an object.
descr | Pointer to the descriptor array |
descr_len | Number of elements in the descriptor array |
val | Struct holding the values |
#include <zephyr/data/json.h>
Calculates the JSON-escaped string length.
str | The string to analyze |
len | String size |
#include <zephyr/data/json.h>
Escapes the string so it can be used to encode JSON objects.
str | The string to escape; the escape string is stored the buffer pointed to by this parameter |
len | Points to a size_t containing the size before and after the escaping process |
buf_size | The size of buffer str points to |
int json_obj_encode | ( | const struct json_obj_descr * | descr, |
size_t | descr_len, | ||
const void * | val, | ||
json_append_bytes_t | append_bytes, | ||
void * | data | ||
) |
#include <zephyr/data/json.h>
Encodes an object using an arbitrary writer function.
descr | Pointer to the descriptor array |
descr_len | Number of elements in the descriptor array |
val | Struct holding the values |
append_bytes | Function to append bytes to the output |
data | Data pointer to be passed to the append_bytes callback function. |
int json_obj_encode_buf | ( | const struct json_obj_descr * | descr, |
size_t | descr_len, | ||
const void * | val, | ||
char * | buffer, | ||
size_t | buf_size | ||
) |
#include <zephyr/data/json.h>
Encodes an object in a contiguous memory location.
descr | Pointer to the descriptor array |
descr_len | Number of elements in the descriptor array |
val | Struct holding the values |
buffer | Buffer to store the JSON data |
buf_size | Size of buffer, in bytes, with space for the terminating NUL character |
int64_t json_obj_parse | ( | char * | json, |
size_t | len, | ||
const struct json_obj_descr * | descr, | ||
size_t | descr_len, | ||
void * | val | ||
) |
#include <zephyr/data/json.h>
Parses the JSON-encoded object pointed to by json, with size len, according to the descriptor pointed to by descr.
Values are stored in a struct pointed to by val. Set up the descriptor like this:
struct s { int32_t foo; char *bar; } struct json_obj_descr descr[] = { JSON_OBJ_DESCR_PRIM(struct s, foo, JSON_TOK_NUMBER), JSON_OBJ_DESCR_PRIM(struct s, bar, JSON_TOK_STRING), };
Since this parser is designed for machine-to-machine communications, some liberties were taken to simplify the design: (1) strings are not unescaped (but only valid escape sequences are accepted); (2) no UTF-8 validation is performed; and (3) only integer numbers are supported (no strtod() in the minimal libc).
json | Pointer to JSON-encoded value to be parsed |
len | Length of JSON-encoded value |
descr | Pointer to the descriptor array |
descr_len | Number of elements in the descriptor array. Must be less than 63 due to implementation detail reasons (if more fields are necessary, use two descriptors) |
val | Pointer to the struct to hold the decoded values |