JsonVariant.cpp 2.5 KB

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