MACSio  0.9
Multi-purpose, Application-Centric, Scalable I/O Proxy App
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
test_set_serializer.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <string.h>
4 
5 #include "json.h"
6 #include "printbuf.h"
7 
8 struct myinfo {
9  int value;
10 };
11 
12 static int freeit_was_called = 0;
13 static void freeit(json_object *jso, void *userdata)
14 {
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.
19 }
20 static int custom_serializer(struct json_object *o,
21  struct printbuf *pb,
22  int level,
23  int flags)
24 {
25  sprintbuf(pb, "Custom Output");
26  return 0;
27 }
28 
29 int main(int argc, char **argv)
30 {
31  json_object *my_object;
32 
33  MC_SET_DEBUG(1);
34 
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"));
39 
40  printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
41 
42  struct myinfo userdata = { .value = 123 };
43  json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
44 
45  printf("my_object.to_string(custom serializer)=%s\n", json_object_to_json_string(my_object));
46 
47  printf("Next line of output should be from the custom freeit function:\n");
49  json_object_set_serializer(my_object, NULL, NULL, NULL);
50  assert(freeit_was_called);
51 
52  printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
53 
54  json_object_put(my_object);
55 
56  // ============================================
57 
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");
65 
67  json_object_put(my_object);
68  assert(freeit_was_called);
69 
70  return 0;
71 }
struct json_object * json_object_new_string(const char *s)
Definition: json_object.c:727
struct json_object * json_object_new_object(void)
Definition: json_object.c:369
int sprintbuf(struct printbuf *p, const char *msg,...)
Definition: printbuf.c:152
static int custom_serializer(struct json_object *o, struct printbuf *pb, int level, int flags)
void json_object_set_serializer(json_object *jso, json_object_to_json_string_fn to_string_func, void *userdata, json_object_delete_fn *user_delete)
Definition: json_object.c:220
struct json_object * json_object_new_int(int32_t i)
Definition: json_object.c:495
int main(int argc, char **argv)
#define MC_SET_DEBUG(x)
Definition: debug.h:60
static int freeit_was_called
struct json_object * json_object_get(struct json_object *obj)
Definition: json_object.c:147
int json_object_put(struct json_object *obj)
Definition: json_object.c:155
static void freeit(json_object *jso, void *userdata)
const char * json_object_to_json_string(struct json_object *obj)
Definition: json_object.c:295
void json_object_object_add(struct json_object *obj, const char *key, struct json_object *val)
Definition: json_object.c:391