JSON5 parser/writer module

Contents

Easy to use and very fast JSON5 parser that can easily load 50 megabytes of JSON content under half a second. It also contains simple JSON5 writer and acts as a good library for handling config files. The parser also supports the Simplified JSON (SJSON) format.

We can parse JSON5 files in two different modes:
1) Fast way (useful for raw data), which can not handle comments and might cause parsing failure if comment is present.
2) Slower way (useful for config files), which handles comments perfectly but might have performance impact on bigger JSON files. (+50MiB)

Classes

struct zpl_json_object
JSON object definition.

Enums

enum zpl_json_type { ZPL_JSON_TYPE_OBJECT, ZPL_JSON_TYPE_STRING, ZPL_JSON_TYPE_MULTISTRING, ZPL_JSON_TYPE_ARRAY, ZPL_JSON_TYPE_INTEGER, ZPL_JSON_TYPE_REAL, ZPL_JSON_TYPE_CONSTANT }
JSON object types.
enum zpl_json_props { ZPL_JSON_PROPS_NONE = 0, ZPL_JSON_PROPS_NAN = 1, ZPL_JSON_PROPS_NAN_NEG = 2, ZPL_JSON_PROPS_INFINITY = 3, ZPL_JSON_PROPS_INFINITY_NEG = 4, ZPL_JSON_PROPS_IS_EXP = 5, ZPL_JSON_PROPS_IS_HEX = 6 }
Field value properties.
enum zpl_json_const { ZPL_JSON_CONST_FALSE, ZPL_JSON_CONST_TRUE, ZPL_JSON_CONST_NULL }
Value constants.
enum zpl_json_error { ZPL_JSON_ERROR_NONE, ZPL_JSON_ERROR_INVALID_NAME, ZPL_JSON_ERROR_INVALID_VALUE, ZPL_JSON_ERROR_OBJECT_OR_SOURCE_WAS_NULL }
Parser error types.
enum zpl_json_naming_style { ZPL_JSON_NAME_STYLE_DOUBLE_QUOTE, ZPL_JSON_NAME_STYLE_SINGLE_QUOTE, ZPL_JSON_NAME_STYLE_NO_QUOTES }
Field name decoration style.
enum zpl_json_assign_style { ZPL_JSON_ASSIGN_STYLE_COLON, ZPL_JSON_ASSIGN_STYLE_EQUALS, ZPL_JSON_ASSIGN_STYLE_LINE }
Field value assign style.
enum zpl_json_delim_style { ZPL_JSON_DELIM_STYLE_COMMA, ZPL_JSON_DELIM_STYLE_LINE, ZPL_JSON_DELIM_STYLE_NEWLINE }
Field delimiter style.

Typedefs

using zpl_json_type = enum zpl_json_type
JSON object types.
using zpl_json_props = enum zpl_json_props
Field value properties.
using zpl_json_const = enum zpl_json_const
Value constants.
using zpl_json_error = enum zpl_json_error
Parser error types.
using zpl_json_naming_style = enum zpl_json_naming_style
Field name decoration style.
using zpl_json_assign_style = enum zpl_json_assign_style
Field value assign style.
using zpl_json_delim_style = enum zpl_json_delim_style
Field delimiter style.
using zpl_json_object = struct zpl_json_object
JSON object definition.

Functions

auto zpl_json_parse(zpl_json_object* root, zpl_usize len, char const* source, zpl_allocator allocator, zpl_b32 handle_comments, zpl_u8* err_code) -> void
Parses JSON5/SJSON text.
auto zpl_json_write(zpl_file* file, zpl_json_object* o, zpl_isize indent) -> void
Exports JSON5 document into text form and outputs it into a file.
auto zpl_json_free(zpl_json_object* obj) -> void
Releases used resources by a JSON object.
auto zpl_json_find(zpl_json_object* obj, char const* name, zpl_b32 deep_search) -> zpl_json_object*
Searches for a JSON node within a document by its name.
auto zpl_json_init_node(zpl_json_object* obj, zpl_allocator backing, char const* name, zpl_u8 type) -> void
Initializes a JSON node.
auto zpl_json_add_at(zpl_json_object* obj, zpl_isize index, char const* name, zpl_u8 type) -> zpl_json_object*
Adds object into JSON document at a specific index.
auto zpl_json_add(zpl_json_object* obj, char const* name, zpl_u8 type) -> zpl_json_object*
Appends object into JSON document.

Defines

#define ZPL_JSON_ASSERT
Debug mode.
#define zpl_json_object

Function documentation

void zpl_json_parse(zpl_json_object* root, zpl_usize len, char const* source, zpl_allocator allocator, zpl_b32 handle_comments, zpl_u8* err_code)

Parses JSON5/SJSON text.

Parameters
root JSON object we store data to.
len Text length. (reserved)
source Text to be processed.
allocator Memory allocator to use. (ex. zpl_heap())
handle_comments Whether to handle possible comments or not. Note that if we won't handle comments in a document containing them, the parser will error out. See remark in source code.
err_code Variable we will store error code in.

This method takes text form of JSON document as a source and parses its contents into JSON object structure we can work with. It also optionally handles comments that usually appear in documents used for configuration.

void zpl_json_write(zpl_file* file, zpl_json_object* o, zpl_isize indent)

Exports JSON5 document into text form and outputs it into a file.

Parameters
file File we write text to.
o
indent Text indentation used during export. Use 0 for root objects.

This method takes JSON object tree and exports it into valid JSON5 form with the support of various styles that were preserved during import or set up programatically.

void zpl_json_free(zpl_json_object* obj)

Releases used resources by a JSON object.

Parameters
obj JSON object to free.

zpl_json_object* zpl_json_find(zpl_json_object* obj, char const* name, zpl_b32 deep_search)

Searches for a JSON node within a document by its name.

Parameters
obj JSON object to search in.
name JSON node's name to search for.
deep_search Perform the search recursively.

void zpl_json_init_node(zpl_json_object* obj, zpl_allocator backing, char const* name, zpl_u8 type)

Initializes a JSON node.

Parameters
obj JSON node to initialize.
backing Memory allocator to use (ex. zpl_heap())
name JSON node's name.
type JSON node's type. (See zpl_json_type)

zpl_json_object* zpl_json_add_at(zpl_json_object* obj, zpl_isize index, char const* name, zpl_u8 type)

Adds object into JSON document at a specific index.

Parameters
obj
index Index to store at.
name JSON node's name.
type JSON node's type. (See zpl_json_type)

Initializes and adds a JSON object into a JSON document at a specific index.

zpl_json_object* zpl_json_add(zpl_json_object* obj, char const* name, zpl_u8 type)

Appends object into JSON document.

Parameters
obj
name JSON node's name.
type JSON node's type. (See zpl_json_type)

Initializes and appends a JSON object into a JSON document.