You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
Krzysztof Gabis e36fa2ab41 Changes in README.md 12 years ago
tests Renamed bool to boolean. 12 years ago
.gitignore Initial commit. 12 years ago
README.md Changes in README.md 12 years ago
parson.c Changed int to size_t to avoid signed/unsigned comparisons 12 years ago
parson.h Fixed some compatibility issues with C++ compilers. 12 years ago
tests.c Fixed some compatibility issues with C++ compilers. 12 years ago
tests.sh Initial commit. 12 years ago

README.md

##About Parson is a small json parser and reader written in C.

##Features

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

##Installation Run the following code:

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

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

##Example Here is a function, which prints basic commit info (date, sha and author) from a github repository. It's also included in tests.c file, you can just uncomment and run it.

void print_commit_info(const char *username, const char * repo) {
    JSON_Value *root_value;
    JSON_Array *commits;
    JSON_Object *commit;
    int 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 \"https://api.github.com/repos/%s/%s/commits\"\
            > %s 2> /dev/null", 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 (root_value == NULL || 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_commit_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
...

##Important Parson currently supports hexadecimal and octal numbers, but they're not a part of JSON standard, so you shouldn't use them.

##License The MIT License (MIT)