Skip to content
This repository
Newer
Older
100644 221 lines (207 sloc) 6.176 kb
579e6dea »
2012-03-27 Made functions static where applicable.
1 /*
2 * Shared functions for working with JSON data.
3 * */
4
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
5 #include "purest_json.h"
6
579e6dea »
2012-03-27 Made functions static where applicable.
7 static char *lowercase_unicode(char *orig, size_t memsize) {
b7c0014e »
2012-03-29 Cleaning up.
8 char *unicode_intro = "\\";
9 char *segment;
10 char *cleaned_string;
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
11 short i;
12 short uni_len = 4; /*TODO: get real length, we just assume 4 for now */
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
13
5854491c »
2012-03-26 rest-json is now working again.
14 memsize = (strlen(orig) + 1) * sizeof(char);
b7c0014e »
2012-03-29 Cleaning up.
15 cleaned_string = (char *)getbytes(memsize);
16 if (cleaned_string != NULL) {
17 segment = strtok(orig, unicode_intro);
18 memset(cleaned_string, 0x00, strlen(orig) + 1);
19 strcpy(cleaned_string, segment);
20 segment = strtok(NULL, unicode_intro);
21
22 while(segment != NULL) {
23 strcat(cleaned_string, unicode_intro);
24 if (segment[0] == 'u') {
25 for (i = 1; i < 1 + uni_len; i++) {
26 switch (segment[i]) {
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
27 case 'A':
b7c0014e »
2012-03-29 Cleaning up.
28 segment[i] = 'a';
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
29 break;
30 case 'B':
b7c0014e »
2012-03-29 Cleaning up.
31 segment[i] = 'b';
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
32 break;
33 case 'C':
b7c0014e »
2012-03-29 Cleaning up.
34 segment[i] = 'c';
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
35 break;
36 case 'D':
b7c0014e »
2012-03-29 Cleaning up.
37 segment[i] = 'd';
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
38 break;
39 case 'E':
b7c0014e »
2012-03-29 Cleaning up.
40 segment[i] = 'e';
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
41 break;
42 case 'F':
b7c0014e »
2012-03-29 Cleaning up.
43 segment[i] = 'f';
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
44 break;
45 }
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
46 }
b7c0014e »
2012-03-29 Cleaning up.
47 }
48 strcat(cleaned_string, segment);
49 segment = strtok(NULL, unicode_intro);
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
50 }
b7c0014e »
2012-03-29 Cleaning up.
51
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
52 } else {
53 error("Could not allocate memory");
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
54 }
b7c0014e »
2012-03-29 Cleaning up.
55 return cleaned_string;
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
56 }
57
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
58 void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet) {
cee81ff9 »
2012-01-30 Segfault when jobj in output_json is not a real JSON object.
59 enum json_type outer_type;
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
60 enum json_type inner_type;
61 t_atom out_data[2];
62 t_float out_float;
63 char *remainder;
64 float float_value;
65 const char *string_value;
66 int array_len;
67 int i;
68
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
69 outer_type = json_object_get_type(jobj);
70 switch (outer_type) {
71 case json_type_boolean:
5854491c »
2012-03-26 rest-json is now working again.
72 SETFLOAT(&out_data[0], json_object_get_boolean(jobj) ? 1: 0);
73 out_float = atom_getfloat(&out_data[0]);
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
74 outlet_float(data_outlet, out_float);
75 outlet_bang(done_outlet);
76 break;
77 case json_type_double:
5854491c »
2012-03-26 rest-json is now working again.
78 SETFLOAT(&out_data[0], json_object_get_double(jobj));
79 out_float = atom_getfloat(&out_data[0]);
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
80 outlet_float(data_outlet, out_float);
81 outlet_bang(done_outlet);
82 break;
83 case json_type_int:
5854491c »
2012-03-26 rest-json is now working again.
84 SETFLOAT(&out_data[0], json_object_get_int(jobj));
85 out_float = atom_getfloat(&out_data[0]);
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
86 outlet_float(data_outlet, out_float);
87 outlet_bang(done_outlet);
88 break;
89 case json_type_string:
90 outlet_symbol(data_outlet, gensym(json_object_get_string(jobj)));
91 outlet_bang(done_outlet);
92 break;
93 case json_type_null:
94 outlet_symbol(data_outlet, gensym(""));
95 outlet_bang(done_outlet);
96 break;
97 case json_type_object:
98 ;
99 json_object_object_foreach(jobj, key, val) { /* Passing through every json object */
100 SETSYMBOL(&out_data[0], gensym(key));
101 /* Problem with null as value */
102 if (val == NULL) {
103 SETSYMBOL(&out_data[1], gensym(""));
104 } else {
105 inner_type = json_object_get_type(val);
106 switch (inner_type) {
107 case json_type_boolean:
108 SETFLOAT(&out_data[1], json_object_get_boolean(val) ? 1: 0);
109 break;
110 case json_type_double:
111 SETFLOAT(&out_data[1], json_object_get_double(val));
112 break;
113 case json_type_int:
114 SETFLOAT(&out_data[1], json_object_get_int(val));
115 break;
116 case json_type_string:
117 /* Float values might come as string */
118 string_value = json_object_get_string(val);
119 float_value = (float)strtod(string_value, &remainder);
120 /* String to float has no remainder => float */
121 if (strlen(remainder) == 0) {
122 SETFLOAT(&out_data[1], float_value);
123 /* Boolean values might come as string */
124 } else if (str_ccmp(string_value, "true") == 0) {
125 SETFLOAT(&out_data[1], 1);
126 } else if (str_ccmp(string_value, "false") == 0) {
127 SETFLOAT(&out_data[1], 0);
128 /* String */
129 } else {
130 SETSYMBOL(&out_data[1], gensym(string_value));
131 }
132 break;
133 case json_type_object:
134 SETSYMBOL(&out_data[1], gensym(json_object_get_string(val)));
135 json_object_put(val);
136 break;
137 case json_type_array:
138 SETSYMBOL(&out_data[1], gensym(json_object_get_string(val)));
139 break;
140 case json_type_null:
141 SETSYMBOL(&out_data[1], gensym(""));
142 break;
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
143 }
144 }
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
145 outlet_list(data_outlet, &s_list, 2, &out_data[0]);
146 }
147 outlet_bang(done_outlet);
148 break;
149 case json_type_array:
150 ;
151 array_len = json_object_array_length(jobj);
152 for (i = 0; i < array_len; i++) {
153 json_object *array_member = json_object_array_get_idx(jobj, i);
154 if (!is_error(array_member)) {
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
155 output_json(array_member, data_outlet, done_outlet);
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
156 json_object_put(array_member);
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
157 }
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
158 }
159 break;
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
160 }
161 }
162
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
163 void output_json_string(char *json_string, t_outlet *data_outlet, t_outlet *done_outlet) {
164 json_object *jobj;
5854491c »
2012-03-26 rest-json is now working again.
165 size_t memsize = 0;
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
166 /* Needed because of bug in json-c 0.9 */
5854491c »
2012-03-26 rest-json is now working again.
167 char* corrected_json_string = lowercase_unicode(json_string, memsize);
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
168 /* Parse JSON */
4e61ccfb »
2012-02-10 Make string filtering functions consistent.
169 jobj = json_tokener_parse(corrected_json_string);
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
170 if (!is_error(jobj)) {
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
171 output_json(jobj, data_outlet, done_outlet);
d57a3c62 »
2012-04-13 Segfault at json_object_put().
172 /* TODO: This sometimes results in a segfault. Why? */
173 /*json_object_put(jobj);*/
9b282220 »
2012-02-10 Some small bug fixes and cleanups.
174 } else {
175 error("Not a JSON object");
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
176 }
5854491c »
2012-03-26 rest-json is now working again.
177 freebytes(corrected_json_string, memsize);
5f000f9c »
2012-02-04 Fix for issue #5: Unicode characters are converted to lowercase.
178 }
179
5854491c »
2012-03-26 rest-json is now working again.
180 char *remove_backslashes(char *source_string, size_t memsize) {
b7c0014e »
2012-03-29 Cleaning up.
181 char *cleaned_string = NULL;
182 char *masking = "\\";
183 char *segment;
5854491c »
2012-03-26 rest-json is now working again.
184 size_t len_src = strlen(source_string);
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
185
5854491c »
2012-03-26 rest-json is now working again.
186 memsize = (len_src + 1) * sizeof(char);
b7c0014e »
2012-03-29 Cleaning up.
187
188 cleaned_string = (char *) getbytes(memsize * sizeof(char));
189 if (cleaned_string == NULL) {
5854491c »
2012-03-26 rest-json is now working again.
190 error("Unable to allocate memory\n");
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
191 }
b7c0014e »
2012-03-29 Cleaning up.
192 else if (len_src > 0) {
193 segment = strtok(source_string, masking);
194 strcpy(cleaned_string, segment);
195 segment = strtok(NULL, masking);
196 while (segment != NULL) {
197 if (segment[0] != ',') {
198 /* We keep the backslash */
199 strcat(cleaned_string, masking);
200 }
201 strcat(cleaned_string, segment);
202 segment = strtok(NULL, masking);
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
203 }
204 }
b7c0014e »
2012-03-29 Cleaning up.
205 return (cleaned_string);
5a9caf94 »
2012-01-28 update Makefile and arrange files to match the Library Template:
206 }
207
208 int str_ccmp(const char *s1, const char *s2) {
209 const unsigned char *p1 = (const unsigned char *)s1;
210 const unsigned char *p2 = (const unsigned char *)s2;
211
212 while (toupper(*p1) == toupper(*p2)) {
213 if (*p1 == '\0') {
214 return 0;
215 }
216 ++p1;
217 ++p2;
218 }
219 return toupper(*p2) > toupper(*p1) ? -1 : 1;
220 }
Something went wrong with that request. Please try again.