2 Copyright (C) 2005 Georgia Public Library Service
3 Bill Erickson <highfalutin@gmail.com>
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
17 #include <objson/object.h>
18 #include <objson/json_parser.h>
23 /* ---------------------------------------------------------------------- */
24 /* See object.h for function info */
25 /* ---------------------------------------------------------------------- */
33 o = jsonParseString("[ 1, 4, 6, 9 ]");
34 jsonObjectIterator* itr = jsonNewObjectIterator(o);
35 jsonObjectNode* tmp = NULL;
36 while( (tmp = jsonObjectIteratorNext(itr)) ) {
37 char* q = jsonObjectToJSON(tmp->item);
38 printf("Iterator thing => %s\n", q);
41 jsonObjectIteratorFree(itr);
46 printf("------------------------------------------------------------------\n");
47 o = jsonParseString("{\"key\":-1}");
48 char* h = jsonObjectToJSON(o);
49 printf("\nParsed number: %s\n", h);
54 /* number, double, and 'null' parsing... */
55 printf("------------------------------------------------------------------\n");
56 o = jsonParseString("1");
57 printf("\nParsed number: %f\n", jsonObjectGetNumber(o));
61 printf("------------------------------------------------------------------\n");
62 o = jsonParseString("nUlL");
63 char* s = jsonObjectToJSON(o);
64 printf("\nJSON Null: %s\n", s);
68 printf("------------------------------------------------------------------\n");
69 o = jsonParseString("[1, .5, null]");
70 s = jsonObjectToJSON(o);
71 printf("\nJSON MIX: %s\n", s );
75 printf("------------------------------------------------------------------\n");
76 /* simulate an error.. */
77 printf("\nShould print error msg: \n");
78 o = jsonParseString("[1, .5. null]");
79 if( o == NULL ) printf("\n");
82 printf("------------------------------------------------------------------\n");
83 o = jsonParseString("[ Null, trUe, falSE, 1, 12.9, \"true\" ]");
84 s = jsonObjectToJSON(o);
85 printf("More JSON: %s\n", s);
89 printf("------------------------------------------------------------------\n");
90 o = jsonParseString("[ Null, trUe, falSE, 1, 12.9, \"true\", "
91 "{\"key\":[0,0.0,1],\"key2\":null},NULL, { }, [] ]");
92 s = jsonObjectToJSON(o);
93 printf("More JSON: %s\n", s);
98 printf("------------------------------------------------------------------\n");
99 o = jsonParseString("{ Null: trUe }");
103 printf("------------------------------------------------------------------\n");
104 o = jsonParseString("\"Pin\\u0303ata\"");
105 s = jsonObjectToJSON(o);
106 printf("UNICODE:: %s\n", o->value.s);
107 printf("Back to JSON: %s\n", s);
112 /* sample JSON string with some encoded UTF8 */
113 char* jsons = "/*--S mvr--*/[null,null,null,\"Griswold del Castillo, Richard\",[],null,\"1405676\",null,null,\"1558853243 (alk. paper) :\",\"c2002\",\"Pin\\u0303ata Books\",null,[],[[\"Chavez, Cesar 1927-\",\"Juvenile literature\"],[\"Labor leaders\",\"United States\",\"Biography\",\"Juvenile literature\"],[\"Mexican Americans\",\"Biography\",\"Juvenile literature\"],[\"Agricultural laborers\",\"Labor unions\",\"United States\",\"History\",\"Juvenile literature\"],[\"United Farm Workers\",\"History\",\"Juvenile literature\"],[\"Chavez, Cesar 1927-\"],[\"Labor leaders\"],[\"Mexican Americans\",\"Biography\"],[\"United Farm Workers.\"],[\"Spanish language materials\",\"Bilingual\"],[\"Chavez, Cesar 1927-\",\"Literatura juvenil\"],[\"Li\\u0301deres obreros\",\"Estados Unidos\",\"Biografi\\u0301a\",\"Literatura juvenil\"],[\"Mexicano-americanos\",\"Biografi\\u0301a\",\"Literatura juvenil\"],[\"Sindicatos\",\"Trabajadores agri\\u0301colas\",\"Estados Unidos\",\"Historia\",\"Literatura juvenil\"],[\"Unio\\u0301n de Trabajadores Agri\\u0301colas\",\"Historia\",\"Literatura juvenil\"]],\"ocm48083852 \",\"Ce\\u0301sar Cha\\u0301vez : the struggle for justice = Ce\\u0301sar Cha\\u0301vez : la lucha por la justicia\",[\"text\"], { \"hi\":\"you\"} ]/*--E mvr--*/";
116 printf("------------------------------------------------------------------\n");
117 printf("\nOriginal JSON\n%s\n", jsons);
119 /* parse the JSON string */
120 jsonObject* yuk = jsonParseString(jsons);
122 /* grab the class name from the object */
123 printf("------------------------------------------------------------------\n");
124 printf("\nParsed object with class %s\n", yuk->classname );
126 /* turn the resulting object back into JSON */
127 char* ccc = jsonObjectToJSON(yuk);
129 /* extract a sub-object from the object and print its data*/
130 o = jsonObjectGetIndex(yuk, 11);
131 printf("\nRandom unicode string => %s\n", jsonObjectGetString(o));
133 /* parse the new JSON string to build yet another object */
134 jsonObject* yuk2 = jsonParseString(ccc);
136 printf("------------------------------------------------------------------\n");
137 /* turn that one back into JSON and print*/
138 char* cccc = jsonObjectToJSON(yuk2);
139 printf("\nFinal JSON: \n%s\n", cccc);
141 char* string2 = strdup(jsons);
143 printf("------------------------------------------------------------------\n");
146 printf("\nParsing %d round trips at %f...\n", count, get_timestamp_millis());
148 /* parse and stringify many times in a loop to check speed */
151 jsonObject* o = jsonParseString(string2);
153 string2 = jsonObjectToJSON(o);
157 fprintf(stderr, "Round trip at %d\n", x);
160 printf("After Loop: %f\n", get_timestamp_millis());
167 /* only free the top level objects. objects that are 'children'
168 of other objects should not be freed */
170 jsonObjectFree(yuk2);
174 /* ------------------------------------------------------------------------ */
176 /* parse a big JSON file */
177 FILE* F = fopen("test.json", "r");
179 perror("unable to open test.json for testing");
186 memset(buf, 0, sizeof(buf));
187 memset(smallbuf, 0, sizeof(smallbuf));
189 while(fgets(smallbuf, sizeof(smallbuf), F))
190 strncat(buf, smallbuf, sizeof(buf) - 1);
192 /* dig our way into the JSON object we parsed, see test.json to get
193 an idea of the object structure */
194 printf("------------------------------------------------------------------\n");
195 jsonObject* big = jsonParseString(buf);
196 jsonObject* k = jsonObjectGetKey(big,"web-app");
197 jsonObject* k2 = jsonObjectGetKey(k,"servlet");
198 jsonObject* k3 = jsonObjectGetIndex(k2, 0);
199 jsonObject* k4 = jsonObjectGetKey(k3,"servlet-class");
203 printf("\nValue for object with key 'servlet-class' in the JSON file => %s\n", jsonObjectGetString(k4));