mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-20 22:12:31 +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_ARRAY,
|
||||||
JSON_OBJECT,
|
JSON_OBJECT,
|
||||||
JSON_KEY,
|
JSON_KEY,
|
||||||
JSON_TRUE,
|
JSON_BOOLEAN,
|
||||||
JSON_FALSE,
|
|
||||||
JSON_STRING,
|
JSON_STRING,
|
||||||
JSON_INTEGER,
|
JSON_INTEGER,
|
||||||
JSON_DOUBLE_0_DECIMALS,
|
JSON_DOUBLE_0_DECIMALS,
|
||||||
@ -24,6 +23,7 @@ struct JsonNode
|
|||||||
|
|
||||||
union
|
union
|
||||||
{
|
{
|
||||||
|
bool asBoolean;
|
||||||
double asDouble;
|
double asDouble;
|
||||||
int asInteger;
|
int asInteger;
|
||||||
|
|
||||||
|
@ -6,22 +6,6 @@ struct JsonNode;
|
|||||||
|
|
||||||
class JsonObject
|
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:
|
public:
|
||||||
|
|
||||||
JsonObject(JsonBuffer* buffer, JsonNode* node)
|
JsonObject(JsonBuffer* buffer, JsonNode* node)
|
||||||
@ -37,8 +21,4 @@ private:
|
|||||||
JsonBuffer* _buffer;
|
JsonBuffer* _buffer;
|
||||||
JsonNode* _node;
|
JsonNode* _node;
|
||||||
JsonNode* getOrCreateNodeAt(char const* key);
|
JsonNode* getOrCreateNodeAt(char const* key);
|
||||||
|
|
||||||
//
|
|
||||||
// // TODO: pull up
|
|
||||||
// void appendChild(JsonNode& newChild);
|
|
||||||
};
|
};
|
@ -2,6 +2,14 @@
|
|||||||
#include "JsonNode.h"
|
#include "JsonNode.h"
|
||||||
#include "JsonValue.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)
|
void JsonValue::operator=(double value)
|
||||||
{
|
{
|
||||||
if (!_node) return;
|
if (!_node) return;
|
||||||
@ -18,6 +26,13 @@ void JsonValue::operator=(int value)
|
|||||||
_node->content.asInteger = value;
|
_node->content.asInteger = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JsonValue::operator bool()
|
||||||
|
{
|
||||||
|
if (!_node || _node->type != JSON_BOOLEAN) return 0;
|
||||||
|
|
||||||
|
return _node->content.asBoolean;
|
||||||
|
}
|
||||||
|
|
||||||
JsonValue::operator double()
|
JsonValue::operator double()
|
||||||
{
|
{
|
||||||
if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0;
|
if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0;
|
||||||
|
@ -8,16 +8,16 @@ class JsonValue
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
JsonValue(JsonNode* node)
|
explicit JsonValue(JsonNode* node)
|
||||||
: _node(node)
|
: _node(node)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// void operator=(const JsonObject& object);
|
void operator=(bool);
|
||||||
void operator=(double);
|
void operator=(double);
|
||||||
void operator=(int);
|
void operator=(int);
|
||||||
|
|
||||||
// operator JsonObject();
|
operator bool();
|
||||||
operator double();
|
operator double();
|
||||||
operator int();
|
operator int();
|
||||||
|
|
||||||
|
@ -53,3 +53,17 @@ TEST(JsonObjectTests, GivenAnDoubleStored_WhenRetreivingTheValue_ThenTheValueIsT
|
|||||||
EXPECT_EQ(123.45, (double) object["hello"]);
|
EXPECT_EQ(123.45, (double) object["hello"]);
|
||||||
EXPECT_EQ(456.78, (double) object["world"]);
|
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