printf family format fixup found (har har) and suggested by Scott McKellar -- %lf...
[OpenSRF.git] / src / objson / json2xml.c
1
2 #include "json2xml.h"
3
4 static char* _escape_xml (char*);
5 static int _recurse_jsonObjectToXML(jsonObject*, growing_buffer*);
6
7 char* jsonObjectToXML(jsonObject* obj) {
8
9         growing_buffer * res_xml;
10         char * output;
11
12         res_xml = buffer_init(1024);
13
14         if (!obj)
15                 return strdup("<null/>");
16         
17         _recurse_jsonObjectToXML( obj, res_xml );
18         output = buffer_data(res_xml);
19         
20         buffer_free(res_xml);
21
22         return output;
23
24 }
25
26 int _recurse_jsonObjectToXML(jsonObject* obj, growing_buffer* res_xml) {
27
28         char * hint = NULL;
29         char * bool_val = NULL;
30         int i = 0;
31         
32         if (obj->classname)
33                 hint = strdup(obj->classname);
34
35         if(obj->type == JSON_NULL) {
36
37                 if (hint)
38                         buffer_fadd(res_xml, "<null class_hint=\"%s\"/>",hint);
39                 else
40                         buffer_add(res_xml, "<null/>");
41
42         } else if(obj->type == JSON_BOOL) {
43
44                 if (obj->value.b)
45                         bool_val = strdup("true");
46                 else
47                         bool_val = strdup("false");
48
49                 if (hint)
50                         buffer_fadd(res_xml, "<boolean value=\"%s\" class_hint=\"%s\"/>", bool_val, hint);
51                 else
52                         buffer_fadd(res_xml, "<boolean value=\"%s\"/>", bool_val);
53
54                 free(bool_val);
55                 
56         } else if (obj->type == JSON_STRING) {
57                 if (hint) {
58                         char * t = _escape_xml(jsonObjectGetString(obj));
59                         buffer_fadd(res_xml,"<string class_hint=\"%s\">%s</string>", hint, t);
60                         free(t);
61                 } else {
62                         char * t = _escape_xml(jsonObjectGetString(obj));
63                         buffer_fadd(res_xml,"<string>%s</string>", t);
64                         free(t);
65                 }
66
67         } else if(obj->type == JSON_NUMBER) {
68                 double x = jsonObjectGetNumber(obj);
69                 if (hint) {
70                         if (x == (int)x)
71                                 buffer_fadd(res_xml,"<number class_hint=\"%s\">%d</number>", hint, (int)x);
72                         else
73                                 buffer_fadd(res_xml,"<number class_hint=\"%s\">%f</number>", hint, x);
74                 } else {
75                         if (x == (int)x)
76                                 buffer_fadd(res_xml,"<number>%d</number>", (int)x);
77                         else
78                                 buffer_fadd(res_xml,"<number>%f</number>", x);
79                 }
80
81         } else if (obj->type == JSON_ARRAY) {
82
83                 if (hint) 
84                         buffer_fadd(res_xml,"<array class_hint=\"%s\">", hint);
85                 else
86                         buffer_add(res_xml,"<array>");
87
88                 for ( i = 0; i!= obj->size; i++ )
89                         _recurse_jsonObjectToXML(jsonObjectGetIndex(obj,i), res_xml);
90
91                 buffer_add(res_xml,"</array>");
92
93         } else if (obj->type == JSON_HASH) {
94
95                 if (hint)
96                         buffer_fadd(res_xml,"<object class_hint=\"%s\">", hint);
97                 else
98                         buffer_add(res_xml,"<object>");
99
100                 jsonObjectIterator* itr = jsonNewObjectIterator(obj);
101                 jsonObjectNode* tmp;
102                 while( (tmp = jsonObjectIteratorNext(itr)) ) {
103
104                         buffer_fadd(res_xml,"<element key=\"%s\">",tmp->key);
105
106                         _recurse_jsonObjectToXML(tmp->item, res_xml);
107
108                         buffer_add(res_xml,"</element>");
109                 }
110                 jsonObjectIteratorFree(itr);
111
112                 buffer_add(res_xml,"</object>");
113         }
114
115         if (hint)
116                 free(hint);
117
118         return 1;
119 }
120
121 char* _escape_xml (char* text) {
122         char* out;
123         growing_buffer* b = buffer_init(256);
124         int len = strlen(text);
125         int i;
126         for (i = 0; i < len; i++) {
127                 if (text[i] == '&')
128                         buffer_add(b,"&amp;");
129                 else if (text[i] == '<')
130                         buffer_add(b,"&lt;");
131                 else if (text[i] == '>')
132                         buffer_add(b,"&gt;");
133                 else
134                         buffer_add_char(b,text[i]);
135         }
136         out = buffer_data(b);
137         buffer_free(b);
138         return out;
139 }
140
141