]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/objson/objson_test.c
added license info and some additional comments
[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         /* sample JSON string with some encoded UTF8 */
29         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--*/";
30
31
32         printf("\nOriginal JSON\n%s\n", jsons); 
33
34         /* parse the JSON string */
35         object* yuk = json_parse_string(jsons); 
36
37         /* grab the class name from the object */
38         printf("\nParsed object with class %s\n", yuk->classname );
39
40         /* turn the resulting object back into JSON */
41         char* ccc = yuk->to_json(yuk); 
42         
43         /* extract a sub-object from the object and print its data*/
44         object* o = yuk->get_index(yuk, 11);
45         printf("\nRandom unicode string => %s\n", o->string_data);
46
47         /* parse the new JSON string to build yet another object */
48         object* yuk2 = json_parse_string(ccc);
49
50         /* turn that one back into JSON and print*/
51         char* cccc = yuk2->to_json(yuk2);
52         printf("\nFinal JSON: \n%s\n", cccc);
53
54         char* string2 = strdup(jsons);
55
56         int x = 0;
57         int count = 10000;
58         printf("\nParsing %d round trips at %f...\n", count, get_timestamp_millis());
59
60         /* parse and stringify many times in a loop to check speed */
61         while(x++ < count) {
62
63                 object* o = json_parse_string(string2); 
64                 free(string2);
65                 string2 = o->to_json(o);
66                 free_object(o);
67
68                 if(!(x % 500))
69                         fprintf(stderr, "Round trip at %d\n", x);
70         }
71
72         printf("After Loop: %f\n", get_timestamp_millis());
73
74
75         /* to_json() generates a string that must be freed by the caller */
76         free(string2);
77         free(ccc); 
78         free(cccc); 
79
80         /* only free the top level objects.  objects that are 'children'
81                 of other objects should not be freed */
82         free_object(yuk); 
83         free_object(yuk2); 
84
85
86
87         /* ------------------------------------------------------------------------ */
88
89         /* parse a big JSON file */
90         FILE* F = fopen("test.json", "r");
91         if(!F) {
92                 perror("unable to open test.json for testing");
93                 exit(99);
94         }
95
96         char buf[10240];
97         char smallbuf[512];
98         bzero(buf, 10240);
99         bzero(smallbuf, 512);
100
101         while(fgets(smallbuf, 512, F)) 
102                 strcat(buf, smallbuf);
103
104         /* dig our way into the JSON object we parsed, see test.json to get
105            an idea of the object structure */
106         object* big = json_parse_string(buf);
107         object* k = big->get_key(big,"web-app");
108         object* k2 = k->get_key(k,"servlet");
109         object* k3 = k2->get_index(k2, 0);
110         object* k4 = k3->get_key(k3,"servlet-class");
111
112         printf("\nValue for object with key 'servlet-class' in the JSON file => %s\n", k4->get_string(k4));
113
114         return 0;
115 }
116
117