GitHub - cesanta/frozen: JSON parser and generator for C/C++ with scanf/printf like interface. Targeting embedded systems. (original) (raw)

JSON parser and emitter for C/C++

Features

API reference

json_scanf(), json_vscanf

int json_scanf(const char *str, int str_len, const char *fmt, ...); int json_vscanf(const char *str, int str_len, const char *fmt, va_list ap);

/* json_scanf's %M handler */ typedef void (*json_scanner_t)(const char *str, int len, void *user_data);

Scans the JSON string str, performing scanf-like conversions according to fmt.fmt uses scanf()-like format, with the following differences:

  1. Object keys in the format string don't have to be quoted, e.g. "{key: %d}"
  2. Order of keys in the format string does not matter, and the format string may omit keys to fetch only those that are of interest, for example, assume str is a JSON string { "a": 123, "b": "hi", c: true }. We can fetch only the value of the c key:
    int value = 0;
    json_scanf(str, strlen(str), "{c: %B}", &value);
  3. Several extra format specifiers are supported:
    • %B: consumes int * (or char *, if sizeof(bool) == sizeof(char)), expects boolean true or false.
    • %Q: consumes char **, expects quoted, JSON-encoded string. Scanned string is malloc-ed, caller must free() the string.
    • %V: consumes char **, int *. Expects base64-encoded string. Result string is base64-decoded, malloced and NUL-terminated. The length of result string is stored in int * placeholder. Caller must free() the result.
    • %H: consumes int *, char **. Expects a hex-encoded string, e.g. "fa014f". Result string is hex-decoded, malloced and NUL-terminated. The length of the result string is stored in int * placeholder. Caller must free() the result.
    • %M: consumes custom scanning function pointer andvoid *user_data parameter - see json_scanner_t definition.
    • %T: consumes struct json_token *, fills it out with matched token.

Returns the number of elements successfully scanned & converted. Negative number means scan error.

Example - scan arbitrary JSON string:

// str has the following JSON string (notice keys are out of order): // { "a": 123, "d": true, "b": [1, 2], "c": "hi" }

int a = 0, d = 0; char *c = NULL; void *my_data = NULL; json_scanf(str, strlen(str), "{ a:%d, b:%M, c:%Q, d:%B }", &a, scan_array, my_data, &c, &d);

// This function is called by json_scanf() call above. // str is "[1, 2]", user_data is my_data. static void scan_array(const char *str, int len, void *user_data) { struct json_token t; int i; printf("Parsing array: %.*s\n", len, str); for (i = 0; json_scanf_array_elem(str, len, "", i, &t) > 0; i++) { printf("Index %d, token [%.*s]\n", i, t.len, t.ptr); } }

Example - parse array of objects:

// str has the following JSON string - array of objects: // { "a": [ {"b": 123}, {"b": 345} ] } // This example shows how to iterate over array, and parse each object.

int i, value, len = strlen(str); struct json_token t;

for (i = 0; json_scanf_array_elem(str, len, ".a", i, &t) > 0; i++) { // t.type == JSON_TYPE_OBJECT json_scanf(t.ptr, t.len, "{b: %d}", &value); // value is 123, then 345 }

json_scanf_array_elem()

int json_scanf_array_elem(const char *s, int len, const char *path, int index, struct json_token *token);

A helper function to scan an array item with given path and index. Fills token with the matched JSON token. Returns -1 if no array element found, otherwise non-negative token length.

json_printf()

Frozen printing API is pluggable. Out of the box, Frozen provides a way to print to a string buffer or to an opened file stream. It is easy to tell Frozen to print to another destination, for example, to a socket, etc. Frozen does this by defining an "output context" descriptor which has a pointer to a low-level printing function. If you want to print to another destination, just define your specific printing function and initialise output context with it.

This is the definition of the output context descriptor:

struct json_out { int (*printer)(struct json_out *, const char *str, size_t len); union { struct { char *buf; size_t size; size_t len; } buf; void *data; FILE *fp; } u; };

Frozen provides two helper macros to initialise two built-in output descriptors:

struct json_out out1 = JSON_OUT_BUF(buf, len); struct json_out out2 = JSON_OUT_FILE(fp);

typedef int (*json_printf_callback_t)(struct json_out *, va_list *ap); int json_printf(struct json_out *, const char *fmt, ...); int json_vprintf(struct json_out *, const char *fmt, va_list ap);

Generate formatted output into a given string buffer, auto-escaping keys. This is a superset of printf() function, with extra format specifiers:

Return number of bytes printed. If the return value is bigger than the supplied buffer, that is an indicator of overflow. In the overflow case, overflown bytes are not printed.

Example:

json_printf(&out, "{%Q: %d, x: [%B, %B], y: %Q}", "foo", 123, 0, -1, "hi"); // Result: // {"foo": 123, "x": [false, true], "y": "hi"}

To print a complex object (for example, serialise a structure into an object), use %M format specifier:

struct my_struct { int a, b; } mys = {1,2}; json_printf(&out, "{foo: %M, bar: %d}", print_my_struct, &mys, 3); // Result: // {"foo": {"a": 1, "b": 2}, "bar": 3}

int print_my_struct(struct json_out *out, va_list *ap) { struct my_struct *p = va_arg(*ap, struct my_struct *); return json_printf(out, "{a: %d, b: %d}", p->a, p->b); }

json_printf_array()

int json_printf_array(struct json_out *, va_list *ap);

A helper %M callback that prints contiguous C arrays. Consumes void *array_ptr, size_t array_size, size_t elem_size, char *fmtReturns number of bytes printed.

json_walk() - low level parsing API

/* JSON token type / enum json_token_type { JSON_TYPE_INVALID = 0, / memsetting to 0 should create INVALID value */ JSON_TYPE_STRING, JSON_TYPE_NUMBER, JSON_TYPE_TRUE, JSON_TYPE_FALSE, JSON_TYPE_NULL, JSON_TYPE_OBJECT_START, JSON_TYPE_OBJECT_END, JSON_TYPE_ARRAY_START, JSON_TYPE_ARRAY_END,

JSON_TYPES_CNT, };

/*

/* Callback-based API */ typedef void (*json_walk_callback_t)(void *callback_data, const char *name, size_t name_len, const char *path, const struct json_token *token);

/*

json_walk() is a low-level, callback based parsing API.json_walk() calls a given callback function for each scanned value.

Callback receives a name, a path to the value, a JSON token that points to the value and an arbitrary user data pointer.

The path is constructed using this rule:

For example, consider the following json string:{ "foo": 123, "bar": [ 1, 2, { "baz": true } ] }. The sequence of callback invocations will be as follows:

If top-level element is an array: [1, {"foo": 2}]

If top-level element is a scalar: true

json_walk_args() - low level parsing API extensible interface

This function is identical to json_walk() except that it takes a struct pointer argument for the callback and callback_dataarguments and additional configuration elements:

struct frozen_args {
  json_walk_callback_t callback;
  void *callback_data;
  int limit;
};

This struct must be initialized using INIT_FROZEN_ARGS() to retain forward compatibility before any members are set as illustrated here:

struct frozen_args args[1];

INIT_FROZEN_ARGS(args);

args->callback = mycb;
args->callback_data = data;
args->limit = 20;

ret = json_walk_args(string, len, args);

the limit member of struct frozen_args can be set to limit the maximum recursion depth to prevent possible stack overflows and limit parsing complexity.

json_fprintf(), json_vfprintf()

/*

json_asprintf(), json_vasprintf()

/*

*/ char *json_asprintf(const char *fmt, ...); char *json_vasprintf(const char *fmt, va_list ap);

json_fread()

/*

json_setf(), json_vsetf()

/*

int json_vsetf(const char *s, int len, struct json_out *out, const char *json_path, const char *json_fmt, va_list ap);

json_prettify()

/*

json_prettify_file()

/*

json_next_key(), json_next_elem()

/*

*/ void *json_next_key(const char *s, int len, void *handle, const char *path, struct json_token *key, struct json_token *val);

/*

Minimal mode

By building with -DJSON_MINIMAL=1 footprint can be significantly reduced. The following limitations apply in this configuration:

Examples

json_fprintf("settings.json", "{ a: %d, b: %Q }", 123, "string_value"); json_prettify_file("settings.json"); // Optional

Read JSON configuration from a file

struct my_config { int a; char *b; } c = { .a = 0, .b = NULL }; char *content = json_fread("settings.json"); json_scanf(content, strlen(content), "{a: %d, b: %Q}", &c.a, &c.b);

Modify configuration setting in a JSON file

const char *settings_file_name = "settings.json", *tmp_file_name = "tmp.json"; char *content = json_fread(settings_file_name); FILE *fp = fopen(tmp_file_name, "w"); struct json_out out = JSON_OUT_FILE(fp); json_setf(content, strlen(content), &out, ".b", "%Q", "new_string_value"); fclose(fp); json_prettify_file(tmp_file_name); // Optional rename(tmp_file_name, settings_file_name);

Contributions

To submit contributions, signCesanta CLAand send GitHub pull request.

Licensing

Frozen is released under theApache 2.0 license.

For commercial support and professional services,contact us.

See also