mirror of
https://github.com/DaveGamble/cJSON.git
synced 2026-12-04 22:08:25 +00:00
updated README markdown, gitignore, added the Makefile and fixed weird comments in test.c
This commit is contained in:
1 parent
4cc55858fd
commit
ebba183f1e
4 files changed
+14
-7
No files matched your search
@@ -0,0 +1,3 @@
|
||||
a.out
|
||||
.svn
|
||||
*.o
|
||||
@@ -0,0 +1,3 @@
|
||||
all: cJSON.o
|
||||
cc -Wall cJSON.c -c -o cJSON.o
|
||||
cc -Wall cJSON.o test.c -lm
|
||||
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
Welcome to cJSON.
|
||||
-----------------
|
||||
|
||||
cJSON aims to be the dumbest possible parser that you can get your job done with.
|
||||
It's a single file of C, and a single header file.
|
||||
@@ -29,7 +30,6 @@ JSON is described best here: http://www.json.org/
|
||||
It's like XML, but fat-free. You use it to move data around, store things, or just
|
||||
generally represent your program's state.
|
||||
|
||||
|
||||
First up, how do I build?
|
||||
Add cJSON.c to your project, and put cJSON.h somewhere in the header search path.
|
||||
For example, to build the test app:
|
||||
@@ -37,17 +37,17 @@ For example, to build the test app:
|
||||
gcc cJSON.c test.c -o test -lm
|
||||
./test
|
||||
|
||||
|
||||
As a library, cJSON exists to take away as much legwork as it can, but not get in your way.
|
||||
As a point of pragmatism (i.e. ignoring the truth), I'm going to say that you can use it
|
||||
in one of two modes: Auto and Manual. Let's have a quick run-through.
|
||||
|
||||
|
||||
I lifted some JSON from this page: http://www.json.org/fatfree.html
|
||||
That page inspired me to write cJSON, which is a parser that tries to share the same
|
||||
philosophy as JSON itself. Simple, dumb, out of the way.
|
||||
|
||||
Some JSON:
|
||||
----------
|
||||
|
||||
{
|
||||
"name": "Jack (\"Bee\") Nimble",
|
||||
"format": {
|
||||
@@ -62,6 +62,7 @@ Some JSON:
|
||||
Assume that you got this from a file, a webserver, or magic JSON elves, whatever,
|
||||
you have a char * to it. Everything is a cJSON struct.
|
||||
Get it parsed:
|
||||
|
||||
cJSON *root = cJSON_Parse(my_json_string);
|
||||
|
||||
This is an object. We're in C. We don't have objects. But we do have structs.
|
||||
@@ -111,6 +112,8 @@ Sibling hs type False, name "interlace", and a sibling:
|
||||
Sibling has type Number, name "frame rate", value 24
|
||||
|
||||
Here's the structure:
|
||||
---------------------
|
||||
|
||||
typedef struct cJSON {
|
||||
struct cJSON *next,*prev;
|
||||
struct cJSON *child;
|
||||
@@ -235,7 +238,6 @@ of those objects.
|
||||
As soon as you call cJSON_Print, it renders the structure to text.
|
||||
|
||||
|
||||
|
||||
The test.c code shows how to handle a bunch of typical cases. If you uncomment
|
||||
the code, it'll load, parse and print a bunch of test files, also from json.org,
|
||||
which are more complex than I'd care to try and stash into a const char array[].
|
||||
@@ -243,5 +245,4 @@ which are more complex than I'd care to try and stash into a const char array[].
|
||||
|
||||
Enjoy cJSON!
|
||||
|
||||
|
||||
- Dave Gamble, Aug 2009
|
||||
@@ -69,7 +69,7 @@ void create_objects()
|
||||
cJSON_AddFalseToObject (fmt,"interlace");
|
||||
cJSON_AddNumberToObject(fmt,"frame rate", 24);
|
||||
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string.
|
||||
out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string. */
|
||||
|
||||
/* Our "days of the week" array: */
|
||||
const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
|
||||
@@ -142,7 +142,7 @@ int main (int argc, const char * argv[]) {
|
||||
doit(text4);
|
||||
doit(text5);
|
||||
|
||||
/* Parse standard testfiles:
|
||||
/* Parse standard testfiles: */
|
||||
/* dofile("../../tests/test1"); */
|
||||
/* dofile("../../tests/test2"); */
|
||||
/* dofile("../../tests/test3"); */
|
||||
|
||||
Reference in new issue
Block a user