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.4 kB

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