/BetterJson

Fast and Leightweight Json Parser for C/C++.

Primary LanguageCGNU General Public License v3.0GPL-3.0

Better JSON Parser (Deprecated)

DISCLAIMER

I've abondened the repository, if you want an efficient Json Parser use https://github.com/Swiftense/Utility instead.

This is just an Preview release and doesn't gurantee full functionality or stability. At current point of time, it is not more than an simple example of how two write an fast JSON parser using C.

Setup

  • Download the BetterJson library from here and unpack the archive.

  • Include Parser/SJsonParser.c and Parser/SJsonParserA.c for Json Arrays.

  • Parse a Json file by calling sjs_loadFile(...)

  • Compile and execute using gcc:

    gcc -o <yourname> <sourcefiles>.c ./libbjson.so && ./<yourname>

Example

Simple example of how you can read a json file that looks like the following:

{
  "name": "Bob Nagel",
  "age": 40,
  "city": "Chicago",
  "pets": ["dog", "bird", "turtle"]
}

Start by loading the Json file...

#include "include/SJsonParser.h" /* basic json parser */
#include "include/SJsonParserA.h" /* json parser for arrays */

void main(void)
{
    /* load example.json */
    JsonData* json = sjs_loadFile("example.json");
    /* set name to "Hans Wurst" */
    sjs_setPair(json, "name", sjs_createValueString("Hans Wurst"));
    /* set age property to  15.5 */
    sjs_setPair(json, "age", sjs_createValueDouble(15.5));
    /* add male property and set it to true */
    sjs_setPair(json, "male", sjs_createValueBool(1));

    /* load pet array */
    JsonArrayData* json_array = sjs_getValue(json, "pets")._jsonArray;
    /* get second item of pet array */
    printf("First pet: %s\n", sjs_arr_getValue(sjs_getValue(json, "pets")._jsonArray, 1)._string);
    /* free array */
    sjs_arr_delete(json_array);

    /* write changes to file */
    sjs_save(json, "test2.json");
    exit(0);
}

But further documentation can be found in the Header files.

License

GNU GPL 3.0