Skip to content

DieTime/static-json-builder

Repository files navigation

Health Check Linux Health Check MacOS Health Check Windows MinGW Health Check Windows MSVC License type: MIT Single Header Support

Library for convenient JSON objects initialization without memory allocations.

👨‍💻 Usage

You can see examples sources here.

#include <stdlib.h>
#include <stdio.h>
#include <static-json-builder.h>

int main(void)
{
    Json json = JsonObject(                /*  {                  */
        JsonProp("items", JsonArray(       /*     "items": [      */
            JsonNull(),                    /*         null,       */
            JsonBool(true),                /*         true,       */
            JsonInt(1),                    /*         1,          */
            JsonString("hello")            /*         "hello",    */
        ))                                 /*      ],             */
    );                                     /*  },                 */

    char *string = NULL;
    char *buffer = malloc(json_stingified_size(json));

    string = json_stringify(json);
    json_stringify_into_buffer(json, buffer);

    printf("%s\n", string);  /*  {"items":[null,true,1,"hello"]}  */
    printf("%s\n", buffer);  /*  {"items":[null,true,1,"hello"]}  */

    free(string);
    free(buffer);

    return 0;
}

🔨 Building, testing and installing

$ meson build -Dbuildtype=release -Dtests=true
$ cd build
$ meson test
$ meson compile
$ sudo meson install

🔌 Linking

The library supports pkg-config, which makes linking easier and more convenient.

👍 Single header version is also supported!

cmake_minimum_required(VERSION 3.14)
project(program)

find_package(PkgConfig)
pkg_check_modules(StaticJsonBuilder REQUIRED IMPORTED_TARGET static-json-builder)

add_executable(program example.c)

target_link_libraries(program PUBLIC PkgConfig::StaticJsonBuilder)

About

Library for convenient JSON objects initialization without memory allocations

Topics

Resources

License

Stars

Watchers

Forks