Add new json_object_array_sort functiontags/json-c-0.10-20120530
| @@ -87,6 +87,13 @@ array_list_add(struct array_list *arr, void *data) | |||||
| return array_list_put_idx(arr, arr->length, data); | return array_list_put_idx(arr, arr->length, data); | ||||
| } | } | ||||
| void | |||||
| array_list_sort(struct array_list *arr, int(*sort_fn)(const void *, const void *)) | |||||
| { | |||||
| qsort(arr->array, arr->length, sizeof(arr->array[0]), | |||||
| (int (*)(const void *, const void *))sort_fn); | |||||
| } | |||||
| int | int | ||||
| array_list_length(struct array_list *arr) | array_list_length(struct array_list *arr) | ||||
| { | { | ||||
| @@ -46,6 +46,9 @@ array_list_add(struct array_list *al, void *data); | |||||
| extern int | extern int | ||||
| array_list_length(struct array_list *al); | array_list_length(struct array_list *al); | ||||
| extern void | |||||
| array_list_sort(struct array_list *arr, int(*compar)(const void *, const void *)); | |||||
| #ifdef __cplusplus | #ifdef __cplusplus | ||||
| } | } | ||||
| #endif | #endif | ||||
| @@ -534,6 +534,11 @@ struct array_list* json_object_get_array(struct json_object *jso) | |||||
| } | } | ||||
| } | } | ||||
| void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *)) | |||||
| { | |||||
| array_list_sort(jso->o.c_array, sort_fn); | |||||
| } | |||||
| int json_object_array_length(struct json_object *jso) | int json_object_array_length(struct json_object *jso) | ||||
| { | { | ||||
| return array_list_length(jso->o.c_array); | return array_list_length(jso->o.c_array); | ||||
| @@ -195,6 +195,16 @@ extern struct array_list* json_object_get_array(struct json_object *obj); | |||||
| */ | */ | ||||
| extern int json_object_array_length(struct json_object *obj); | extern int json_object_array_length(struct json_object *obj); | ||||
| /** Sorts the elements of jso of type json_type_array | |||||
| * | |||||
| * Pointers to the json_object pointers will be passed as the two arguments | |||||
| * to @sort_fn | |||||
| * | |||||
| * @param obj the json_object instance | |||||
| * @param sort_fn a sorting function | |||||
| */ | |||||
| extern void json_object_array_sort(struct json_object *jso, int(*sort_fn)(const void *, const void *)); | |||||
| /** Add an element to the end of a json_object of type json_type_array | /** Add an element to the end of a json_object of type json_type_array | ||||
| * | * | ||||
| * The reference count will *not* be incremented. This is to make adding | * The reference count will *not* be incremented. This is to make adding | ||||
| @@ -6,6 +6,29 @@ | |||||
| #include "json.h" | #include "json.h" | ||||
| static int sort_fn (const void *j1, const void *j2) | |||||
| { | |||||
| json_object **jso1, **jso2; | |||||
| int i1, i2; | |||||
| jso1 = j1; | |||||
| jso2 = j2; | |||||
| if (!*jso1 && !*jso2) { | |||||
| return 0; | |||||
| } | |||||
| if (!*jso1) { | |||||
| return -1; | |||||
| } | |||||
| if (!*jso2) { | |||||
| return 1; | |||||
| } | |||||
| i1 = json_object_get_int(*jso1); | |||||
| i2 = json_object_get_int(*jso2); | |||||
| return i1 - i2; | |||||
| } | |||||
| int main(int argc, char **argv) | int main(int argc, char **argv) | ||||
| { | { | ||||
| json_tokener *tok; | json_tokener *tok; | ||||
| @@ -45,6 +68,27 @@ int main(int argc, char **argv) | |||||
| } | } | ||||
| printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); | printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); | ||||
| json_object_put(my_array); | |||||
| my_array = json_object_new_array(); | |||||
| json_object_array_add(my_array, json_object_new_int(3)); | |||||
| json_object_array_add(my_array, json_object_new_int(1)); | |||||
| json_object_array_add(my_array, json_object_new_int(2)); | |||||
| json_object_array_put_idx(my_array, 4, json_object_new_int(0)); | |||||
| printf("my_array=\n"); | |||||
| for(i=0; i < json_object_array_length(my_array); i++) { | |||||
| json_object *obj = json_object_array_get_idx(my_array, i); | |||||
| printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); | |||||
| } | |||||
| printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); | |||||
| json_object_array_sort(my_array, sort_fn); | |||||
| printf("my_array=\n"); | |||||
| for(i=0; i < json_object_array_length(my_array); i++) { | |||||
| json_object *obj = json_object_array_get_idx(my_array, i); | |||||
| printf("\t[%d]=%s\n", i, json_object_to_json_string(obj)); | |||||
| } | |||||
| printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array)); | |||||
| my_object = json_object_new_object(); | my_object = json_object_new_object(); | ||||
| json_object_object_add(my_object, "abc", json_object_new_int(12)); | json_object_object_add(my_object, "abc", json_object_new_int(12)); | ||||
| json_object_object_add(my_object, "foo", json_object_new_string("bar")); | json_object_object_add(my_object, "foo", json_object_new_string("bar")); | ||||
| @@ -13,6 +13,20 @@ my_array= | |||||
| [3]=null | [3]=null | ||||
| [4]=5 | [4]=5 | ||||
| my_array.to_string()=[ 1, 2, 3, null, 5 ] | my_array.to_string()=[ 1, 2, 3, null, 5 ] | ||||
| my_array= | |||||
| [0]=3 | |||||
| [1]=1 | |||||
| [2]=2 | |||||
| [3]=null | |||||
| [4]=0 | |||||
| my_array.to_string()=[ 3, 1, 2, null, 0 ] | |||||
| my_array= | |||||
| [0]=null | |||||
| [1]=0 | |||||
| [2]=1 | |||||
| [3]=2 | |||||
| [4]=3 | |||||
| my_array.to_string()=[ null, 0, 1, 2, 3 ] | |||||
| my_object= | my_object= | ||||
| abc: 12 | abc: 12 | ||||
| foo: "bar" | foo: "bar" | ||||