]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/objson/objson_test.c
printf family format fixup found (har har) and suggested by Scott McKellar -- %lf...
[OpenSRF.git] / src / objson / objson_test.c
1 /*
2 Copyright (C) 2005  Georgia Public Library Service 
3 Bill Erickson <highfalutin@gmail.com>
4
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.
9
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.
14 */
15
16 //#include "utils.h"
17 #include "object.h"
18 #include "json_parser.h"
19
20 #include <stdio.h>
21 #include <fcntl.h>
22
23 /* ---------------------------------------------------------------------- */
24 /* See object.h for function info */
25 /* ---------------------------------------------------------------------- */
26 int main() {
27
28
29
30         jsonObject* o;
31
32
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);
39                 free(q);
40         }
41         jsonObjectIteratorFree(itr);
42         jsonObjectFree(o);
43         
44
45
46         printf("------------------------------------------------------------------\n");
47         o = jsonParseString("{\"key\":-1}");
48         char* h = jsonObjectToJSON(o);
49         printf("\nParsed number: %s\n", h);
50         free(h);
51         jsonObjectFree(o);
52
53
54         /* number, double, and 'null' parsing... */
55         printf("------------------------------------------------------------------\n");
56         o = jsonParseString("1");
57         printf("\nParsed number: %f\n", jsonObjectGetNumber(o));
58         jsonObjectFree(o);
59
60
61         printf("------------------------------------------------------------------\n");
62         o = jsonParseString("nUlL");
63         char* s = jsonObjectToJSON(o);
64         printf("\nJSON Null: %s\n", s);
65         free(s);
66         jsonObjectFree(o);
67
68         printf("------------------------------------------------------------------\n");
69         o = jsonParseString("[1, .5, null]");
70         s  = jsonObjectToJSON(o);
71         printf("\nJSON MIX: %s\n", s );
72         free(s);
73         jsonObjectFree(o);
74
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"); 
80         jsonObjectFree(o);
81
82         printf("------------------------------------------------------------------\n");
83         o = jsonParseString("[ Null, trUe, falSE, 1, 12.9, \"true\" ]");
84         s = jsonObjectToJSON(o);
85         printf("More JSON: %s\n", s);
86         free(s);
87         jsonObjectFree(o);
88
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);
94         free(s);
95         jsonObjectFree(o);
96
97
98         printf("------------------------------------------------------------------\n");
99         o = jsonParseString("{ Null: trUe }");
100
101
102
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);
108         jsonObjectFree(o);
109         free(s);
110
111
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--*/";
114
115
116         printf("------------------------------------------------------------------\n");
117         printf("\nOriginal JSON\n%s\n", jsons); 
118
119         /* parse the JSON string */
120         jsonObject* yuk = jsonParseString(jsons); 
121
122         /* grab the class name from the object */
123         printf("------------------------------------------------------------------\n");
124         printf("\nParsed object with class %s\n", yuk->classname );
125
126         /* turn the resulting object back into JSON */
127         char* ccc = jsonObjectToJSON(yuk); 
128         
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));
132
133         /* parse the new JSON string to build yet another object */
134         jsonObject* yuk2 = jsonParseString(ccc);
135
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);
140
141         char* string2 = strdup(jsons);
142
143         printf("------------------------------------------------------------------\n");
144         int x = 0;
145         int count = 30;
146         printf("\nParsing %d round trips at %f...\n", count, get_timestamp_millis());
147
148         /* parse and stringify many times in a loop to check speed */
149         while(x++ < count) {
150
151                 jsonObject* o = jsonParseString(string2); 
152                 free(string2);
153                 string2 = jsonObjectToJSON(o);
154                 jsonObjectFree(o);
155
156                 if(!(x % 500))
157                         fprintf(stderr, "Round trip at %d\n", x);
158         }
159
160         printf("After Loop: %f\n", get_timestamp_millis());
161
162
163         free(string2);
164         free(ccc); 
165         free(cccc); 
166
167         /* only free the top level objects.  objects that are 'children'
168                 of other objects should not be freed */
169         jsonObjectFree(yuk); 
170         jsonObjectFree(yuk2); 
171
172
173
174         /* ------------------------------------------------------------------------ */
175
176         /* parse a big JSON file */
177         FILE* F = fopen("test.json", "r");
178         if(!F) {
179                 perror("unable to open test.json for testing");
180                 exit(99);
181         }
182         fclose(F);
183
184         char buf[10240];
185         char smallbuf[512];
186         bzero(buf, 10240);
187         bzero(smallbuf, 512);
188
189         while(fgets(smallbuf, 512, F)) 
190                 strcat(buf, smallbuf);
191
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");
200
201         jsonObjectFree(big);
202
203         printf("\nValue for object with key 'servlet-class' in the JSON file => %s\n", jsonObjectGetString(k4));
204         
205
206         return 0;
207 }
208
209