AminetAminet
Search:
84716 packages online
About
Recent
Browse
Search
Upload
Setup
Services

dev/c/parson.lha

Mirror:Random
Showing:m68k-amigaosppc-amigaosppc-morphosi386-arosi386-amithlonppc-warpupppc-powerupgeneric
No screenshot available
Short:lightweight json library written in C
Author:Krzysztof Gabis
Uploader:Carsten Larsen (carsten larsen mail com)
Type:dev/c
Version:1.0
Architecture:generic; m68k-amigaos;
URL:https://github.com/kgabis/parson
Date:2020-04-27
Download:http://aminet.net/dev/c/parson.lha - View contents
Readme:http://aminet.net/dev/c/parson.readme
Downloads:887

Features
--------

* Full JSON support
* Lightweight (only 2 files)
* Simple API
* Addressing json values with dot notation
* C89 compatible
* Test suites


Installation
------------

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

Pre-compiled executable for test is included: test and testcpp. Please
be aware they both contain bugs in double precision parsing - tracing back
to linked version of clib2.


Parsing JSON
------------

Here is a function, which prints basic commit info (date, sha and author)
from a github repository (pre-compiled version in samples/parse).

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 (pre-compiled version in samples/persistence).

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 (pre-compiled version
in samples/serialization).

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@example.com\",\"email2 at example.com\"]"));
    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 at example.com",
            "email2 at example.com"
        ]
    }
}

Contributing
------------

The author claims to 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 you 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.

License
-------

The MIT License (MIT)


Contents of dev/c/parson.lha
 PERMSSN    UID  GID    PACKED    SIZE  RATIO METHOD CRC     STAMP          NAME
---------- ----------- ------- ------- ------ ---------- ------------ -------------
[Amiga]                    637    1079  59.0% -lh5- d01b Apr 24 20:48 parson/LICENSE
[Amiga]                   2056    5064  40.6% -lh5- 6812 Apr 27 21:07 parson/parson.readme
[Amiga]                    147     196  75.0% -lh5- 0ac5 Apr 27 20:40 parson/samples/CurlFetch
[Amiga]                  30318   64452  47.0% -lh5- 1624 Apr 27 20:40 parson/samples/parse
[Amiga]                  34344   72076  47.6% -lh5- 1638 Apr 27 20:40 parson/samples/persistence
[Amiga]                  30271   64384  47.0% -lh5- e652 Apr 27 20:40 parson/samples/serialization
[Amiga]                  31836   31836 100.0% -lh0- a569 Apr 27 21:08 parson/source.lha
[Amiga]                  66164  181972  36.4% -lh5- fc55 Apr 27 20:40 parson/test
[Amiga]                  83087  219056  37.9% -lh5- 350f Apr 27 20:40 parson/testcpp
[Amiga]                    742    1480  50.1% -lh5- 9387 Apr 24 20:48 parson/tests/test_1_1.txt
[Amiga]                     27    4108   0.7% -lh5- 4338 Apr 24 20:48 parson/tests/test_1_2.txt
[Amiga]                    109     148  73.6% -lh5- e0e8 Apr 24 20:48 parson/tests/test_1_3.txt
[Amiga]                    458    1099  41.7% -lh5- 4c63 Apr 24 20:48 parson/tests/test_2.txt
[Amiga]                    655    1446  45.3% -lh5- 1f5c Apr 24 20:48 parson/tests/test_2_comments.txt
[Amiga]                    443    1136  39.0% -lh5- 0ba9 Apr 24 20:48 parson/tests/test_2_pretty.txt
[Amiga]                    258     409  63.1% -lh5- 394b Apr 24 20:48 parson/tests/test_5.txt
---------- ----------- ------- ------- ------ ---------- ------------ -------------
 Total        16 files  281552  649941  43.3%            Apr 27 22:52

Aminet © 1992-2024 Urban Müller and the Aminet team. Aminet contact address: <aminetaminet net>