test_dump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2009-2012 Petri Lehtinen <petri@digip.org>
  3. *
  4. * Jansson is free software; you can redistribute it and/or modify
  5. * it under the terms of the MIT license. See LICENSE for details.
  6. */
  7. #include <jansson.h>
  8. #include <string.h>
  9. #include "util.h"
  10. static void encode_twice()
  11. {
  12. /* Encode an empty object/array, add an item, encode again */
  13. json_t *json;
  14. char *result;
  15. json = json_object();
  16. result = json_dumps(json, 0);
  17. if(!result || strcmp(result, "{}"))
  18. fail("json_dumps failed");
  19. free(result);
  20. json_object_set_new(json, "foo", json_integer(5));
  21. result = json_dumps(json, 0);
  22. if(!result || strcmp(result, "{\"foo\": 5}"))
  23. fail("json_dumps failed");
  24. free(result);
  25. json_decref(json);
  26. json = json_array();
  27. result = json_dumps(json, 0);
  28. if(!result || strcmp(result, "[]"))
  29. fail("json_dumps failed");
  30. free(result);
  31. json_array_append_new(json, json_integer(5));
  32. result = json_dumps(json, 0);
  33. if(!result || strcmp(result, "[5]"))
  34. fail("json_dumps failed");
  35. free(result);
  36. json_decref(json);
  37. }
  38. static void circular_references()
  39. {
  40. /* Construct a JSON object/array with a circular reference:
  41. object: {"a": {"b": {"c": <circular reference to $.a>}}}
  42. array: [[[<circular reference to the $[0] array>]]]
  43. Encode it, remove the circular reference and encode again.
  44. */
  45. json_t *json;
  46. char *result;
  47. json = json_object();
  48. json_object_set_new(json, "a", json_object());
  49. json_object_set_new(json_object_get(json, "a"), "b", json_object());
  50. json_object_set(json_object_get(json_object_get(json, "a"), "b"), "c",
  51. json_object_get(json, "a"));
  52. if(json_dumps(json, 0))
  53. fail("json_dumps encoded a circular reference!");
  54. json_object_del(json_object_get(json_object_get(json, "a"), "b"), "c");
  55. result = json_dumps(json, 0);
  56. if(!result || strcmp(result, "{\"a\": {\"b\": {}}}"))
  57. fail("json_dumps failed!");
  58. free(result);
  59. json_decref(json);
  60. json = json_array();
  61. json_array_append_new(json, json_array());
  62. json_array_append_new(json_array_get(json, 0), json_array());
  63. json_array_append(json_array_get(json_array_get(json, 0), 0),
  64. json_array_get(json, 0));
  65. if(json_dumps(json, 0))
  66. fail("json_dumps encoded a circular reference!");
  67. json_array_remove(json_array_get(json_array_get(json, 0), 0), 0);
  68. result = json_dumps(json, 0);
  69. if(!result || strcmp(result, "[[[]]]"))
  70. fail("json_dumps failed!");
  71. free(result);
  72. json_decref(json);
  73. }
  74. static void encode_other_than_array_or_object()
  75. {
  76. /* Encoding anything other than array or object should only
  77. * succeed if the JSON_ENCODE_ANY flag is used */
  78. json_t *json;
  79. FILE *fp = NULL;
  80. char *result;
  81. json = json_string("foo");
  82. if(json_dumps(json, 0) != NULL)
  83. fail("json_dumps encoded a string!");
  84. if(json_dumpf(json, fp, 0) == 0)
  85. fail("json_dumpf encoded a string!");
  86. result = json_dumps(json, JSON_ENCODE_ANY);
  87. if(!result || strcmp(result, "\"foo\"") != 0)
  88. fail("json_dumps failed to encode a string with JSON_ENCODE_ANY");
  89. free(result);
  90. json_decref(json);
  91. json = json_integer(42);
  92. if(json_dumps(json, 0) != NULL)
  93. fail("json_dumps encoded an integer!");
  94. if(json_dumpf(json, fp, 0) == 0)
  95. fail("json_dumpf encoded an integer!");
  96. result = json_dumps(json, JSON_ENCODE_ANY);
  97. if(!result || strcmp(result, "42") != 0)
  98. fail("json_dumps failed to encode an integer with JSON_ENCODE_ANY");
  99. free(result);
  100. json_decref(json);
  101. }
  102. static void escape_slashes()
  103. {
  104. /* Test dump escaping slashes */
  105. json_t *json;
  106. char *result;
  107. json = json_object();
  108. json_object_set_new(json, "url", json_string("https://github.com/akheron/jansson"));
  109. result = json_dumps(json, 0);
  110. if(!result || strcmp(result, "{\"url\": \"https://github.com/akheron/jansson\"}"))
  111. fail("json_dumps failed to not escape slashes");
  112. free(result);
  113. result = json_dumps(json, JSON_ESCAPE_SLASH);
  114. if(!result || strcmp(result, "{\"url\": \"https:\\/\\/github.com\\/akheron\\/jansson\"}"))
  115. fail("json_dumps failed to escape slashes");
  116. free(result);
  117. json_decref(json);
  118. }
  119. static void run_tests()
  120. {
  121. encode_twice();
  122. circular_references();
  123. encode_other_than_array_or_object();
  124. escape_slashes();
  125. }
粤ICP备19079148号