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

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