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