mirror of
https://github.com/json-c/json-c.git
synced 2026-03-19 13:09:07 +08:00
Change %d to %llu and add cast to unsigned long long for size_t arguments,
otherwise compilation will fail with errors like:
test1.c:70:15: error: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘size_t {aka long unsigned int}’ [-Werror=format=]
%zu is avoided to stay compatible with old libc versions (like old Visual
Studio).
218 lines
7.0 KiB
C
218 lines
7.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stddef.h>
|
|
#include <string.h>
|
|
#include <assert.h>
|
|
|
|
#include "json.h"
|
|
#include "parse_flags.h"
|
|
|
|
static int sort_fn (const void *j1, const void *j2)
|
|
{
|
|
json_object * const *jso1, * const *jso2;
|
|
int i1, i2;
|
|
|
|
jso1 = (json_object* const*)j1;
|
|
jso2 = (json_object* const*)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;
|
|
}
|
|
|
|
#ifdef TEST_FORMATTED
|
|
#define json_object_to_json_string(obj) json_object_to_json_string_ext(obj,sflags)
|
|
#else
|
|
/* no special define */
|
|
#endif
|
|
|
|
json_object *make_array(void);
|
|
json_object *make_array()
|
|
{
|
|
json_object *my_array;
|
|
|
|
my_array = json_object_new_array();
|
|
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_add(my_array, json_object_new_int(3));
|
|
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
|
|
json_object_array_put_idx(my_array, 3, json_object_new_int(4));
|
|
json_object_array_put_idx(my_array, 6, json_object_new_int(7));
|
|
|
|
return my_array;
|
|
}
|
|
|
|
void test_array_del_idx(void);
|
|
void test_array_del_idx()
|
|
{
|
|
int rc;
|
|
size_t ii;
|
|
size_t orig_array_len;
|
|
json_object *my_array;
|
|
#ifdef TEST_FORMATTED
|
|
int sflags = 0;
|
|
#endif
|
|
|
|
my_array = make_array();
|
|
orig_array_len = json_object_array_length(my_array);
|
|
|
|
printf("my_array=\n");
|
|
for(ii = 0; ii < json_object_array_length(my_array); ii++)
|
|
{
|
|
json_object *obj = json_object_array_get_idx(my_array, ii);
|
|
printf("\t[%llu]=%s\n", (unsigned long long)ii, json_object_to_json_string(obj));
|
|
}
|
|
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
|
|
|
|
for (ii = 0; ii < orig_array_len; ii++)
|
|
{
|
|
rc = json_object_array_del_idx(my_array, 0, 1);
|
|
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n",
|
|
rc, json_object_to_json_string(my_array));
|
|
}
|
|
|
|
// One more time, with the empty array:
|
|
rc = json_object_array_del_idx(my_array, 0, 1);
|
|
printf("after del_idx(0,1)=%d, my_array.to_string()=%s\n",
|
|
rc, json_object_to_json_string(my_array));
|
|
|
|
json_object_put(my_array);
|
|
|
|
// Delete all array indexes at once
|
|
my_array = make_array();
|
|
rc = json_object_array_del_idx(my_array, 0, orig_array_len);
|
|
printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n",
|
|
(unsigned long long)orig_array_len, rc, json_object_to_json_string(my_array));
|
|
|
|
json_object_put(my_array);
|
|
|
|
// Delete *more* than all array indexes at once
|
|
my_array = make_array();
|
|
rc = json_object_array_del_idx(my_array, 0, orig_array_len + 1);
|
|
printf("after del_idx(0,%llu)=%d, my_array.to_string()=%s\n",
|
|
(unsigned long long)(orig_array_len + 1), rc, json_object_to_json_string(my_array));
|
|
|
|
json_object_put(my_array);
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
json_object *my_string, *my_int, *my_object, *my_array;
|
|
size_t i;
|
|
#ifdef TEST_FORMATTED
|
|
int sflags = 0;
|
|
#endif
|
|
|
|
MC_SET_DEBUG(1);
|
|
|
|
#ifdef TEST_FORMATTED
|
|
sflags = parse_flags(argc, argv);
|
|
#endif
|
|
|
|
my_string = json_object_new_string("\t");
|
|
printf("my_string=%s\n", json_object_get_string(my_string));
|
|
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
|
|
json_object_put(my_string);
|
|
|
|
my_string = json_object_new_string("\\");
|
|
printf("my_string=%s\n", json_object_get_string(my_string));
|
|
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
|
|
json_object_put(my_string);
|
|
|
|
my_string = json_object_new_string("/");
|
|
printf("my_string=%s\n", json_object_get_string(my_string));
|
|
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
|
|
printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
|
|
json_object_put(my_string);
|
|
|
|
my_string = json_object_new_string("/foo/bar/baz");
|
|
printf("my_string=%s\n", json_object_get_string(my_string));
|
|
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
|
|
printf("my_string.to_string(NOSLASHESCAPE)=%s\n", json_object_to_json_string_ext(my_string, JSON_C_TO_STRING_NOSLASHESCAPE));
|
|
json_object_put(my_string);
|
|
|
|
my_string = json_object_new_string("foo");
|
|
printf("my_string=%s\n", json_object_get_string(my_string));
|
|
printf("my_string.to_string()=%s\n", json_object_to_json_string(my_string));
|
|
|
|
my_int = json_object_new_int(9);
|
|
printf("my_int=%d\n", json_object_get_int(my_int));
|
|
printf("my_int.to_string()=%s\n", json_object_to_json_string(my_int));
|
|
|
|
my_array = json_object_new_array();
|
|
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_add(my_array, json_object_new_int(3));
|
|
json_object_array_put_idx(my_array, 4, json_object_new_int(5));
|
|
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[%llu]=%s\n", (unsigned long long)i, json_object_to_json_string(obj));
|
|
}
|
|
printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));
|
|
|
|
json_object_put(my_array);
|
|
|
|
test_array_del_idx();
|
|
|
|
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[%llu]=%s\n", (unsigned long long)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[%llu]=%s\n", (unsigned long long)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();
|
|
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, "bool0", json_object_new_boolean(0));
|
|
json_object_object_add(my_object, "bool1", json_object_new_boolean(1));
|
|
json_object_object_add(my_object, "baz", json_object_new_string("bang"));
|
|
|
|
json_object *baz_obj = json_object_new_string("fark");
|
|
json_object_get(baz_obj);
|
|
json_object_object_add(my_object, "baz", baz_obj);
|
|
json_object_object_del(my_object, "baz");
|
|
|
|
/* baz_obj should still be valid */
|
|
printf("baz_obj.to_string()=%s\n", json_object_to_json_string(baz_obj));
|
|
json_object_put(baz_obj);
|
|
|
|
/*json_object_object_add(my_object, "arr", my_array);*/
|
|
printf("my_object=\n");
|
|
json_object_object_foreach(my_object, key, val)
|
|
{
|
|
printf("\t%s: %s\n", key, json_object_to_json_string(val));
|
|
}
|
|
printf("my_object.to_string()=%s\n", json_object_to_json_string(my_object));
|
|
|
|
json_object_put(my_string);
|
|
json_object_put(my_int);
|
|
json_object_put(my_object);
|
|
json_object_put(my_array);
|
|
|
|
return 0;
|
|
}
|