• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/json-c/tests/test_set_serializer.c

  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 {
 10         int value;
 11 };
 12 
 13 static int freeit_was_called = 0;
 14 static void freeit(json_object *jso, void *userdata)
 15 {
 16         struct myinfo *info = userdata;
 17         printf("freeit, value=%d\n", info->value);
 18         // Don't actually free anything here, the userdata is stack allocated.
 19         freeit_was_called = 1;
 20 }
 21 static int custom_serializer(struct json_object *o, struct printbuf *pb, int level, int flags)
 22 {
 23         sprintbuf(pb, "Custom Output");
 24         return 0;
 25 }
 26 
 27 int main(int argc, char **argv)
 28 {
 29         json_object *my_object, *my_sub_object;
 30 
 31         MC_SET_DEBUG(1);
 32 
 33         printf("Test setting, then resetting a custom serializer:\n");
 34         my_object = json_object_new_object();
 35         json_object_object_add(my_object, "abc", json_object_new_int(12));
 36         json_object_object_add(my_object, "foo", json_object_new_string("bar"));
 37 
 38         printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
 39 
 40         struct myinfo userdata = {.value = 123};
 41         json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
 42 
 43         printf("my_object.to_string(custom serializer)=%s\n",
 44                json_object_to_json_string(my_object));
 45 
 46         printf("Next line of output should be from the custom freeit function:\n");
 47         freeit_was_called = 0;
 48         json_object_set_serializer(my_object, NULL, NULL, NULL);
 49         assert(freeit_was_called);
 50 
 51         printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
 52 
 53         json_object_put(my_object);
 54 
 55         // ============================================
 56 
 57         my_object = json_object_new_object();
 58         printf("Check that the custom serializer isn't free'd until the last json_object_put:\n");
 59         json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
 60         json_object_get(my_object);
 61         json_object_put(my_object);
 62         printf("my_object.to_string(custom serializer)=%s\n",
 63                json_object_to_json_string(my_object));
 64         printf("Next line of output should be from the custom freeit function:\n");
 65 
 66         freeit_was_called = 0;
 67         json_object_put(my_object);
 68         assert(freeit_was_called);
 69 
 70         // ============================================
 71 
 72         my_object = json_object_new_object();
 73         my_sub_object = json_object_new_double(1.0);
 74         json_object_object_add(my_object, "double", my_sub_object);
 75         printf("Check that the custom serializer does not include nul byte:\n");
 76         json_object_set_serializer(my_sub_object, json_object_double_to_json_string, "%125.0f,", NULL);
 77         printf("my_object.to_string(custom serializer)=%s\n",
 78                json_object_to_json_string_ext(my_object, JSON_C_TO_STRING_NOZERO));
 79 
 80         json_object_put(my_object);
 81 
 82         return 0;
 83 }
 84 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt