Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

About

Parson is a lightweight json library written in C.

Features

  • Lightweight (only 2 files)
  • Simple API
  • Addressing json values with dot notation (similar to C structs or objects in most OO languages, e.g. "objectA.objectB.value")
  • C89 compatible
  • Test suites

Installation

Run:

git clone https://github.com/kgabis/parson.git

and copy parson.h and parson.c to you source code tree.

Run make test to compile and run tests.

Examples

Parsing JSON

Here is a function, which prints basic commit info (date, sha and author) from a github repository.

void print_commits_info(const char *username, const char *repo) {
    JSON_Value *root_value;
    JSON_Array *commits;
    JSON_Object *commit;
    size_t i;
    
    char curl_command[512];
    char cleanup_command[256];
    char output_filename[] = "commits.json";
    
    /* it ain't pretty, but it's not a libcurl tutorial */
    sprintf(curl_command, 
        "curl -s \"https://api.github.com/repos/%s/%s/commits\" > %s",
        username, repo, output_filename);
    sprintf(cleanup_command, "rm -f %s", output_filename);
    system(curl_command);
    
    /* parsing json and validating output */
    root_value = json_parse_file(output_filename);
    if (json_value_get_type(root_value) != JSONArray) {
        system(cleanup_command);
        return;
    }
    
    /* getting array from root value and printing commit info */
    commits = json_value_get_array(root_value);
    printf("%-10.10s %-10.10s %s\n", "Date", "SHA", "Author");
    for (i = 0; i < json_array_get_count(commits); i++) {
        commit = json_array_get_object(commits, i);
        printf("%.10s %.10s %s\n",
               json_object_dotget_string(commit, "commit.author.date"),
               json_object_get_string(commit, "sha"),
               json_object_dotget_string(commit, "commit.author.name"));
    }
    
    /* cleanup code */
    json_value_free(root_value);
    system(cleanup_command);
}

Calling print_commits_info("torvalds", "linux"); prints:

Date       SHA        Author
2012-10-15 dd8e8c4a2c David Rientjes
2012-10-15 3ce9e53e78 Michal Marek
2012-10-14 29bb4cc5e0 Randy Dunlap
2012-10-15 325adeb55e Ralf Baechle
2012-10-14 68687c842c Russell King
2012-10-14 ddffeb8c4d Linus Torvalds
...

Persistence

In this example I'm using parson to save user information to a file and then load it and validate later.

void persistence_example(void) {
    JSON_Value *schema = json_parse_string("{\"name\":\"\"}");
    JSON_Value *user_data = json_parse_file("user_data.json");
    char buf[256];
    const char *name = NULL;
    if (user_data == NULL || json_validate(schema, user_data) != JSONSuccess) {
        puts("Enter your name:");
        scanf("%s", buf);
        user_data = json_value_init_object();
        json_object_set_string(json_object(user_data), "name", buf);
        json_serialize_to_file(user_data, "user_data.json");
    }
    name = json_object_get_string(json_object(user_data), "name");
    printf("Hello, %s.", name);
    json_value_free(schema);
    json_value_free(user_data);
    return;
}

Serialization

Creating JSON values is very simple thanks to the dot notation. Object hierarchy is automatically created when addressing specific fields. In the following example I create a simple JSON value containing basic information about a person.

void serialization_example(void) {
    JSON_Value *root_value = json_value_init_object();
    JSON_Object *root_object = json_value_get_object(root_value);
    char *serialized_string = NULL;
    json_object_set_string(root_object, "name", "John Smith");
    json_object_set_number(root_object, "age", 25);
    json_object_dotset_string(root_object, "address.city", "Cupertino");
    json_object_dotset_value(root_object, "contact.emails", json_parse_string("[\"[email protected]\",\"[email protected]\"]"));
    serialized_string = json_serialize_to_string_pretty(root_value);
    puts(serialized_string);
    json_free_serialized_string(serialized_string);
    json_value_free(root_value);
}

Output:

{
    "name": "John Smith",
    "age": 25,
    "address": {
        "city": "Cupertino"
    },
    "contact": {
        "emails": [
            "[email protected]",
            "[email protected]"
        ]
    }
}

Contributing

I will always merge working bug fixes. However, if you want to add something new to the API, please create an "issue" on github for this first so we can discuss if it should end up in the library before you start implementing it. Remember to follow parson's code style and write appropriate tests.

My other projects

  • ape - simple programming language implemented in C library
  • kgflags - easy to use command-line flag parsing library
  • agnes - header-only NES emulation library

License

The MIT License (MIT)

C Programming
C Programming
C is a foundational programming language, known for its efficiency and control over system resources. It’s widely used in system programming, embedded devices, and performance-critical applications. Learning C helps in understanding core programming concepts.
Create new page · clibs/clib Wiki
Create new page · clibs/clib Wiki
utf8proc/LICENSE.md at master · JuliaStrings/utf8proc
utf8proc/LICENSE.md at master · JuliaStrings/utf8proc
ape_tag_libs/c at master · jeremyevans/ape_tag_libs
ape_tag_libs/c at master · jeremyevans/ape_tag_libs
Build software better, together
Build software better, together
GitHub - antirez/smaz: Small strings compression library
GitHub - antirez/smaz: Small strings compression library
cmathl/LICENSE at master · ScientificC/cmathl
cmathl/LICENSE at master · ScientificC/cmathl
GitHub - siu/minunit: Minimal unit testing framework for C
GitHub - siu/minunit: Minimal unit testing framework for C
GitHub - sheredom/utf8.h: 📚 single header utf8 string functions for C and C++
GitHub - sheredom/utf8.h: 📚 single header utf8 string functions for C and C++
GitHub - joaotavora/yasnippet: A template system for Emacs
GitHub - joaotavora/yasnippet: A template system for Emacs
GitHub - rxi/dyad: Asynchronous networking for C
GitHub - rxi/dyad: Asynchronous networking for C
GitHub - cesanta/slre: Super Light Regexp engine for C/C++
GitHub - cesanta/slre: Super Light Regexp engine for C/C++
GitHub - liteserver/netstring-c: Netstring for C
GitHub - liteserver/netstring-c: Netstring for C
GitHub - cloudwu/pbc: A protocol buffers library for C
GitHub - cloudwu/pbc: A protocol buffers library for C
GitHub - google/pblog: Pblog is a small, low overhead, structured logging library
GitHub - google/pblog: Pblog is a small, low overhead, structured logging library
GitHub - abiggerhammer/hammer: Parser combinators for binary formats, in C. Yes, in C. What? Don't look at me like that.
GitHub - abiggerhammer/hammer: Parser combinators for binary formats, in C. Yes, in C. What? Don't look at me like that.
GitHub - WolfgangSt/libelf: libelf
GitHub - WolfgangSt/libelf: libelf
GitHub - open-mpi/ompi: Open MPI main development repository
GitHub - open-mpi/ompi: Open MPI main development repository
GitHub - dertuxmalwieder/libvldmail: Your friendly e-mail address validation library.
GitHub - dertuxmalwieder/libvldmail: Your friendly e-mail address validation library.
GitHub - RhysU/c99sh: A shebang-friendly script for "interpreting" single C99, C11, and C++ files, including rcfile support.
GitHub - RhysU/c99sh: A shebang-friendly script for "interpreting" single C99, C11, and C++ files, including rcfile support.
GitHub - adamierymenko/huffandpuff: Minimal Huffman coder/decoder
GitHub - adamierymenko/huffandpuff: Minimal Huffman coder/decoder
GitHub - jmckaskill/c-capnproto: C library/compiler for the Cap'n Proto serialization/RPC protocol
GitHub - jmckaskill/c-capnproto: C library/compiler for the Cap'n Proto serialization/RPC protocol
GitHub - docopt/docopt.c: C-code generator for docopt language.
GitHub - docopt/docopt.c: C-code generator for docopt language.
syntastic/LICENCE at master · vim-syntastic/syntastic
syntastic/LICENCE at master · vim-syntastic/syntastic
GitHub - codeplea/tinyexpr: tiny recursive descent expression parser, compiler, and evaluation engine for math expressions
GitHub - codeplea/tinyexpr: tiny recursive descent expression parser, compiler, and evaluation engine for math expressions
libonion - Coralbits S.L.
libonion - Coralbits S.L.
GitHub - nodejs/http-parser: http request/response parser for c
GitHub - nodejs/http-parser: http request/response parser for c
commonmark-spec/LICENSE at master · commonmark/commonmark-spec
commonmark-spec/LICENSE at master · commonmark/commonmark-spec
GitHub - include-what-you-use/include-what-you-use: A tool for use with clang to analyze #includes in C and C++ source files
GitHub - include-what-you-use/include-what-you-use: A tool for use with clang to analyze #includes in C and C++ source files
GitHub - openresty/sregex: A non-backtracking NFA/DFA-based Perl-compatible regex engine matching on large data streams
GitHub - openresty/sregex: A non-backtracking NFA/DFA-based Perl-compatible regex engine matching on large data streams
GitHub - libffi/libffi: A portable foreign-function interface library.
GitHub - libffi/libffi: A portable foreign-function interface library.
C Programming
More on C Programming

Programming Tips & Tricks

Code smarter, not harder—insider tips and tricks for developers.

Error Solutions

Turn frustration into progress—fix errors faster than ever.

Shortcuts

The art of speed—shortcuts to supercharge your workflow.
  1. Collections 😎
  2. Frequently Asked Question's 🤯

Tools

available to use.

Made with ❤️

to provide resources in various ares.