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