Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 243 lines (222 sloc) 6.973 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
/*
* [json-decode] decodes JSON data and outputs it as lists.
* */

#include "purest_json.h"

static t_class *json_decode_class;

static int str_ccmp(const char *s1, const char *s2) {
const unsigned char *p1 = (const unsigned char *)s1;
const unsigned char *p2 = (const unsigned char *)s2;

while (toupper(*p1) == toupper(*p2)) {
if (*p1 == '\0') {
return 0;
}
++p1;
++p2;
}
return toupper(*p2) > toupper(*p1) ? -1 : 1;
}

static char *lowercase_unicode(char *orig, size_t memsize) {
char *unicode_intro = "\\";
char *segment;
char *cleaned_string;
short i;
short uni_len = 4; /*TODO: get real length, we just assume 4 for now */

memsize = (strlen(orig) + 1) * sizeof(char);
cleaned_string = (char *)getbytes(memsize);
if (cleaned_string != NULL) {
segment = strtok(orig, unicode_intro);
memset(cleaned_string, 0x00, strlen(orig) + 1);
strcpy(cleaned_string, segment);
segment = strtok(NULL, unicode_intro);

while(segment != NULL) {
strcat(cleaned_string, unicode_intro);
if (segment[0] == 'u') {
for (i = 1; i < 1 + uni_len; i++) {
switch (segment[i]) {
case 'A':
segment[i] = 'a';
break;
case 'B':
segment[i] = 'b';
break;
case 'C':
segment[i] = 'c';
break;
case 'D':
segment[i] = 'd';
break;
case 'E':
segment[i] = 'e';
break;
case 'F':
segment[i] = 'f';
break;
}
}
}
strcat(cleaned_string, segment);
segment = strtok(NULL, unicode_intro);
}

} else {
error("Could not allocate memory");
}
return cleaned_string;
}

static void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet) {
enum json_type outer_type;
enum json_type inner_type;
t_atom out_data[2];
t_float out_float;
char *remainder;
float float_value;
const char *string_value;
int array_len;
int i;

outer_type = json_object_get_type(jobj);
switch (outer_type) {
case json_type_boolean:
SETFLOAT(&out_data[0], json_object_get_boolean(jobj) ? 1: 0);
out_float = atom_getfloat(&out_data[0]);
outlet_float(data_outlet, out_float);
outlet_bang(done_outlet);
break;
case json_type_double:
SETFLOAT(&out_data[0], json_object_get_double(jobj));
out_float = atom_getfloat(&out_data[0]);
outlet_float(data_outlet, out_float);
outlet_bang(done_outlet);
break;
case json_type_int:
SETFLOAT(&out_data[0], json_object_get_int(jobj));
out_float = atom_getfloat(&out_data[0]);
outlet_float(data_outlet, out_float);
outlet_bang(done_outlet);
break;
case json_type_string:
outlet_symbol(data_outlet, gensym(json_object_get_string(jobj)));
outlet_bang(done_outlet);
break;
case json_type_null:
outlet_symbol(data_outlet, gensym(""));
outlet_bang(done_outlet);
break;
case json_type_object:
;
json_object_object_foreach(jobj, key, val) { /* Passing through every json object */
SETSYMBOL(&out_data[0], gensym(key));
/* Problem with null as value */
if (val == NULL) {
SETSYMBOL(&out_data[1], gensym(""));
} else {
inner_type = json_object_get_type(val);
switch (inner_type) {
case json_type_boolean:
SETFLOAT(&out_data[1], json_object_get_boolean(val) ? 1: 0);
break;
case json_type_double:
SETFLOAT(&out_data[1], json_object_get_double(val));
break;
case json_type_int:
SETFLOAT(&out_data[1], json_object_get_int(val));
break;
case json_type_string:
/* Float values might come as string */
string_value = json_object_get_string(val);
float_value = (float)strtod(string_value, &remainder);
/* String to float has no remainder => float */
if (strlen(remainder) == 0) {
SETFLOAT(&out_data[1], float_value);
/* Boolean values might come as string */
} else if (str_ccmp(string_value, "true") == 0) {
SETFLOAT(&out_data[1], 1);
} else if (str_ccmp(string_value, "false") == 0) {
SETFLOAT(&out_data[1], 0);
/* String */
} else {
SETSYMBOL(&out_data[1], gensym(string_value));
}
break;
case json_type_object:
SETSYMBOL(&out_data[1], gensym(json_object_get_string(val)));
json_object_put(val);
break;
case json_type_array:
SETSYMBOL(&out_data[1], gensym(json_object_get_string(val)));
break;
case json_type_null:
SETSYMBOL(&out_data[1], gensym(""));
break;
}
}
outlet_list(data_outlet, &s_list, 2, &out_data[0]);
}
outlet_bang(done_outlet);
break;
case json_type_array:
;
array_len = json_object_array_length(jobj);
for (i = 0; i < array_len; i++) {
json_object *array_member = json_object_array_get_idx(jobj, i);
if (!is_error(array_member)) {
output_json(array_member, data_outlet, done_outlet);
json_object_put(array_member);
}
}
break;
}
}

static void output_json_string(char *json_string, t_outlet *data_outlet, t_outlet *done_outlet) {
json_object *jobj;
size_t memsize = 0;
/* Needed because of bug in json-c 0.9 */
char* corrected_json_string = lowercase_unicode(json_string, memsize);
/* Parse JSON */
jobj = json_tokener_parse(corrected_json_string);
if (!is_error(jobj)) {
output_json(jobj, data_outlet, done_outlet);
/* TODO: This sometimes results in a segfault. Why? */
/*json_object_put(jobj);*/
} else {
error("Not a JSON object");
}
freebytes(corrected_json_string, memsize);
}

void setup_json0x2ddecode(void) {
json_decode_class = class_new(gensym("json-decode"), (t_newmethod)json_decode_new,
0, sizeof(t_json_decode), 0, A_GIMME, 0);
class_addsymbol(json_decode_class, (t_method)json_decode_string);
/* Commented because it defeats json_decode_string: [l2s] is always needed */
/*class_addlist(json_decode_class, (t_method)json_decode_list);*/
class_sethelpsymbol(json_decode_class, gensym("json"));
}

void *json_decode_new(t_symbol *selector, int argcount, t_atom *argvec) {
t_json_decode *x = (t_json_decode*)pd_new(json_decode_class);

(void) selector;
(void) argcount;
(void) argvec;

outlet_new(&x->x_ob, NULL);
x->done_outlet = outlet_new(&x->x_ob, &s_bang);
return (void *)x;
}

void json_decode_string(t_json_decode *x, t_symbol *data) {
size_t memsize = 0;
char *json_string = remove_backslashes(data->s_name, memsize);

if (json_string != NULL) {
output_json_string(json_string, x->x_ob.ob_outlet, x->done_outlet);
freebytes(json_string, memsize);
}
}

void json_decode_list(t_json_decode *x, t_symbol *selector, int argcount, t_atom *argvec) {
char json_string[MAXPDSTRING];
char value[MAXPDSTRING];
int i;

(void) selector;


if (argcount > 1) {
atom_string(argvec + 1, json_string, MAXPDSTRING);
for (i = 2; i < argcount; i++) {
atom_string(argvec + i, value, MAXPDSTRING);
strcat(json_string, value);
}
output_json_string(json_string, x->x_ob.ob_outlet, x->done_outlet);
}
}
Something went wrong with that request. Please try again.