{"name":"parson","tagline":"Lightweight json library written in C.","body":"##About\r\nParson is a lighweight [json](http://json.org) library written in C.\r\n\r\n##Features\r\n* Full JSON support\r\n* Lightweight (only 2 files)\r\n* Simple API\r\n* Addressing json values with dot notation (similiar to C structs or objects in most OO languages, e.g. \"objectA.objectB.value\")\r\n* C89 compatible\r\n* Test suites\r\n\r\n##Installation\r\nRun:\r\n```\r\ngit clone https://github.com/kgabis/parson.git\r\n```\r\nand copy parson.h and parson.c to you source code tree.\r\n\r\nRun ```make test``` to compile and run tests.\r\n\r\n##Examples\r\n###Parsing JSON\r\nHere is a function, which prints basic commit info (date, sha and author) from a github repository. \r\n```c\r\nvoid print_commits_info(const char *username, const char *repo) {\r\n JSON_Value *root_value;\r\n JSON_Array *commits;\r\n JSON_Object *commit;\r\n size_t i;\r\n \r\n char curl_command[512];\r\n char cleanup_command[256];\r\n char output_filename[] = \"commits.json\";\r\n \r\n /* it ain't pretty, but it's not a libcurl tutorial */\r\n sprintf(curl_command, \r\n \"curl -s \\\"https://api.github.com/repos/%s/%s/commits\\\" > %s\",\r\n username, repo, output_filename);\r\n sprintf(cleanup_command, \"rm -f %s\", output_filename);\r\n system(curl_command);\r\n \r\n /* parsing json and validating output */\r\n root_value = json_parse_file(output_filename);\r\n if (json_value_get_type(root_value) != JSONArray) {\r\n system(cleanup_command);\r\n return;\r\n }\r\n \r\n /* getting array from root value and printing commit info */\r\n commits = json_value_get_array(root_value);\r\n printf(\"%-10.10s %-10.10s %s\\n\", \"Date\", \"SHA\", \"Author\");\r\n for (i = 0; i < json_array_get_count(commits); i++) {\r\n commit = json_array_get_object(commits, i);\r\n printf(\"%.10s %.10s %s\\n\",\r\n json_object_dotget_string(commit, \"commit.author.date\"),\r\n json_object_get_string(commit, \"sha\"),\r\n json_object_dotget_string(commit, \"commit.author.name\"));\r\n }\r\n \r\n /* cleanup code */\r\n json_value_free(root_value);\r\n system(cleanup_command);\r\n}\r\n\r\n```\r\nCalling ```print_commits_info(\"torvalds\", \"linux\");``` prints: \r\n```\r\nDate SHA Author\r\n2012-10-15 dd8e8c4a2c David Rientjes\r\n2012-10-15 3ce9e53e78 Michal Marek\r\n2012-10-14 29bb4cc5e0 Randy Dunlap\r\n2012-10-15 325adeb55e Ralf Baechle\r\n2012-10-14 68687c842c Russell King\r\n2012-10-14 ddffeb8c4d Linus Torvalds\r\n...\r\n```\r\n\r\n###Persistence\r\nIn this example I'm using parson to save user information to a file and then load it and validate later.\r\n```c\r\nvoid persistence_example(void) {\r\n JSON_Value *schema = json_parse_string(\"{\\\"name\\\":\\\"\\\"}\");\r\n JSON_Value *user_data = json_parse_file(\"user_data.json\");\r\n char buf[256];\r\n const char *name = NULL;\r\n if (user_data == NULL || json_validate(schema, user_data) != JSONSuccess) {\r\n puts(\"Enter your name:\");\r\n scanf(\"%s\", buf);\r\n user_data = json_value_init_object();\r\n json_object_set_string(json_object(user_data), \"name\", buf);\r\n json_serialize_to_file(user_data, \"user_data.json\");\r\n }\r\n name = json_object_get_string(json_object(user_data), \"name\");\r\n printf(\"Hello, %s.\", name);\r\n json_value_free(schema);\r\n json_value_free(user_data);\r\n return;\r\n}\r\n```\r\n\r\n###Serialization\r\nCreating JSON values is very simple thanks to the dot notation. \r\nObject hierarchy is automatically created when addressing specific fields. \r\nIn the following example I create a simple JSON value containing basic information about a person.\r\n```c\r\nvoid serialization_example(void) {\r\n JSON_Value *root_value = json_value_init_object();\r\n JSON_Object *root_object = json_value_get_object(root_value);\r\n char *serialized_string = NULL;\r\n json_object_set_string(root_object, \"name\", \"John Smith\");\r\n json_object_set_number(root_object, \"age\", 25);\r\n json_object_dotset_string(root_object, \"address.city\", \"Cupertino\");\r\n json_object_dotset_value(root_object, \"contact.emails\", json_parse_string(\"[\\\"email@example.com\\\",\\\"email2@example.com\\\"]\"));\r\n serialized_string = json_serialize_to_string_pretty(root_value);\r\n puts(serialized_string);\r\n json_free_serialized_string(serialized_string);\r\n json_value_free(root_value);\r\n}\r\n\r\n```\r\n\r\nOutput:\r\n```\r\n{\r\n \"name\": \"John Smith\",\r\n \"age\": 25,\r\n \"address\": {\r\n \"city\": \"Cupertino\"\r\n },\r\n \"contact\": {\r\n \"emails\": [\r\n \"email@example.com\",\r\n \"email2@example.com\"\r\n ]\r\n }\r\n}\r\n```\r\n\r\n##Contributing\r\n\r\nI will always merge *working* bug fixes. However, if you want to add something to the API, \r\nI *won't* merge it without prior discussion.\r\nRemember to follow parson's code style and write appropriate tests.\r\n\r\n##License\r\n[The MIT License (MIT)](http://opensource.org/licenses/mit-license.php)","google":"UA-35563760-2","note":"Don't delete this file! It's used internally to help with page regeneration."}