About
-Parson is a small json parser and reader written in C.
+Parson is a lighweight json parser and reader written in C.
Features
-
-
- Small (only 2 files) +
- Lightweight (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 @@ -52,6 +52,8 @@
and copy parson.h and parson.c to you source code tree.
+Run tests.sh to compile and run tests.
+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.
@@ -64,17 +66,17 @@ char curl_command[512]; char cleanup_command[256]; - char *output_filename = "commits.json"; + 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(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 (root_value == NULL || json_value_get_type(root_value) != JSONArray) { + root_value = json_parse_file(output_filename); + if (root_value == NULL || json_value_get_type(root_value) != JSONArray) { system(cleanup_command); return; } @@ -94,9 +96,10 @@ json_value_free(root_value); system(cleanup_command); } + -Calling print_commit_info("torvalds", "linux");
prints:
Calling print_commits_info("torvalds", "linux");
prints:
Date SHA Author
2012-10-15 dd8e8c4a2c David Rientjes
diff --git a/params.json b/params.json
index f48e007..b0806a3 100644
--- a/params.json
+++ b/params.json
@@ -1 +1 @@
-{"note":"Don't delete this file! It's used internally to help with page regeneration.","body":"##About\r\nParson is a small [json](http://json.org) parser and reader written in C. \r\n\r\n##Features\r\n* Small (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 the following code:\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\n##Example\r\nHere 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.\r\n```c\r\nvoid print_commit_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 int 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, \"curl \\\"https://api.github.com/repos/%s/%s/commits\\\"\\\r\n > %s 2> /dev/null\", 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 (root_value == NULL || 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\nCalling ```print_commit_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##Important\r\nParson currently supports hexadecimal and octal numbers, but they're not a part of JSON standard, so you shouldn't use them.\r\n\r\n##License\r\n[The MIT License (MIT)](http://opensource.org/licenses/mit-license.php)","name":"parson","google":"UA-35563760-2","tagline":"Small json parser and reader written in C."}
\ No newline at end of file
+{"note":"Don't delete this file! It's used internally to help with page regeneration.","body":"##About\r\nParson is a lighweight [json](http://json.org) parser and reader written in C. \r\n\r\n##Features\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 the following code:\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 tests.sh to compile and run tests.\r\n\r\n##Example\r\nHere 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.\r\n```c\r\nvoid print_commit_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 int 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, \"curl -s \\\"https://api.github.com/repos/%s/%s/commits\\\"\\\r\n > %s\", 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 (root_value == NULL || 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##Important\r\nParson currently supports hexadecimal and octal numbers, but they're not a part of JSON standard, so you shouldn't use them.\r\n\r\n##License\r\n[The MIT License (MIT)](http://opensource.org/licenses/mit-license.php)","tagline":"Lightweight json parser and reader written in C.","name":"parson","google":"UA-35563760-2"}
\ No newline at end of file