]> git.evergreen-ils.org Git - working/Evergreen.git/blob - OpenSRF/src/objson/objson_test.c
89bd2d28452b11130701a181eea16e2ee9004229
[working/Evergreen.git] / OpenSRF / 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         object* o;
31
32
33         printf("------------------------------------------------------------------\n");
34         //o = json_parse_string("-1");
35         o = json_parse_string("{\"key\":-1}");
36         char* h = o->to_json(o);
37         printf("\nParsed number: %s\n", h);
38         free_object(o);
39
40
41         /* number, double, and 'null' parsing... */
42         printf("------------------------------------------------------------------\n");
43          o = json_parse_string("1");
44         printf("\nParsed number: %ld\n", o->num_value);
45         free_object(o);
46
47
48         printf("------------------------------------------------------------------\n");
49         o = json_parse_string("1.1");
50         printf("\nDouble number: %lf\n", o->double_value);
51         free_object(o);
52
53         printf("------------------------------------------------------------------\n");
54         o = json_parse_string("nUlL");
55         char* s = o->to_json(o);
56         free_object(o);
57
58         printf("\nJSON Null: %s\n", s);
59         free(s);
60
61         printf("------------------------------------------------------------------\n");
62         o = json_parse_string("[1, .5, null]");
63         s  = o->to_json(o);
64         printf("\nJSON MIX: %s\n", s );
65         free(s);
66         free_object(o);
67
68         printf("------------------------------------------------------------------\n");
69         /* simulate an error.. */
70         printf("\nShould print error msg: \n");
71         o = json_parse_string("[1, .5. null]");
72         if( o == NULL ) printf("\n"); 
73
74         printf("------------------------------------------------------------------\n");
75         o = json_parse_string("[ Null, trUe, falSE, 1, 12.9, \"true\" ]");
76         s = o->to_json(o);
77         printf("More JSON: %s\n", s);
78         free(s);
79         free_object(o);
80
81         printf("------------------------------------------------------------------\n");
82         o = json_parse_string("[ Null, trUe, falSE, 1, 12.9, \"true\", "
83                         "{\"key\":[0,0.0,1],\"key2\":null},NULL, { }, [] ]");
84         s = o->to_json(o);
85         printf("More JSON: %s\n", s);
86         free(s);
87         free_object(o);
88
89
90         printf("------------------------------------------------------------------\n");
91         o = json_parse_string("{ Null: trUe }");
92
93
94
95         printf("------------------------------------------------------------------\n");
96         o = json_parse_string("\"Pin\\u0303ata\"");
97         s = o->to_json(o);
98         printf("UNICODE:: %s\n", o->string_data);
99         printf("Back to JSON: %s\n", s);
100         free_object(o);
101         free(s);
102
103
104         /* sample JSON string with some encoded UTF8 */
105         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--*/";
106
107
108         printf("------------------------------------------------------------------\n");
109         printf("\nOriginal JSON\n%s\n", jsons); 
110
111         /* parse the JSON string */
112         object* yuk = json_parse_string(jsons); 
113
114         /* grab the class name from the object */
115         printf("------------------------------------------------------------------\n");
116         printf("\nParsed object with class %s\n", yuk->classname );
117
118         /* turn the resulting object back into JSON */
119         char* ccc = yuk->to_json(yuk); 
120         
121         /* extract a sub-object from the object and print its data*/
122         o = yuk->get_index(yuk, 11);
123         printf("\nRandom unicode string => %s\n", o->string_data);
124
125         /* parse the new JSON string to build yet another object */
126         object* yuk2 = json_parse_string(ccc);
127
128         printf("------------------------------------------------------------------\n");
129         /* turn that one back into JSON and print*/
130         char* cccc = yuk2->to_json(yuk2);
131         printf("\nFinal JSON: \n%s\n", cccc);
132
133         char* string2 = strdup(jsons);
134
135         printf("------------------------------------------------------------------\n");
136         int x = 0;
137         int count = 3000;
138         printf("\nParsing %d round trips at %f...\n", count, get_timestamp_millis());
139
140         /* parse and stringify many times in a loop to check speed */
141         while(x++ < count) {
142
143                 object* o = json_parse_string(string2); 
144                 free(string2);
145                 string2 = o->to_json(o);
146                 free_object(o);
147
148                 if(!(x % 500))
149                         fprintf(stderr, "Round trip at %d\n", x);
150         }
151
152         printf("After Loop: %f\n", get_timestamp_millis());
153
154
155         /* to_json() generates a string that must be freed by the caller */
156         free(string2);
157         free(ccc); 
158         free(cccc); 
159
160         /* only free the top level objects.  objects that are 'children'
161                 of other objects should not be freed */
162         free_object(yuk); 
163         free_object(yuk2); 
164
165
166
167         /* ------------------------------------------------------------------------ */
168
169         /* parse a big JSON file */
170         FILE* F = fopen("test.json", "r");
171         if(!F) {
172                 perror("unable to open test.json for testing");
173                 exit(99);
174         }
175
176         char buf[10240];
177         char smallbuf[512];
178         bzero(buf, 10240);
179         bzero(smallbuf, 512);
180
181         while(fgets(smallbuf, 512, F)) 
182                 strcat(buf, smallbuf);
183
184         /* dig our way into the JSON object we parsed, see test.json to get
185            an idea of the object structure */
186         printf("------------------------------------------------------------------\n");
187         object* big = json_parse_string(buf);
188         object* k = big->get_key(big,"web-app");
189         object* k2 = k->get_key(k,"servlet");
190         object* k3 = k2->get_index(k2, 0);
191         object* k4 = k3->get_key(k3,"servlet-class");
192
193         printf("\nValue for object with key 'servlet-class' in the JSON file => %s\n", k4->get_string(k4));
194
195
196         
197
198         return 0;
199 }
200
201