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_deep_copy.c 8.5 kB

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