JSON encoder/decoder in a single C file.

xJSON

xJSON is a lightweight library that implements a JSON encoder, decoder and other utility functions.

Usage

To use xJSON, just add xjson.c and xjson.h to your files, then include xjson.h where you want to use it and compile xjson.c with your other files.

Overview

The main functions implemented by xJSON are

xj_value *xj_decode(const char *str, int len, 
                    xj_alloc *alloc, xj_error *error);

char *xj_encode(xj_value *value, int *len);

which let you transform a JSON-encoded UTF-8 string to an xj_value and transform an xj_value to a string.

Object model

The xj_value structure represents a generic JSON value. It's definition is made public, so that you can access it directly to read and modify it

enum {
    XJ_NULL, XJ_BOOL, XJ_INT, XJ_FLOAT,
    XJ_ARRAY, XJ_OBJECT, XJ_STRING,
};

typedef struct xj_value xj_value;
struct xj_value {
    int       type;
    int       size;
    xj_value *next;
    char     *key;
    union {
        xj_i64    as_int;
        xj_bool   as_bool;
        xj_f64    as_float;
        xj_value *as_array;
        xj_value *as_object;
        char     *as_string;
    };
};

objects and arrays are represented as linked lists of xj_values.

Although the user can make as many xj_value nodes as he wants, some constructor functions are also provided

xj_value *xj_value_null(xj_alloc *alloc, xj_error *error);

xj_value *xj_value_bool (xj_bool val, xj_alloc *alloc, xj_error *error);
xj_value *xj_value_int  (xj_i64  val, xj_alloc *alloc, xj_error *error);
xj_value *xj_value_float(xj_f64  val, xj_alloc *alloc, xj_error *error);

xj_value *xj_value_array (xj_value *head, xj_alloc *alloc, xj_error *error);
xj_value *xj_value_object(xj_value *head, xj_alloc *alloc, xj_error *error);

xj_value *xj_value_string(const char *str, int len, xj_alloc *alloc, xj_error *error);

Error handling

You may have noticed many functions require you to specify a xj_error object. Whenever an error occurres, the error structure is used to inform the caller of the context of the failure. This is optional, so whenever a function expects an error pointer, you can provide a NULL.

The structure is public and defined as following

typedef struct {
    xj_bool occurred;
    xj_bool truncated;
    int off, row, col;
    char message[128];
} xj_error;

when an error occurres you can read it's fields directly.

Memory management

Many JSON libraries handle memory using reference counting. xJSON uses a different approach where all nodes are stored in a single memory pool and then freed up at the same time. Assuming most objects have the same lifetime, this makes it both faster and easier to manage many objects.

An allocator is instanciated using one of

xj_alloc *xj_alloc_using(void *mem, int size, int ext, void (*free)(void*));
xj_alloc *xj_alloc_new(int size, int ext);

the first lets you specify the memory that the allocator will use to operate, while the second tells the allocator to call malloc to get the memory he needs.

When an xj_alloc is instanciated, you can call all of the functions that require you to provide an xj_alloc*. The objects that those functions return will be stored in the allocator. You deallocate all of the nodes by freeing up the whole allocator using

void xj_alloc_del(xj_alloc *alloc);
Owner
Francesco Cozzuto
Just an italian programmer. Trying to build the best software I can.
Francesco Cozzuto
Similar Resources

json2cpp is compiles a json file into static constexpr data structures that can be used at compile time or runtime

json2cpp json2cpp is compiles a json file into static constexpr data structures that can be used at compile time or runtime. Features Literally 0 runt

May 14, 2022

A C++17 single-file header-only library to wrap RapidJSON SAX interface

A C++17 single-file header-only library for RapidJSON SAX interface

Mar 24, 2022

Ultralightweight JSON parser in ANSI C

cJSON Ultralightweight JSON parser in ANSI C. Table of contents License Usage Welcome to cJSON Building Copying the source CMake Makefile Vcpkg Includ

May 10, 2022

JSON parser and generator for C/C++ with scanf/printf like interface. Targeting embedded systems.

JSON parser and emitter for C/C++ Features ISO C and ISO C++ compliant portable code Very small footprint No dependencies json_scanf() scans a string

May 12, 2022

C library for encoding, decoding and manipulating JSON data

Jansson README Jansson is a C library for encoding, decoding and manipulating JSON data. Its main features and design principles are: Simple and intui

May 7, 2022

JSON & BSON parser/writer

jbson is a library for building & iterating BSON data, and JSON documents in C++14. \tableofcontents Features # {#features} Header only. Boost license

Feb 18, 2022

A very sane (header only) C++14 JSON library

JeayeSON - a very sane C++14 JSON library JeayeSON was designed out of frustration that there aren't many template-based approaches to handling JSON i

May 12, 2022

Jsmn is a world fastest JSON parser/tokenizer. This is the official repo replacing the old one at Bitbucket

JSMN jsmn (pronounced like 'jasmine') is a minimalistic JSON parser in C. It can be easily integrated into resource-limited or embedded projects. You

May 17, 2022

JSON for Modern C++

JSON for Modern C++

Design goals Sponsors Integration CMake Package Managers Pkg-config Examples JSON as first-class data type Serialization / Deserialization STL-like ac

May 19, 2022
Related tags
A C++, header-only library for constructing JSON and JSON-like data formats, with JSON Pointer, JSON Patch, JSON Schema, JSONPath, JMESPath, CSV, MessagePack, CBOR, BSON, UBJSON

JSONCONS jsoncons is a C++, header-only library for constructing JSON and JSON-like data formats such as CBOR. For each supported data format, it enab

May 9, 2022
json-cpp is a C++11 JSON serialization library.

JSON parser and generator for C++ Version 0.1 alpha json-cpp is a C++11 JSON serialization library. Example #include <json-cpp.hpp> struct Foo {

Dec 31, 2019
This is a JSON C++ library. It can write and read JSON files with ease and speed.

Json Box JSON (JavaScript Object Notation) is a lightweight data-interchange format. Json Box is a C++ library used to read and write JSON with ease a

Apr 24, 2022
A convenience C++ wrapper library for JSON-Glib providing friendly syntactic sugar for parsing JSON

This library is a wrapper for the json-glib library that aims to provide the user with a trivial alternative API to the API provided by the base json-

May 10, 2022
json-build is a zero-allocation JSON serializer compatible with C89

json-build is a zero-allocation JSON serializer compatible with C89. It is inspired by jsmn, a minimalistic JSON tokenizer.

Apr 26, 2022
🗄️ single header json parser for C and C++

??️ json.h A simple single header solution to parsing JSON in C and C++. JSON is parsed into a read-only, single allocation buffer. The current suppor

May 18, 2022
single-header json parser for c99 and c++

ghh_json.h a single-header ISO-C99 (and C++ compatible) json loader. why? obviously this isn't the first json library written for C, so why would I wr

May 10, 2022
json_struct is a single header only C++ library for parsing JSON directly to C++ structs and vice versa

Structurize your JSON json_struct is a single header only library that parses JSON to C++ structs/classes and serializing structs/classes to JSON. It

May 19, 2022
a header-file-only, JSON parser serializer in C++

PicoJSON - a C++ JSON parser / serializer Copyright © 2009-2010 Cybozu Labs, Inc. Copyright © 2011-2015 Kazuho Oku Licensed under 2-clause BSD license

May 12, 2022
This is a demo for QDBus & parse json file.

说明 This is a demo for QDBus & parse json file. cmake version:3.13.4 qtcreator 4.8.2验证ok cli侧的 --version暂时没有使用 使用方式 mkdir build cd build cmake .. 开Debu

Nov 26, 2021