QMI: add uqmi tool with all depends
[tomato.git] / release / src / router / libjson-c / tests / test_set_serializer.c
blob0f122af2375eaa92fca865b01b1aab7f076f687f
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
5 #include "json.h"
6 #include "printbuf.h"
8 struct myinfo {
9 int value;
12 static int freeit_was_called = 0;
13 static void freeit(json_object *jso, void *userdata)
15 struct myinfo *info = userdata;
16 printf("freeit, value=%d\n", info->value);
17 // Don't actually free anything here, the userdata is stack allocated.
18 freeit_was_called = 1;
20 static int custom_serializer(struct json_object *o,
21 struct printbuf *pb,
22 int level,
23 int flags)
25 sprintbuf(pb, "Custom Output");
26 return 0;
29 int main(int argc, char **argv)
31 json_object *my_object;
33 MC_SET_DEBUG(1);
35 printf("Test setting, then resetting a custom serializer:\n");
36 my_object = json_object_new_object();
37 json_object_object_add(my_object, "abc", json_object_new_int(12));
38 json_object_object_add(my_object, "foo", json_object_new_string("bar"));
40 printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
42 struct myinfo userdata = { .value = 123 };
43 json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
45 printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object));
47 printf("Next line of output should be from the custom freeit function:\n");
48 freeit_was_called = 0;
49 json_object_set_serializer(my_object, NULL, NULL, NULL);
50 assert(freeit_was_called);
52 printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
54 json_object_put(my_object);
56 // ============================================
58 my_object = json_object_new_object();
59 printf("Check that the custom serializer isn't free'd until the last json_object_put:\n");
60 json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
61 json_object_get(my_object);
62 json_object_put(my_object);
63 printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object));
64 printf("Next line of output should be from the custom freeit function:\n");
66 freeit_was_called = 0;
67 json_object_put(my_object);
68 assert(freeit_was_called);
70 return 0;