Permalink
Browse files

rest-json is now working again.

Removed own constants;
json-encode uses pointers and Pd memory functions instead of arrays and malloc() and free()
  • Loading branch information...
1 parent e8b9fbd commit 5854491cfce5feee7bef1588765127a9b1f269b8 @residuum committed Mar 26, 2012
Showing with 196 additions and 84 deletions.
  1. +7 −6 json-decode.c
  2. +102 −29 json-encode.c
  3. +23 −19 libpurest_json.c
  4. +14 −14 purest_json.h
  5. +50 −16 rest-json.c
View
@@ -24,26 +24,27 @@ void *json_decode_new(t_symbol *selector, int argcount, t_atom *argvec) {
}
void json_decode_string(t_json_decode *x, t_symbol *data) {
- char *json_string = remove_backslashes(data->s_name);
+ 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);
- free(json_string);
+ freebytes(json_string, memsize);
}
}
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];
+ char json_string[MAXPDSTRING];
+ char value[MAXPDSTRING];
int i;
(void) selector;
if (argcount > 1) {
- atom_string(argvec + 1, json_string, MAX_STRING_SIZE);
+ atom_string(argvec + 1, json_string, MAXPDSTRING);
for (i = 2; i < argcount; i++) {
- atom_string(argvec + i, value, MAX_STRING_SIZE);
+ atom_string(argvec + i, value, MAXPDSTRING);
strcat(json_string, value);
}
output_json_string(json_string, x->x_ob.ob_outlet, x->done_outlet);
View
@@ -4,7 +4,7 @@ static t_class *json_encode_class;
void setup_json0x2dencode(void) {
json_encode_class = class_new(gensym("json-encode"), (t_newmethod)json_encode_new,
- 0, sizeof(t_json_encode), 0, A_GIMME, 0);
+ (t_method)json_encode_free, sizeof(t_json_encode), 0, A_GIMME, 0);
class_addbang(json_encode_class, (t_method)json_encode_bang);
class_addmethod(json_encode_class, (t_method)json_encode_add, gensym("add"), A_GIMME, 0);
class_addmethod(json_encode_class, (t_method)json_encode_array_add, gensym("array"), A_GIMME, 0);
@@ -13,7 +13,7 @@ void setup_json0x2dencode(void) {
}
void *json_encode_new(t_symbol *selector, int argcount, t_atom *argvec) {
- t_json_encode *x = (t_json_encode*)pd_new(json_encode_class);
+ t_json_encode *x = (t_json_encode *)pd_new(json_encode_class);
(void) selector;
(void) argcount;
@@ -24,23 +24,35 @@ void *json_encode_new(t_symbol *selector, int argcount, t_atom *argvec) {
return (void *)x;
}
+void json_encode_free (t_json_encode *x, t_symbol *selector, int argcount, t_atom *argvec) {
+ (void) selector;
+ (void) argcount;
+ (void) argvec;
+
+ json_encode_free_memory(x);
+}
+
void json_encode_bang(t_json_encode *x) {
int i, j, k;
int array_member_numbers[x->data_count];
int array_member_count = 0;
short already_added = 0;
+ t_key_value_pair *data_member;
+ t_key_value_pair *data_member_compare;
json_object *jobj = json_object_new_object();
json_object *value;
json_object *array_members[x->data_count];
if (x->data_count > 0) {
+ data_member = x->data;
for (i = 0; i < x->data_count; i++) {
already_added = 0;
/* Is it an array member? */
- if (x->data[i].is_array == 1) {
+ if (data_member->is_array == 1) {
value = json_object_new_array();
+ data_member_compare = data_member;
for (j = i; j < x->data_count; j++) {
- if (strcmp(x->data[j].key, x->data[i].key) == 0) {
+ if (strcmp(data_member_compare->key, data_member->key) == 0) {
for (k = 0; k < array_member_count; k++) {
/* If already inserted, continue i loop */
if (array_member_numbers[k] == j) {
@@ -49,20 +61,22 @@ void json_encode_bang(t_json_encode *x) {
}
}
if (already_added == 0) {
- json_object *array_member = create_object(x->data[j].value);
+ json_object *array_member = create_object(data_member_compare->value);
json_object_array_add(value, array_member);
array_member_numbers[array_member_count] = j;
array_members[array_member_count] = array_member;
array_member_count++;
}
}
+ data_member_compare = data_member_compare->next;
}
} else {
- value = create_object(x->data[i].value);
+ value = create_object(data_member->value);
}
if (already_added == 0) {
- json_object_object_add(jobj, x->data[i].key, value);
+ json_object_object_add(jobj, data_member->key, value);
}
+ data_member = data_member->next;
}
outlet_symbol(x->x_ob.ob_outlet, gensym(json_object_to_json_string(jobj)));
for (i = 0; i < array_member_count; i++) {
@@ -75,62 +89,104 @@ void json_encode_bang(t_json_encode *x) {
json_object *create_object(char *value) {
json_object *object;
char *parsed_string;
+ size_t memsize = 0;
/* if stored value is string is starting with { and ending with },
then create a json object from it. */
if (value[0] == '{' && value[strlen(value) - 1] == '}') {
- parsed_string = remove_backslashes(value);
+ parsed_string = remove_backslashes(value, memsize);;
object = json_tokener_parse(parsed_string);
- free(parsed_string);
+ freebytes(parsed_string, memsize);
} else {
object = json_object_new_string(value);
}
return object;
}
void json_encode_add(t_json_encode *x, t_symbol *selector, int argcount, t_atom *argvec) {
- char key[MAX_STRING_SIZE];
- char value[MAX_STRING_SIZE];
+ char *key;
+ char *value;
+ char temp_value[MAXPDSTRING];
+ t_key_value_pair *created_data = NULL;
+ t_key_value_pair *previous_data = NULL;
int i;
(void) selector;
if (argcount < 2) {
error("For method 'add' You need to specify a value.");
} else {
- atom_string(argvec, key, MAX_STRING_SIZE);
- strcpy(x->data[x->data_count].key, key);
- atom_string(argvec + 1, value, MAX_STRING_SIZE);
- strcpy(x->data[x->data_count].value, value);
+ created_data = (t_key_value_pair *)getbytes(sizeof(t_key_value_pair));
+ key = (char *)getbytes(MAXPDSTRING * sizeof(char));
+ value = (char *)getbytes(MAXPDSTRING * sizeof(char));
+ if (created_data == NULL || key == NULL || value == NULL) {
+ error("Could not allocate memory.");
+ return;
+ }
+ atom_string(argvec, key, MAXPDSTRING);
+ created_data->key = key;
+ atom_string(argvec + 1, value, MAXPDSTRING);
for(i = 2; i < argcount; i++) {
- atom_string(argvec + i, value, MAX_STRING_SIZE);
- strcat(x->data[x->data_count].value, " ");
- strcat(x->data[x->data_count].value, value);
+ atom_string(argvec + i, temp_value, MAXPDSTRING);
+ strcat(value, " ");
+ strcat(value, temp_value);
+ }
+ created_data->value = value;
+ created_data->next = NULL;
+ created_data->is_array = 0;
+ if (x->data == NULL) {
+ x->data = created_data;
+ } else {
+ previous_data = x->data;
+ while (previous_data->next) {
+ previous_data = previous_data->next;
+ }
+ previous_data->next = created_data;
}
- x->data[x->data_count].is_array = 0;
+
x->data_count++;
}
}
void json_encode_array_add(t_json_encode *x, t_symbol *selector, int argcount, t_atom *argvec) {
- char key[MAX_STRING_SIZE];
- char value[MAX_STRING_SIZE];
+ char *key;
+ char *value;
+ char temp_value[MAXPDSTRING];
+ t_key_value_pair *created_data = NULL;
+ t_key_value_pair *previous_data = NULL;
int i;
(void) selector;
if (argcount < 2) {
error("For method 'array' You need to specify a value.");
} else {
- atom_string(argvec, key, MAX_STRING_SIZE);
- strcpy(x->data[x->data_count].key, key);
- atom_string(argvec + 1, value, MAX_STRING_SIZE);
- strcpy(x->data[x->data_count].value, value);
+ created_data = (t_key_value_pair *)getbytes(sizeof(t_key_value_pair));
+ key = (char *)getbytes(MAXPDSTRING * sizeof(char));
+ value = (char *)getbytes(MAXPDSTRING * sizeof(char));
+ if (created_data == NULL || key == NULL || value == NULL) {
+ error("Could not get data");
+ return;
+ }
+ atom_string(argvec, key, MAXPDSTRING);
+ created_data->key = key;
+ atom_string(argvec + 1, value, MAXPDSTRING);
for(i = 2; i < argcount; i++) {
- atom_string(argvec + i, value, MAX_STRING_SIZE);
- strcat(x->data[x->data_count].value, " ");
- strcat(x->data[x->data_count].value, value);
+ atom_string(argvec + i, temp_value, MAXPDSTRING);
+ strcat(value, " ");
+ strcat(value, temp_value);
+ }
+ created_data->value = value;
+ created_data->next = NULL;
+ created_data->is_array = 1;
+ if (x->data == NULL) {
+ x->data = created_data;
+ } else {
+ previous_data = x->data;
+ while (previous_data->next) {
+ previous_data = previous_data->next;
+ }
+ previous_data->next = created_data;
}
- x->data[x->data_count].is_array = 1;
x->data_count++;
}
}
@@ -140,5 +196,22 @@ void json_encode_clear(t_json_encode *x, t_symbol *selector, int argcount, t_ato
(void) argcount;
(void) argvec;
+ json_encode_free_memory(x);
+}
+
+void json_encode_free_memory(t_json_encode *x) {
+ t_key_value_pair *data_to_free;
+ t_key_value_pair *next_data;
+
+ data_to_free = x->data;
+ while(data_to_free != NULL) {
+ next_data = data_to_free->next;
+ freebytes(data_to_free->key, MAXPDSTRING);
+ freebytes(data_to_free->value, MAXPDSTRING);
+ freebytes(data_to_free, sizeof(t_key_value_pair));
+ data_to_free = next_data;
+ }
+
x->data_count = 0;
+ x->data = NULL;
}
View
@@ -1,13 +1,15 @@
#include "purest_json.h"
-char *lowercase_unicode(char *orig) {
+char *lowercase_unicode(char *orig, size_t memsize) {
char *unicode_intro = "\\u";
char *tmp = strstr(orig, unicode_intro);
char *tmp_without_intro;
- char *return_string = (char *) calloc(strlen(orig) + 1, sizeof(char));
+ char *return_string;
short i;
short uni_len = 4; /*TODO: get real length, we just assume 4 for now */
+ memsize = (strlen(orig) + 1) * sizeof(char);
+ return_string = (char *)getbytes(memsize);
if (return_string != NULL) {
if (tmp) {
memset(return_string, 0x00, strlen(orig) + 1);
@@ -67,20 +69,20 @@ void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet
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]);
+ 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[1], json_object_get_double(jobj));
- out_float = atom_getfloat(&out_data[1]);
+ 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[1], json_object_get_int(jobj));
- out_float = atom_getfloat(&out_data[1]);
+ 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;
@@ -160,8 +162,9 @@ void output_json(json_object *jobj, t_outlet *data_outlet, t_outlet *done_outlet
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);
+ char* corrected_json_string = lowercase_unicode(json_string, memsize);
/* Parse JSON */
jobj = json_tokener_parse(corrected_json_string);
if (!is_error(jobj)) {
@@ -170,26 +173,27 @@ void output_json_string(char *json_string, t_outlet *data_outlet, t_outlet *done
} else {
error("Not a JSON object");
}
- free(corrected_json_string);
+ freebytes(corrected_json_string, memsize);
}
-char *remove_backslashes(char *source_string) {
+char *remove_backslashes(char *source_string, size_t memsize) {
char *dest = NULL;
char remove[2] = "\\,";
- size_t len_src = strlen(source_string);
int found;
- unsigned int i = 0;
- int j = 0;
+ size_t i = 0;
+ size_t j = 0;
+ size_t len_src = strlen(source_string);
- dest = (char *) calloc(len_src + 1, sizeof(char));
+ memsize = (len_src + 1) * sizeof(char);
+
+ dest = (char *) getbytes(memsize * sizeof(char));
if (dest == NULL) {
- printf("Unable to allocate memory\n");
+ error("Unable to allocate memory\n");
}
- memset(dest, 0x00,sizeof(char) * len_src + 1 );
- for ( i = 0; i < len_src; i++ ) {
+ for (i = 0; i < memsize; i++ ) {
found = FALSE;
- if (source_string[i] == remove[0] && source_string[i +1] == remove[1]) {
+ if (source_string[i] == remove[0] && source_string[i + 1] == remove[1]) {
i++;
found = TRUE;
}
Oops, something went wrong.

0 comments on commit 5854491

Please sign in to comment.