test_deep_copy.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stddef.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <errno.h>
  7. #include <time.h>
  8. #include "json.h"
  9. #include "printbuf.h"
  10. static void do_benchmark(json_object *src1);
  11. static const char *json_str1 =
  12. "{"
  13. " \"glossary\": {"
  14. " \"title\": \"example glossary\","
  15. " \"GlossDiv\": {"
  16. " \"title\": \"S\","
  17. " \"null_obj\": null, "
  18. " \"GlossList\": {"
  19. " \"GlossEntry\": {"
  20. " \"ID\": \"SGML\","
  21. " \"SortAs\": \"SGML\","
  22. " \"GlossTerm\": \"Standard Generalized Markup Language\","
  23. " \"Acronym\": \"SGML\","
  24. " \"Abbrev\": \"ISO 8879:1986\","
  25. " \"GlossDef\": {"
  26. " \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\","
  27. " \"GlossSeeAlso\": [\"GML\", \"XML\"]"
  28. " },"
  29. " \"GlossSee\": \"markup\""
  30. " }"
  31. " }"
  32. " }"
  33. " }"
  34. "}";
  35. static const char *json_str2 =
  36. "{\"menu\": {"
  37. " \"header\": \"SVG Viewer\","
  38. " \"items\": ["
  39. " {\"id\": \"Open\"},"
  40. " {\"id\": \"OpenNew\", \"label\": \"Open New\"},"
  41. " null,"
  42. " {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},"
  43. " {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},"
  44. " {\"id\": \"OriginalView\", \"label\": \"Original View\"},"
  45. " null,"
  46. " {\"id\": \"Quality\", \"another_null\": null},"
  47. " {\"id\": \"Pause\"},"
  48. " {\"id\": \"Mute\"},"
  49. " null,"
  50. " {\"id\": \"Find\", \"label\": \"Find...\"},"
  51. " {\"id\": \"FindAgain\", \"label\": \"Find Again\"},"
  52. " {\"id\": \"Copy\"},"
  53. " {\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},"
  54. " {\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},"
  55. " {\"id\": \"ViewSVG\", \"label\": \"View SVG\"},"
  56. " {\"id\": \"ViewSource\", \"label\": \"View Source\"},"
  57. " {\"id\": \"SaveAs\", \"label\": \"Save As\"},"
  58. " null,"
  59. " {\"id\": \"Help\"},"
  60. " {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}"
  61. " ]"
  62. "}}";
  63. static const char *json_str3 =
  64. "{\"menu\": {"
  65. " \"id\": \"file\","
  66. " \"value\": \"File\","
  67. " \"popup\": {"
  68. " \"menuitem\": ["
  69. " {\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},"
  70. " {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},"
  71. " {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}"
  72. " ]"
  73. " }"
  74. "}}";
  75. json_object_to_json_string_fn my_custom_serializer;
  76. int my_custom_serializer(struct json_object *jso, struct printbuf *pb, int level, int flags)
  77. {
  78. sprintbuf(pb, "OTHER");
  79. return 0;
  80. }
  81. json_c_shallow_copy_fn my_shallow_copy;
  82. int my_shallow_copy(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
  83. {
  84. int rc;
  85. rc = json_c_shallow_copy_default(src, parent, key, index, dst);
  86. if (rc < 0)
  87. return rc;
  88. if (key != NULL && strcmp(key, "with_serializer") == 0)
  89. {
  90. printf("CALLED: my_shallow_copy on with_serializer object\n");
  91. void *userdata = json_object_get_userdata(src);
  92. json_object_set_serializer(*dst, my_custom_serializer, userdata, NULL);
  93. return 2;
  94. }
  95. return rc;
  96. }
  97. int main(int argc, char **argv)
  98. {
  99. struct json_object *src1, *src2, *src3;
  100. struct json_object *dst1 = NULL, *dst2 = NULL, *dst3 = NULL;
  101. int benchmark = 0;
  102. if (argc > 1 && strcmp(argv[1], "--benchmark") == 0)
  103. {
  104. benchmark = 1;
  105. }
  106. src1 = json_tokener_parse(json_str1);
  107. src2 = json_tokener_parse(json_str2);
  108. src3 = json_tokener_parse(json_str3);
  109. assert(src1 != NULL);
  110. assert(src1 != NULL);
  111. assert(src3 != NULL);
  112. printf("PASSED - loaded input data\n");
  113. /* do this 3 times to make sure overwriting it works */
  114. assert(0 == json_object_deep_copy(src1, &dst1, NULL));
  115. assert(0 == json_object_deep_copy(src2, &dst2, NULL));
  116. assert(0 == json_object_deep_copy(src3, &dst3, NULL));
  117. printf("PASSED - all json_object_deep_copy() returned succesful\n");
  118. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  119. assert(errno == EINVAL);
  120. assert(-1 == json_object_deep_copy(src2, &dst2, NULL));
  121. assert(errno == EINVAL);
  122. assert(-1 == json_object_deep_copy(src3, &dst3, NULL));
  123. assert(errno == EINVAL);
  124. printf("PASSED - all json_object_deep_copy() returned EINVAL for non-null pointer\n");
  125. assert(1 == json_object_equal(src1, dst1));
  126. assert(1 == json_object_equal(src2, dst2));
  127. assert(1 == json_object_equal(src3, dst3));
  128. printf("PASSED - all json_object_equal() tests returned succesful\n");
  129. assert(0 == strcmp(json_object_to_json_string_ext(src1, JSON_C_TO_STRING_PRETTY),
  130. json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY)));
  131. assert(0 == strcmp(json_object_to_json_string_ext(src2, JSON_C_TO_STRING_PRETTY),
  132. json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY)));
  133. assert(0 == strcmp(json_object_to_json_string_ext(src3, JSON_C_TO_STRING_PRETTY),
  134. json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY)));
  135. printf("PASSED - comparison of string output\n");
  136. json_object_get(dst1);
  137. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  138. assert(errno == EINVAL);
  139. json_object_put(dst1);
  140. printf("PASSED - trying to overrwrite an object that has refcount > 1");
  141. printf("\nPrinting JSON objects for visual inspection\n");
  142. printf("------------------------------------------------\n");
  143. printf(" JSON1\n");
  144. printf("%s\n", json_object_to_json_string_ext(dst1, JSON_C_TO_STRING_PRETTY));
  145. printf("------------------------------------------------\n");
  146. printf("------------------------------------------------\n");
  147. printf(" JSON2\n");
  148. printf("%s\n", json_object_to_json_string_ext(dst2, JSON_C_TO_STRING_PRETTY));
  149. printf("------------------------------------------------\n");
  150. printf("------------------------------------------------\n");
  151. printf(" JSON3\n");
  152. printf("------------------------------------------------\n");
  153. printf("%s\n", json_object_to_json_string_ext(dst3, JSON_C_TO_STRING_PRETTY));
  154. printf("------------------------------------------------\n");
  155. json_object_put(dst1);
  156. json_object_put(dst2);
  157. json_object_put(dst3);
  158. printf("\nTesting deep_copy with a custom serializer set\n");
  159. json_object *with_serializer = json_object_new_string("notemitted");
  160. json_object_set_serializer(with_serializer, my_custom_serializer, "dummy userdata", NULL);
  161. json_object_object_add(src1, "with_serializer", with_serializer);
  162. dst1 = NULL;
  163. /* With a custom serializer in use, a custom shallow_copy function must also be used */
  164. assert(-1 == json_object_deep_copy(src1, &dst1, NULL));
  165. assert(0 == json_object_deep_copy(src1, &dst1, my_shallow_copy));
  166. json_object *dest_with_serializer = json_object_object_get(dst1, "with_serializer");
  167. assert(dest_with_serializer != NULL);
  168. char *dst_userdata = json_object_get_userdata(dest_with_serializer);
  169. assert(strcmp(dst_userdata, "dummy userdata") == 0);
  170. const char *special_output = json_object_to_json_string(dest_with_serializer);
  171. assert(strcmp(special_output, "OTHER") == 0);
  172. printf("\ndeep_copy with custom serializer worked OK.\n");
  173. json_object_put(dst1);
  174. if (benchmark)
  175. {
  176. do_benchmark(src2);
  177. }
  178. json_object_put(src1);
  179. json_object_put(src2);
  180. json_object_put(src3);
  181. return 0;
  182. }
  183. static void do_benchmark(json_object *src2)
  184. {
  185. json_object *dst2 = NULL;
  186. int ii;
  187. /**
  188. * The numbers that I got are:
  189. * BENCHMARK - 1000000 iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took 71 seconds
  190. * BENCHMARK - 1000000 iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took 29 seconds
  191. */
  192. int iterations = 1000000;
  193. time_t start = time(NULL);
  194. start = time(NULL);
  195. for (ii = 0; ii < iterations; ii++) {
  196. dst2 = json_tokener_parse(json_object_get_string(src2));
  197. json_object_put(dst2);
  198. }
  199. printf("BENCHMARK - %d iterations of 'dst2 = json_tokener_parse(json_object_get_string(src2))' took %d seconds\n", iterations, (int)(time(NULL) - start));
  200. start = time(NULL);
  201. dst2 = NULL;
  202. for (ii = 0; ii < iterations; ii++) {
  203. json_object_deep_copy(src2, &dst2, NULL);
  204. json_object_put(dst2);
  205. dst2 = NULL;
  206. }
  207. printf("BENCHMARK - %d iterations of 'json_object_deep_copy(src2, &dst2, NULL)' took %d seconds\n", iterations, (int)(time(NULL) - start));
  208. }