forked from bblanchon/ArduinoJson
Fixed JsonVariant::is<bool>()
that was incorrectly returning false (issue #214)
This commit is contained in:
@ -5,6 +5,7 @@ HEAD
|
||||
----
|
||||
|
||||
* Made the library compatible with [PlatformIO](http://platformio.org/) (issue #181)
|
||||
* Fixed `JsonVariant::is<bool>()` that was incorrectly returning false (issue #214)
|
||||
|
||||
v5.0.7
|
||||
------
|
||||
|
@ -174,10 +174,8 @@ bool JsonVariant::is<signed long>() const;
|
||||
template <> // in .cpp
|
||||
bool JsonVariant::is<double>() const;
|
||||
|
||||
template <>
|
||||
inline bool JsonVariant::is<bool>() const {
|
||||
return _type == Internals::JSON_BOOLEAN;
|
||||
}
|
||||
template <> // int .cpp
|
||||
bool JsonVariant::is<bool>() const;
|
||||
|
||||
template <>
|
||||
inline bool JsonVariant::is<char const *>() const {
|
||||
|
@ -86,6 +86,16 @@ String JsonVariant::as<String>() const {
|
||||
return s;
|
||||
}
|
||||
|
||||
template <>
|
||||
bool JsonVariant::is<bool>() const {
|
||||
if (_type == JSON_BOOLEAN) return true;
|
||||
|
||||
if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
|
||||
|
||||
return !strcmp(_content.asString, "true") ||
|
||||
!strcmp(_content.asString, "false");
|
||||
}
|
||||
|
||||
template <>
|
||||
bool JsonVariant::is<signed long>() const {
|
||||
if (_type == JSON_INTEGER) return true;
|
||||
|
16
test/Issue214.cpp
Normal file
16
test/Issue214.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(Issue214, IsBool) {
|
||||
char json[] = "{\"ota\": {\"enabled\": true}}";
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(1)> jsonBuffer;
|
||||
JsonObject& parsedJson = jsonBuffer.parseObject(json);
|
||||
ASSERT_TRUE(parsedJson["ota"]["enabled"].is<bool>());
|
||||
}
|
@ -77,6 +77,26 @@ TEST(SUITE, StringIsInt) { assertIsNot<int>("42"); }
|
||||
TEST(SUITE, StringIsLong) { assertIsNot<long>("42"); }
|
||||
TEST(SUITE, StringIsString) { assertIs<const char*>("42"); }
|
||||
|
||||
TEST(SUITE, UnparsedTrueIsArra) { assertIsNot<JsonArray&>(Unparsed("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsBool) { assertIs<bool>(Unparsed("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsDouble) { assertIsNot<double>(Unparsed("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsFloat) { assertIsNot<float>(Unparsed("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsInt) { assertIsNot<int>(Unparsed("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsLong) { assertIsNot<long>(Unparsed("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsString) {
|
||||
assertIsNot<const char*>(Unparsed("true"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedFalseIsArra) { assertIsNot<JsonArray&>(Unparsed("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsBool) { assertIs<bool>(Unparsed("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsDouble) { assertIsNot<double>(Unparsed("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsFloat) { assertIsNot<float>(Unparsed("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsInt) { assertIsNot<int>(Unparsed("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsLong) { assertIsNot<long>(Unparsed("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsString) {
|
||||
assertIsNot<const char*>(Unparsed("false"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedIntIsArra) { assertIsNot<JsonArray&>(Unparsed("42")); }
|
||||
TEST(SUITE, UnparsedIntIsBool) { assertIsNot<bool>(Unparsed("42")); }
|
||||
TEST(SUITE, UnparsedIntIsDouble) { assertIsNot<double>(Unparsed("42")); }
|
||||
|
Reference in New Issue
Block a user