mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-21 06:22:23 +02:00
Test that boolean values can be stored in a JsonObject
This commit is contained in:
@ -7,8 +7,7 @@ enum JsonNodeType
|
||||
JSON_ARRAY,
|
||||
JSON_OBJECT,
|
||||
JSON_KEY,
|
||||
JSON_TRUE,
|
||||
JSON_FALSE,
|
||||
JSON_BOOLEAN,
|
||||
JSON_STRING,
|
||||
JSON_INTEGER,
|
||||
JSON_DOUBLE_0_DECIMALS,
|
||||
@ -24,6 +23,7 @@ struct JsonNode
|
||||
|
||||
union
|
||||
{
|
||||
bool asBoolean;
|
||||
double asDouble;
|
||||
int asInteger;
|
||||
|
||||
|
@ -6,22 +6,6 @@ struct JsonNode;
|
||||
|
||||
class JsonObject
|
||||
{
|
||||
// friend class JsonValue;
|
||||
//
|
||||
//public:
|
||||
// JsonObject(JsonBuffer& buffer, JsonNode& node)
|
||||
// : _buffer(buffer), _node(node)
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// JsonObject createObject(const char* key)
|
||||
// {
|
||||
// JsonObject innerObject = _buffer.createObject();
|
||||
// addNodeAt(key, innerObject._node);
|
||||
// return innerObject;
|
||||
// }
|
||||
//
|
||||
|
||||
public:
|
||||
|
||||
JsonObject(JsonBuffer* buffer, JsonNode* node)
|
||||
@ -37,8 +21,4 @@ private:
|
||||
JsonBuffer* _buffer;
|
||||
JsonNode* _node;
|
||||
JsonNode* getOrCreateNodeAt(char const* key);
|
||||
|
||||
//
|
||||
// // TODO: pull up
|
||||
// void appendChild(JsonNode& newChild);
|
||||
};
|
@ -2,6 +2,14 @@
|
||||
#include "JsonNode.h"
|
||||
#include "JsonValue.h"
|
||||
|
||||
void JsonValue::operator=(bool value)
|
||||
{
|
||||
if (!_node) return;
|
||||
|
||||
_node->type = JSON_BOOLEAN;
|
||||
_node->content.asBoolean = value;
|
||||
}
|
||||
|
||||
void JsonValue::operator=(double value)
|
||||
{
|
||||
if (!_node) return;
|
||||
@ -18,6 +26,13 @@ void JsonValue::operator=(int value)
|
||||
_node->content.asInteger = value;
|
||||
}
|
||||
|
||||
JsonValue::operator bool()
|
||||
{
|
||||
if (!_node || _node->type != JSON_BOOLEAN) return 0;
|
||||
|
||||
return _node->content.asBoolean;
|
||||
}
|
||||
|
||||
JsonValue::operator double()
|
||||
{
|
||||
if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0;
|
||||
|
@ -8,16 +8,16 @@ class JsonValue
|
||||
{
|
||||
public:
|
||||
|
||||
JsonValue(JsonNode* node)
|
||||
explicit JsonValue(JsonNode* node)
|
||||
: _node(node)
|
||||
{
|
||||
}
|
||||
|
||||
// void operator=(const JsonObject& object);
|
||||
void operator=(bool);
|
||||
void operator=(double);
|
||||
void operator=(int);
|
||||
|
||||
// operator JsonObject();
|
||||
operator bool();
|
||||
operator double();
|
||||
operator int();
|
||||
|
||||
|
@ -53,3 +53,17 @@ TEST(JsonObjectTests, GivenAnDoubleStored_WhenRetreivingTheValue_ThenTheValueIsT
|
||||
EXPECT_EQ(123.45, (double) object["hello"]);
|
||||
EXPECT_EQ(456.78, (double) object["world"]);
|
||||
}
|
||||
|
||||
|
||||
TEST(JsonObjectTests, GivenABooleanStored_WhenRetreivingTheValue_ThenTheValueIsTheSame)
|
||||
{
|
||||
StaticJsonBuffer<42> json;
|
||||
|
||||
JsonObject object = json.createObject();
|
||||
|
||||
object["hello"] = true;
|
||||
object["world"] = false;
|
||||
|
||||
EXPECT_TRUE((bool) object["hello"]);
|
||||
EXPECT_FALSE((bool) object["world"]);
|
||||
}
|
Reference in New Issue
Block a user