Skip to content

Commit 75e02b9

Browse files
committed
Fixed issue #998
1 parent c756952 commit 75e02b9

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

src/ArduinoJson/Operators/VariantComparisons.hpp

+12-8
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ struct Comparer;
1313

1414
template <typename T>
1515
struct Comparer<T, typename enable_if<IsString<T>::value>::type> {
16-
bool result;
1716
T comparand;
17+
bool result;
1818

19-
explicit Comparer(T value) : comparand(value) {}
19+
explicit Comparer(T value) : comparand(value), result(false) {}
2020

2121
void visitArray(const CollectionData &) {}
2222
void visitObject(const CollectionData &) {}
@@ -28,15 +28,17 @@ struct Comparer<T, typename enable_if<IsString<T>::value>::type> {
2828
void visitNegativeInteger(UInt) {}
2929
void visitPositiveInteger(UInt) {}
3030
void visitBoolean(bool) {}
31-
void visitNull() {}
31+
void visitNull() {
32+
result = adaptString(comparand).isNull();
33+
}
3234
};
3335

3436
template <typename T>
3537
struct ArithmeticComparer {
36-
bool result;
3738
T comparand;
39+
bool result;
3840

39-
explicit ArithmeticComparer(T value) : comparand(value) {}
41+
explicit ArithmeticComparer(T value) : comparand(value), result(false) {}
4042

4143
void visitArray(const CollectionData &) {}
4244
void visitObject(const CollectionData &) {}
@@ -114,7 +116,9 @@ class VariantComparisons {
114116
template <typename T>
115117
friend typename enable_if<is_simple_value<T>::value, bool>::type operator==(
116118
TVariant lhs, const T &rhs) {
117-
return lhs.template as<T>() == rhs;
119+
ArithmeticComparer<T> comparer(rhs);
120+
lhs.accept(comparer);
121+
return comparer.result;
118122
}
119123

120124
// const char* != TVariant
@@ -149,14 +153,14 @@ class VariantComparisons {
149153
template <typename T>
150154
friend typename enable_if<is_simple_value<T>::value, bool>::type operator!=(
151155
const T &lhs, TVariant rhs) {
152-
return lhs != rhs.template as<T>();
156+
return !operator==(lhs, rhs);
153157
}
154158

155159
// TVariant != bool/int/float
156160
template <typename T>
157161
friend typename enable_if<is_simple_value<T>::value, bool>::type operator!=(
158162
TVariant lhs, const T &rhs) {
159-
return lhs.template as<T>() != rhs;
163+
return !operator==(lhs, rhs);
160164
}
161165

162166
// bool/int/float < TVariant

test/JsonVariant/createNested.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ TEST_CASE("JsonVariant::createNestedObject()") {
1414

1515
SECTION("promotes to array") {
1616
JsonObject obj = variant.createNestedObject();
17-
obj["value"] = "42";
17+
obj["value"] = 42;
1818

1919
REQUIRE(variant.is<JsonArray>() == true);
2020
REQUIRE(variant[0]["value"] == 42);
@@ -23,7 +23,7 @@ TEST_CASE("JsonVariant::createNestedObject()") {
2323

2424
SECTION("works on MemberProxy") {
2525
JsonObject obj = variant["items"].createNestedObject();
26-
obj["value"] = "42";
26+
obj["value"] = 42;
2727

2828
REQUIRE(variant["items"][0]["value"] == 42);
2929
}
@@ -42,7 +42,7 @@ TEST_CASE("JsonVariant::createNestedArray()") {
4242

4343
SECTION("works on MemberProxy") {
4444
JsonArray arr = variant["items"].createNestedArray();
45-
arr.add("42");
45+
arr.add(42);
4646

4747
REQUIRE(variant["items"][0][0] == 42);
4848
}
@@ -54,15 +54,15 @@ TEST_CASE("JsonVariant::createNestedObject(key)") {
5454

5555
SECTION("promotes to object") {
5656
JsonObject obj = variant.createNestedObject("weather");
57-
obj["temp"] = "42";
57+
obj["temp"] = 42;
5858

5959
REQUIRE(variant.is<JsonObject>() == true);
6060
REQUIRE(variant["weather"]["temp"] == 42);
6161
}
6262

6363
SECTION("works on MemberProxy") {
6464
JsonObject obj = variant["status"].createNestedObject("weather");
65-
obj["temp"] = "42";
65+
obj["temp"] = 42;
6666

6767
REQUIRE(variant["status"]["weather"]["temp"] == 42);
6868
}
@@ -81,7 +81,7 @@ TEST_CASE("JsonVariant::createNestedArray(key)") {
8181

8282
SECTION("works on MemberProxy") {
8383
JsonArray arr = variant["weather"].createNestedArray("temp");
84-
arr.add("42");
84+
arr.add(42);
8585

8686
REQUIRE(variant["weather"]["temp"][0] == 42);
8787
}

0 commit comments

Comments
 (0)