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

enum json_type

Values:

enumerator t_integer
enumerator t_uinteger
enumerator t_real
enumerator t_string
enumerator t_boolean
enumerator t_character
enumerator t_object
enumerator t_structobject
enumerator t_array
enumerator t_check
enumerator t_ignore

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
struct json_encoder
#include <json.h>

Public Members

json_write_func_t je_write
void *je_arg
unsigned int je_wr_commas
char je_encode_buf[64]
struct json_enum_t
#include <json.h>

Public Members

char *name
long long int value
struct json_array_t
#include <json.h>

Public Members

json_type element_type
union json_array_t.[anonymous] arr
int *count
int maxlen
struct json_attr_t
#include <json.h>

Public Members

char *attribute
json_type type
union json_attr_t.[anonymous] addr
union json_attr_t.[anonymous] dflt
size_t len
const struct json_enum_t *map
bool nodefault
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
union json_value.jv_val

Public Members

uint64_t u
float fl
char *str
struct json_value.[anonymous].[anonymous] composite
struct json_value.jv_val.composite

Public Members

char **keys
struct json_value **values
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.arr.objects

Public Members

const struct json_attr_t *subtype
char *base
size_t stride
struct json_array_t.arr.strings

Public Members

char **ptrs
char *store
int storelen
struct json_array_t.arr.integers

Public Members

long long int *store
struct json_array_t.arr.uintegers

Public Members

long long unsigned int * store
struct json_array_t.arr.reals

Public Members

double *store
struct json_array_t.arr.booleans

Public Members

bool *store
union json_attr_t.addr

Public Members

long long int *integer
long long unsigned int * uinteger
double *real
char *string
bool *boolean
char *character
struct json_array_t array
size_t offset
union json_attr_t.dflt

Public Members

long long int integer
long long unsigned int uinteger
double real
bool boolean
char character
char *check