🔋 In-place lightweight JSON parser

🔋 JSON parser for C

Build Status Build status Documentation Status Codacy Badge

This is very simple and very powerful JSON parser. It creates DOM-like data structure and allows to iterate and process JSON objects very simple way. It does not alloc any memory for JSON itself, it only allocs memory for tokens. It also does not use recursive way to build data structure which makes it very fast to build DOM-like tree structure.

Documentation

Almost all functions (inline versions) and parameters are documented inside related headers.
Complete documentation: http://json.readthedocs.io

Features

  • header-only or optional compiled library
  • option to store members and arrays as reverse order or normal
  • doesn't alloc memory for keys and values only for tokens
  • creates DOM-like data structure to make it easy to iterate though
  • simple api
  • provides some util functions to print json, get int32, int64, float, double...
  • very small library
  • unique way to parse JSON (check the object map section)

Object Map

Here a VERY UNIQUE and VERY COOL and VERY EASY and VERY FAST way to parse known JSON: objmap.

void
callback_1(json_t * __restrict json, void * __restrict obj) {
  printf("entered callback_1\n");
}

json = json_parse(/* JSON string */, true);

json_objmap_t objmap[] = {
    {
      .key = "key1",
      .foundFunc = {
        .func  = callback_1,
        .param = "callback 1 param" 
      }
    },
    {
      .key = "key2",
      .foundFunc = {
        .func = callback_1
      }
    }
};

/* or you can use macro helpers which is more readable if you don't need more details: */
json_objmap_t objmap[] = {
    JSON_OBJMAP_FN("key 1", func1, param1),
    JSON_OBJMAP_FN("key 2", func2, param2),
    JSON_OBJMAP_FN("key 3", func3, param3),
    /* ... */
};

json_objmap_call(json, objmap, ARRAY_LEN(objmap), NULL);

/* or use this to iterate objmap manually */
json_objmap(json, objmap, ARRAY_LEN(objmap));

In this way you don't have to compare keys in a loopi just map the keys with a function or with userdata. You don't have to use function in this way, you may use to map json object to userdata which may be a GOTO LABEL (to use compound gotos) or something else.

Important Note for Arrays

  • json_array_t inherits json_t, so you can cast json_array_t to json_t. It only adds count member.
  • The value link gives array's childen (last element if you set reverse to true)
  • Every item of json_array_t is json_t. You can get integer, float or boolean values from that object. The next link of that object is next item of that object in array.

Important Note for keys and values

  • json doesn't copy keys and values, it only gives pointers to key and values. So when compaing keys or copying values, you must use keySize or valSize. Or you can use builtin inline functions.

TODOs

  • provide header only library and optionally compile version
  • provide option to preserve array order (currently array order is reversed, because it is easy to parse it in this way; this may be changed. Please follow new commits or releases)
  • windows build
  • documentation
  • ignore comments?
  • cmake
  • tests
  • extra optimizations
  • usage in detail
  • Unicode support (UTF-8)
  • null object

Build

Unix (Autotools)

sh autogen.sh
./configure
make
[sudo] make install

you can grap library in .libs folder after build finished

Windows (MSBuild)

Windows related build files, project files are located in win folder, make sure you are inside tm/win folder. Code Analysis are enabled, it may take awhile to build

cd win
.\build.bat
Cmake options with Defaults:
option(JSON_SHARED "Shared build" ON)
option(JSON_STATIC "Static build" OFF)
option(JSON_USE_C99 "" OFF) # C11 
option(JSON_USE_TEST "Enable Tests" OFF) # for make check - make test
mkdir build && cd build
cmake ..
make
[sudo] make install

Header-only or Compiled Library

The functions has the jsonc_ prefix are compiled version which is called from library. To use this feature you must include json/call/json.h header.

To use header-only library you must include json/json.h header. The functions has the json_ prefix are forced to be inlined. When you use this, you don't have to compile the library.

todo.

Example usage

You can inspect json_print() to view usage in more detail. The example will be updated later to give more detail.

#include <json/json.h>
#include <json/print.h>

int main(int argc, const char * argv[]) {
  json_doc_t *doc;
  json_t     *root;
  
  doc  = json_parse(/* JSON string */, true);
  root = doc->root;

  json_print_human(stderr, root);

  json_free(doc);

  return 0;
}
const json_doc_t *jsonDoc;
const json_t         *json;

jsonDoc = json_parse(/* JSON string */, true);
json    = jsonDoc->root->value;

/* already defined in util.h */
JSON_INLINE
bool
json_key_eq(const json_t * __restrict obj, const char * __restrict str) {
  return strncmp(str, obj->key, obj->keySize) == 0;
}

while (json) {
    if (json_key_eq(json, "key 1")) {
      int aNumber;

     aNumber = json_int32(json, 0);

     /* ... */
    } else if (json_key_eq(json, "key 2")) {
      const char *nonNullTerminatedString;
      const char *nullTerminatedString;

      /* just pointer */
      nonNullTerminatedString = json_string(json);

       /* null-terminated string (strdup), needs to be freed */
      nullTerminatedString    = json_string_dup(json);

     /* ... */
    } else if (json_key_eq(json,"key 3")) {
      json_t *aChild;
      
      aChild = json->value;
      while (aChild) {
          /* handle child node */
          aChild = aChild->next;
      }
    }

    json = json->next;
}

Using Object Map

Here a VERY UNIQUE and VERY COOL and VERY EASY and VERY FAST way to parse known JSON: objmap.

void
callback_1(json_t * __restrict json, void * __restrict obj) {
  printf("entered callback_1\n");
}

json = json_parse(/* JSON string */, true);

json_objmap_t objmap[] = {
    {
      .key = "key1",
      .foundFunc = {
        .func = callback_1
      }
    },
    {
      .key = "key2",
      .foundFunc = {
        .func = callback_1
      }
    }
};

/* or you can use macro helpers which is more readable if you don't need more details: */
json_objmap_t objmap[] = {
    JSON_OBJMAP_FN("key 1", func1, param1),
    JSON_OBJMAP_FN("key 2", func2, param2),
    JSON_OBJMAP_FN("key 3", func3, param3),
    /* ... */
};

json_objmap_call(json, objmap, ARRAY_LEN(objmap), NULL);

In this way you don't have to compare keys in a loopi just map the keys with a function or with userdata. You don't have to use function in this way, you may use to map json object to userdata which may be a GOTO LABEL (to use compound gotos) or something else.

License

MIT. check the LICENSE file

Owner
Recep Aslantas
alone with numbers
Recep Aslantas
Similar Resources

a JSON parser and printer library in C. easy to integrate with any model.

libjson - simple and efficient json parser and printer in C Introduction libjson is a simple library without any dependancies to parse and pretty prin

Nov 21, 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

Dec 27, 2022

A fast JSON parser/generator for C++ with both SAX/DOM style API

A fast JSON parser/generator for C++ with both SAX/DOM style API

A fast JSON parser/generator for C++ with both SAX/DOM style API Tencent is pleased to support the open source community by making RapidJSON available

Dec 30, 2022

RapidJSON is a JSON parser and generator for C++.

RapidJSON is a JSON parser and generator for C++.

A fast JSON parser/generator for C++ with both SAX/DOM style API

Dec 30, 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

Dec 1, 2022

Buggy JSON parser

Fuzzgoat: A minimal libFuzzer integration This repository contains a basic C project that includes an (intentionally insecure) JSON parser. It is an e

Apr 11, 2022

A generator of JSON parser & serializer C++ code from structure header files

JSON-CPP-gen This is a program that parses C++ structures from a header file and automatically generates C++ code capable of serializing said structur

Oct 13, 2022

Very low footprint JSON parser written in portable ANSI C

Very low footprint JSON parser written in portable C89 (sometimes referred to as ANSI C). BSD licensed with no dependencies (i.e. just drop the C file

Dec 23, 2022

A light-weight json parser.

pson pson is a lightweight parser and it support six type, null , bool, number, string, array, object, and it can parse the encoding of UTF-8. It's fa

Dec 1, 2022
Comments
  • how can i use it after configure this project?

    how can i use it after configure this project?

    when i generate .libs directory ,how can i use it to parse json? You said the project file is header I use command :

    gcc -shared -fPIC test_json.c -o test -L ./lib/libjson
    

    to build c file. And I get an error

    fatal error: common.h: No such file or directory
    

    this is my code

    #include "json/json.h"
    #include "json/print.h"
    
    int main(int argc, const char * argv[]) {
      json_doc_t *doc;
      json_t     *root;
      
      doc  = json_parse(/* JSON string */, true);
      root = doc->root;
    
      json_print_human(stderr, root);
    
      json_free(doc);
    
      return 0;
    }
    

    image

  • Added a fuzzing test harness to improve stability.

    Added a fuzzing test harness to improve stability.

    The fuzzing is about the example main highlighted in README.md, which calls json_parse, json_print_human and json_free; maybe more test harnesses could be added, using other functions.

  • normal order is not working properly

    normal order is not working properly

    Reverse order is working fine but normal order is not working properly in complex json. So currently only reverse order is working.

    I'll fix normal order (!= reverse) asap.

  • New Design: drop prev and use parent link

    New Design: drop prev and use parent link

    The json library supports both reverse order and normal order. Unlike reverse order, it is not easy (or fast) to implement the normal order for now by using prev links.

    Now I would like to introduce new design which drops prev links and brings parent links.

    • Every item has parent and next: so you can switch to parent easily, but three is no easy way to get back item (I don't think we need this, do we?).

    if we need prev in the future we may update the design

    Now it is fast to parse JSON as normal and reverse order 🤗

🔋 In-place lightweight JSON parser

?? JSON parser for C This is very simple and very powerful JSON parser. It creates DOM-like data structure and allows to iterate and process JSON obje

Dec 10, 2022
Lightweight, extremely high-performance JSON parser for C++11

sajson sajson is an extremely high-performance, in-place, DOM-style JSON parser written in C++. Originally, sajson meant Single Allocation JSON, but i

Dec 16, 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

Jan 4, 2023
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

Dec 30, 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

Sep 14, 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

Jan 9, 2023
A JSON parser in C++

JSON++ Introduction JSON++ is a light-weight JSON parser, writer and reader written in C++. JSON++ can also convert JSON documents into lossless XML d

Dec 28, 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

Jan 7, 2023
Very low footprint JSON parser written in portable ANSI C

Very low footprint JSON parser written in portable ANSI C. BSD licensed with no dependencies (i.e. just drop the C file into your project) Never recur

Jan 5, 2023
Very simple C++ JSON Parser

Very simple JSON parser for c++ data.json: { "examples": [ { "tag_name": "a", "attr": [ { "key":

Nov 20, 2022