Permalink
Browse files

Fixing issue #4:

- test for is_error(jobj) before json_object_put(jobj)
- using lists for [json-decode]
  • Loading branch information...
1 parent 5a2fbc5 commit 6624bc67efabac3179bc62b32852e386814a2ea8 @residuum committed Jan 30, 2012
Showing with 25 additions and 1 deletion.
  1. +24 −1 json-decode.c
  2. +1 −0 purest_json.h
View
@@ -6,6 +6,7 @@ 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);
class_sethelpsymbol(json_decode_class, gensym("json"));
}
@@ -21,5 +22,27 @@ void json_decode_string(t_json_decode *x, t_symbol *data) {
json_object *jobj;
jobj = json_tokener_parse(json_string);
output_json(jobj, x->x_ob.ob_outlet, x->done_outlet);
- json_object_put(jobj);
+ if (!is_error(jobj)) {
+ json_object_put(jobj);
+ }
+}
+
+void json_decode_list(t_json_decode *x, t_symbol *selector, int argcount, t_atom *argvec) {
+ char json_string[MAX_STRING_SIZE];
+ char value[MAX_STRING_SIZE];
+ int i;
+ json_object *jobj;
+ if (argcount > 1) {
+ atom_string(argvec + 1, json_string, MAX_STRING_SIZE);
+ for (i = 2; i < argcount; i++) {
+ atom_string(argvec + i, value, MAX_STRING_SIZE);
+ strcat(json_string, value);
+ }
+ post("%s", json_string);
+ jobj = json_tokener_parse(json_string);
+ output_json(jobj, x->x_ob.ob_outlet, x->done_outlet);
+ if (!is_error(jobj)) {
+ json_object_put(jobj);
+ }
+ }
}
View
@@ -76,6 +76,7 @@ void setup_json0x2ddecode(void);
void *json_decode_new(t_symbol *selector, int argcount, t_atom *argvec);
void json_decode_string(t_json_decode *x, t_symbol *data);
+void json_decode_list(t_json_decode *x, t_symbol *selector, int argcount, t_atom *argvec);
void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet);
/* general */

0 comments on commit 6624bc6

Please sign in to comment.