JsonVariant.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #include <limits>
  7. template <typename T>
  8. void check(T value, const std::string& expected) {
  9. DynamicJsonDocument doc(4096);
  10. doc.to<JsonVariant>().set(value);
  11. char buffer[256] = "";
  12. size_t returnValue = serializeJson(doc, buffer, sizeof(buffer));
  13. REQUIRE(expected == buffer);
  14. REQUIRE(expected.size() == returnValue);
  15. }
  16. TEST_CASE("serializeJson(JsonVariant)") {
  17. SECTION("Undefined") {
  18. check(JsonVariant(), "null");
  19. }
  20. SECTION("Null string") {
  21. check(static_cast<char*>(0), "null");
  22. }
  23. SECTION("const char*") {
  24. check("hello", "\"hello\"");
  25. }
  26. SECTION("string") {
  27. check(std::string("hello"), "\"hello\"");
  28. SECTION("Escape quotation mark") {
  29. check(std::string("hello \"world\""), "\"hello \\\"world\\\"\"");
  30. }
  31. SECTION("Escape reverse solidus") {
  32. check(std::string("hello\\world"), "\"hello\\\\world\"");
  33. }
  34. SECTION("Don't escape solidus") {
  35. check(std::string("fifty/fifty"), "\"fifty/fifty\"");
  36. }
  37. SECTION("Escape backspace") {
  38. check(std::string("hello\bworld"), "\"hello\\bworld\"");
  39. }
  40. SECTION("Escape formfeed") {
  41. check(std::string("hello\fworld"), "\"hello\\fworld\"");
  42. }
  43. SECTION("Escape linefeed") {
  44. check(std::string("hello\nworld"), "\"hello\\nworld\"");
  45. }
  46. SECTION("Escape carriage return") {
  47. check(std::string("hello\rworld"), "\"hello\\rworld\"");
  48. }
  49. SECTION("Escape tab") {
  50. check(std::string("hello\tworld"), "\"hello\\tworld\"");
  51. }
  52. SECTION("NUL char") {
  53. check(std::string("hello\0world", 11), "\"hello\\u0000world\"");
  54. }
  55. }
  56. SECTION("SerializedValue<const char*>") {
  57. check(serialized("[1,2]"), "[1,2]");
  58. }
  59. SECTION("SerializedValue<std::string>") {
  60. check(serialized(std::string("[1,2]")), "[1,2]");
  61. }
  62. SECTION("Double") {
  63. check(3.1415927, "3.1415927");
  64. }
  65. SECTION("Zero") {
  66. check(0, "0");
  67. }
  68. SECTION("Integer") {
  69. check(42, "42");
  70. }
  71. SECTION("NegativeLong") {
  72. check(-42, "-42");
  73. }
  74. SECTION("UnsignedLong") {
  75. check(4294967295UL, "4294967295");
  76. }
  77. SECTION("True") {
  78. check(true, "true");
  79. }
  80. SECTION("OneFalse") {
  81. check(false, "false");
  82. }
  83. #if ARDUINOJSON_USE_LONG_LONG
  84. SECTION("NegativeInt64") {
  85. check(-9223372036854775807 - 1, "-9223372036854775808");
  86. }
  87. SECTION("PositiveInt64") {
  88. check(9223372036854775807, "9223372036854775807");
  89. }
  90. SECTION("UInt64") {
  91. check(18446744073709551615U, "18446744073709551615");
  92. }
  93. #endif
  94. }