Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Some small bug fixes and cleanups.

  • Loading branch information...
commit 9b2822202096be957acd911878281bdce6a5d1ae 1 parent e39f917
@residuum authored
Showing with 103 additions and 95 deletions.
  1. +10 −4 json-decode.c
  2. +92 −91 libpurest_json.c
  3. +1 −0  rest-json.c
View
14 json-decode.c
@@ -6,24 +6,30 @@ 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);
- class_addlist(json_decode_class, (t_method)json_decode_list);
+ /* 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) {
- output_json_string(remove_backslashes(data->s_name), x->x_ob.ob_outlet, x->done_outlet);
+ char *json_string = remove_backslashes(data->s_name);
+
+ if (json_string != NULL) {
+ output_json_string(json_string, x->x_ob.ob_outlet, x->done_outlet);
+ free(json_string);
+ }
}
void json_decode_list(t_json_decode *x, t_symbol *selector, int argcount, t_atom *argvec) {
View
183 libpurest_json.c
@@ -4,11 +4,11 @@ void lowercase_unicode(char *orig) {
char *unicode_intro = "\\u";
char *tmp = strstr(orig, unicode_intro);
char *tmp_without_intro;
- char orig_return[strlen(orig)];
+ char orig_return[strlen(orig) + 1];
short i;
short uni_len = 4; /*TODO: get real length, we just assume 4 for now */
if (tmp) {
- memset(orig_return, 0, strlen(orig));
+ memset(orig_return, 0, strlen(orig) + 1);
strncpy(orig_return, orig, strlen(orig) - strlen(tmp));
do {
for (i = 2; i < 2 + uni_len; i++) {
@@ -57,98 +57,97 @@ void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet
int array_len;
int i;
- if (is_error(jobj)) {
- error("Not a JSON object.");
- } else {
- outer_type = json_object_get_type(jobj);
- switch (outer_type) {
- /* We really have a JSON object */
- case json_type_boolean:
- SETFLOAT(&out_data[1], json_object_get_boolean(jobj) ? 1: 0);
- out_float = atom_getfloat(&out_data[1]);
- outlet_float(data_outlet, out_float);
- outlet_bang(done_outlet);
- break;
- case json_type_double:
- SETFLOAT(&out_data[1], json_object_get_double(jobj));
- out_float = atom_getfloat(&out_data[1]);
- outlet_float(data_outlet, out_float);
- outlet_bang(done_outlet);
- break;
- case json_type_int:
- SETFLOAT(&out_data[1], json_object_get_int(jobj));
- out_float = atom_getfloat(&out_data[1]);
- 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)));
- 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;
- }
+ outer_type = json_object_get_type(jobj);
+ switch (outer_type) {
+ case json_type_boolean:
+ SETFLOAT(&out_data[1], json_object_get_boolean(jobj) ? 1: 0);
+ out_float = atom_getfloat(&out_data[1]);
+ outlet_float(data_outlet, out_float);
+ outlet_bang(done_outlet);
+ break;
+ case json_type_double:
+ SETFLOAT(&out_data[1], json_object_get_double(jobj));
+ out_float = atom_getfloat(&out_data[1]);
+ outlet_float(data_outlet, out_float);
+ outlet_bang(done_outlet);
+ break;
+ case json_type_int:
+ SETFLOAT(&out_data[1], json_object_get_int(jobj));
+ out_float = atom_getfloat(&out_data[1]);
+ 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);
+ 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;
- }
+ }
+ break;
}
}
@@ -158,9 +157,11 @@ void output_json_string(char *json_string, t_outlet *data_outlet, t_outlet *done
lowercase_unicode(json_string);
/* Parse JSON */
jobj = json_tokener_parse(json_string);
- output_json(jobj, data_outlet, done_outlet);
if (!is_error(jobj)) {
+ output_json(jobj, data_outlet, done_outlet);
json_object_put(jobj);
+ } else {
+ error("Not a JSON object");
}
}
View
1  rest-json.c
@@ -166,6 +166,7 @@ void *execute_rest_thread(void *thread_args) {
if (out_memory.memory) {
free(out_memory.memory);
}
+ free((void *)result);
} else {
error("Error while performing request: %s", curl_easy_strerror(result));
}
Please sign in to comment.
Something went wrong with that request. Please try again.