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