JSON¶
JSON is a data interchange format. The description of this format can be found from IETF RFC 4627.
Description¶
This package helps in converting between C data types and JSON data objects. It supports both encoding and decoding.
Data structures¶
Encoding¶
/* Encoding functions */
typedef int (*json_write_func_t)(void *buf, char *data,
int len);
struct json_encoder {
json_write_func_t je_write;
void *je_arg;
int je_wr_commas:1;
char je_encode_buf[64];
};
Here’s the data structure encoder funtions use, and it must be initialized by the caller. The key element is je_write, which is a function pointer which gets called whenever encoding routine is ready with encoded data. The element je_arg is passed to je_write as the first argument. The rest of the structure contents are for internal state management. This function should collect all the data encoder function generates. It can collect this data to a flat buffer, chain of mbufs or even stream through.
/**
* For encode. The contents of a JSON value to encode.
*/
struct json_value {
uint8_t jv_pad1;
uint8_t jv_type;
uint16_t jv_len;
union {
uint64_t u;
float fl;
char *str;
struct {
char **keys;
struct json_value **values;
} composite;
} jv_val;
};
This data structure is filled with data to be encoded. It is best to fill this using the macros JSON_VALUE_STRING() or JSON_VALUE_STRINGN() when value is string, JSON_VALUE_INT() when value is an integer, and so forth.
Decoding¶
/* when you implement a json buffer, you must implement these functions */
/* returns the next character in the buffer or '\0'*/
typedef char (*json_buffer_read_next_byte_t)(struct json_buffer *);
/* returns the previous character in the buffer or '\0' */
typedef char (*json_buffer_read_prev_byte_t)(struct json_buffer *);
/* returns the number of characters read or zero */
typedef int (*json_buffer_readn_t)(struct json_buffer *, char *buf, int n);
struct json_buffer {
json_buffer_readn_t jb_readn;
json_buffer_read_next_byte_t jb_read_next;
json_buffer_read_prev_byte_t jb_read_prev;
};
Function pointers within this structure are used by decoder when it is reading in more data to decode.
struct json_attr_t {
char *attribute;
json_type type;
union {
int *integer;
unsigned int *uinteger;
double *real;
char *string;
bool *boolean;
char *character;
struct json_array_t array;
size_t offset;
} addr;
union {
int integer;
unsigned int uinteger;
double real;
bool boolean;
char character;
char *check;
} dflt;
size_t len;
const struct json_enum_t *map;
bool nodefault;
};
This structure tells the decoder about a particular name/value pair. Structure must be filled in before calling the decoder routine json_read_object().
Element |
Description |
---|---|
attribute |
Name of the value |
type |
The type of the variable; see enum json_type |
addr |
Contains the address where value should be stored |
dflt |
Default value to fill in, if this name is not found |
len |
Max number of bytes to read in for value |
nodefault |
If set, default value is not copied name |
API¶
Defines
-
JSON_VALUE_TYPE_BOOL
¶
-
JSON_VALUE_TYPE_UINT64
¶
-
JSON_VALUE_TYPE_INT64
¶
-
JSON_VALUE_TYPE_STRING
¶
-
JSON_VALUE_TYPE_ARRAY
¶
-
JSON_VALUE_TYPE_OBJECT
¶
-
JSON_VALUE_STRING
(__jv, __str)¶
-
JSON_VALUE_STRINGN
(__jv, __str, __len)¶
-
JSON_VALUE_BOOL
(__jv, __v)¶
-
JSON_VALUE_INT
(__jv, __v)¶
-
JSON_VALUE_UINT
(__jv, __v)¶
-
JSON_NITEMS
(x)¶
-
JSON_ATTR_MAX
¶
-
JSON_VAL_MAX
¶
-
JSON_ERR_OBSTART
¶
-
JSON_ERR_ATTRSTART
¶
-
JSON_ERR_BADATTR
¶
-
JSON_ERR_ATTRLEN
¶
-
JSON_ERR_NOARRAY
¶
-
JSON_ERR_NOBRAK
¶
-
JSON_ERR_STRLONG
¶
-
JSON_ERR_TOKLONG
¶
-
JSON_ERR_BADTRAIL
¶
-
JSON_ERR_ARRAYSTART
¶
-
JSON_ERR_OBJARR
¶
-
JSON_ERR_SUBTOOLONG
¶
-
JSON_ERR_BADSUBTRAIL
¶
-
JSON_ERR_SUBTYPE
¶
-
JSON_ERR_BADSTRING
¶
-
JSON_ERR_CHECKFAIL
¶
-
JSON_ERR_NOPARSTR
¶
-
JSON_ERR_BADENUM
¶
-
JSON_ERR_QNONSTRING
¶
-
JSON_ERR_NONQSTRING
¶
-
JSON_ERR_MISC
¶
-
JSON_ERR_BADNUM
¶
-
JSON_ERR_NULLPTR
¶
-
JSON_STRUCT_OBJECT
(s, f)¶
-
JSON_STRUCT_ARRAY
(a, e, n)¶
Typedefs
-
typedef int (*
json_write_func_t
)(void *buf, char *data, int len)¶
-
typedef char (*
json_buffer_read_next_byte_t
)(struct json_buffer*)¶
-
typedef char (*
json_buffer_read_prev_byte_t
)(struct json_buffer*)¶
-
typedef int (*
json_buffer_readn_t
)(struct json_buffer*, char *buf, int n)¶
Enums
Functions
-
int
json_encode_object_start
(struct json_encoder*)¶
-
int
json_encode_object_key
(struct json_encoder *encoder, char *key)¶
-
int
json_encode_object_entry
(struct json_encoder*, char*, struct json_value*)¶
-
int
json_encode_object_finish
(struct json_encoder*)¶
-
int
json_encode_array_name
(struct json_encoder *encoder, char *name)¶
-
int
json_encode_array_start
(struct json_encoder *encoder)¶
-
int
json_encode_array_value
(struct json_encoder *encoder, struct json_value *val)¶
-
int
json_encode_array_finish
(struct json_encoder *encoder)¶
-
int
json_read_object
(struct json_buffer*, const struct json_attr_t*)¶
-
int
json_read_array
(struct json_buffer*, const struct json_array_t*)¶
-
struct
json_value
¶ - #include <json.h>
Public Members
-
uint8_t
jv_pad1
¶
-
uint8_t
jv_type
¶
-
uint16_t
jv_len
¶
-
union json_value.[anonymous]
jv_val
¶
-
uint8_t
-
struct
json_encoder
¶ - #include <json.h>
-
struct
json_enum_t
¶ - #include <json.h>
-
struct
json_array_t
¶ - #include <json.h>
-
struct
json_attr_t
¶ - #include <json.h>
Public Members
-
char *
attribute
¶
-
union json_attr_t.[anonymous]
addr
¶
-
union json_attr_t.[anonymous]
dflt
¶
-
size_t
len
¶
-
const struct json_enum_t *
map
¶
-
bool
nodefault
¶
-
char *
-
struct
json_buffer
¶ - #include <json.h>
Public Members
-
json_buffer_readn_t
jb_readn
¶
-
json_buffer_read_next_byte_t
jb_read_next
¶
-
json_buffer_read_prev_byte_t
jb_read_prev
¶
-
json_buffer_readn_t
-
union
json_value
.
jv_val
Public Members
-
uint64_t
u
¶
-
float
fl
¶
-
char *
str
¶
-
struct json_value.[anonymous].[anonymous]
composite
¶
-
uint64_t
-
struct
json_value.jv_val
.
composite
-
union
json_array_t
.
arr
Public Members
-
struct json_array_t.[anonymous].[anonymous]
objects
¶
-
struct json_array_t.[anonymous].[anonymous]
strings
¶
-
struct json_array_t.[anonymous].[anonymous]
integers
¶
-
struct json_array_t.[anonymous].[anonymous]
uintegers
¶
-
struct json_array_t.[anonymous].[anonymous]
reals
¶
-
struct json_array_t.[anonymous].[anonymous]
booleans
¶
-
struct json_array_t.[anonymous].[anonymous]
-
struct
json_array_t.arr
.
objects
-
struct
json_array_t.arr
.
strings
-
struct
json_array_t.arr
.
integers
Public Members
-
long long int *
store
¶
-
long long int *
-
struct
json_array_t.arr
.
uintegers
Public Members
-
long long unsigned int * store
-
-
struct
json_array_t.arr
.
reals
Public Members
-
double *
store
¶
-
double *
-
struct
json_array_t.arr
.
booleans
Public Members
-
bool *
store
¶
-
bool *
-
union
json_attr_t
.
addr
-
union
json_attr_t
.
dflt