Home

Awesome

JSON-BUILD

json-build is a zero-allocation JSON serializer written in ANSI C. Its tokenizer counterpart can be found at jsmn-find.

Features

Usage

Download json-build.h, include it, done.

#include "json-build.h"

...
jsonb b;
char buf[1024];

jsonb_init(&b);
jsonb_object(&b, buf, sizeof(buf));
{
    jsonb_key(&b, buf, sizeof(buf), "foo", strlen("foo"));
    jsonb_array(&b, buf, sizeof(buf));
    {
        jsonb_number(&b, buf, sizeof(buf), 1);
        jsonb_string(&b, buf, sizeof(buf), "hi", 2);
        jsonb_bool(&b, buf, sizeof(buf), 0);
        jsonb_null(&b, buf, sizeof(buf));
        jsonb_array_pop(&b, buf, sizeof(buf));
    }
    jsonb_object_pop(&b, buf, sizeof(buf));
}
printf("JSON: %s", buf); // JSON: {"foo":[1,"hi",false,null]}

Since json-build is a single-header, header-only library, for more complex use cases you might need to define additional macros. #define JSONB_STATIChides all json-build API symbols by making them static. Also, if you want to include json-build.h for multiple C files, to avoid duplication of symbols you may define JSONB_HEADER macro.

/* In every .c file that uses json-build include only declarations: */
#define JSONB_HEADER
#include "json-build.h"

/* Additionally, create one json-build.c file for json-build implementation: */
#include "json-build.h"

API

The following are the possible return codes for the builder functions:

Its worth mentioning that all JSONB_ERROR_ prefixed codes are negative.

If you get JSONB_ERROR_NOMEM you can either:

  1. re-allocate a larger buffer and call the builder function once more
  2. call jsonb_reset() to reset the buffer's position tracker and call the builder function once more (useful for streaming with a fixed sized buffer!)

Other info

This software is distributed under MIT license, so feel free to integrate it in your commercial products.