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