You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_set_serializer.c 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifdef NDEBUG
  2. #undef NDEBUG
  3. #endif
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "json.h"
  8. #include "printbuf.h"
  9. struct myinfo
  10. {
  11. int value;
  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. int main(int argc, char **argv)
  27. {
  28. json_object *my_object, *my_sub_object;
  29. MC_SET_DEBUG(1);
  30. printf("Test setting, then resetting a custom serializer:\n");
  31. my_object = json_object_new_object();
  32. json_object_object_add(my_object, "abc", json_object_new_int(12));
  33. json_object_object_add(my_object, "foo", json_object_new_string("bar"));
  34. printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
  35. struct myinfo userdata = {.value = 123};
  36. json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
  37. printf("my_object.to_string(custom serializer)=%s\n",
  38. json_object_to_json_string(my_object));
  39. printf("Next line of output should be from the custom freeit function:\n");
  40. freeit_was_called = 0;
  41. json_object_set_serializer(my_object, NULL, NULL, NULL);
  42. assert(freeit_was_called);
  43. printf("my_object.to_string(standard)=%s\n", json_object_to_json_string(my_object));
  44. json_object_put(my_object);
  45. // ============================================
  46. my_object = json_object_new_object();
  47. printf("Check that the custom serializer isn't free'd until the last json_object_put:\n");
  48. json_object_set_serializer(my_object, custom_serializer, &userdata, freeit);
  49. json_object_get(my_object);
  50. json_object_put(my_object);
  51. printf("my_object.to_string(custom serializer)=%s\n",
  52. json_object_to_json_string(my_object));
  53. printf("Next line of output should be from the custom freeit function:\n");
  54. freeit_was_called = 0;
  55. json_object_put(my_object);
  56. assert(freeit_was_called);
  57. // ============================================
  58. my_object = json_object_new_object();
  59. my_sub_object = json_object_new_double(1.0);
  60. json_object_object_add(my_object, "double", my_sub_object);
  61. printf("Check that the custom serializer does not include nul byte:\n");
  62. #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
  63. json_object_set_serializer(my_sub_object, json_object_double_to_json_string, UNCONST("%125.0f"), NULL);
  64. printf("my_object.to_string(custom serializer)=%s\n",
  65. json_object_to_json_string_ext(my_object, JSON_C_TO_STRING_NOZERO));
  66. json_object_put(my_object);
  67. return 0;
  68. }