From 5fa446d3f5c0648f5d41fbe709b35ffe03fa6318 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 27 Sep 2014 14:59:02 +0200 Subject: [PATCH] Test that a double can be stored in a JsonObject --- srcs/JsonNode.h | 1 + srcs/JsonObject.cpp | 2 +- srcs/JsonValue.cpp | 21 +++++++++++++-------- srcs/JsonValue.h | 2 ++ tests/JsonObjectTests.cpp | 15 ++++++++++++++- 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/srcs/JsonNode.h b/srcs/JsonNode.h index 4f9101fe..53085109 100644 --- a/srcs/JsonNode.h +++ b/srcs/JsonNode.h @@ -24,6 +24,7 @@ struct JsonNode union { + double asDouble; int asInteger; struct diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index 7f31e38c..e992febb 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -2,7 +2,7 @@ #include "JsonObject.h" #include "JsonValue.h" #include "JsonNode.h" -#include +#include // for strcmp //JsonValue& JsonObject::operator[](char const* key) //{ diff --git a/srcs/JsonValue.cpp b/srcs/JsonValue.cpp index dcfbc9ce..0e5b7deb 100644 --- a/srcs/JsonValue.cpp +++ b/srcs/JsonValue.cpp @@ -2,10 +2,13 @@ #include "JsonNode.h" #include "JsonValue.h" -//void JsonValue::operator=(JsonObject const& object) -//{ -// _node = object._node; -//} +void JsonValue::operator=(double value) +{ + if (!_node) return; + + _node->type = JSON_DOUBLE_2_DECIMALS; + _node->content.asDouble = value; +} void JsonValue::operator=(int value) { @@ -15,10 +18,12 @@ void JsonValue::operator=(int value) _node->content.asInteger = value; } -//JsonValue::operator JsonObject() -//{ -// return JsonObject(_buffer, _node); -//} +JsonValue::operator double() +{ + if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0; + + return _node->content.asDouble; +} JsonValue::operator int() { diff --git a/srcs/JsonValue.h b/srcs/JsonValue.h index 692de4fd..39e5b2c7 100644 --- a/srcs/JsonValue.h +++ b/srcs/JsonValue.h @@ -14,9 +14,11 @@ public: } // void operator=(const JsonObject& object); + void operator=(double); void operator=(int); // operator JsonObject(); + operator double(); operator int(); private: diff --git a/tests/JsonObjectTests.cpp b/tests/JsonObjectTests.cpp index ca7e97d3..4f528615 100644 --- a/tests/JsonObjectTests.cpp +++ b/tests/JsonObjectTests.cpp @@ -28,7 +28,7 @@ TEST(JsonObjectTests, WhenTheSameValueIsAddedTwice_ThenSizeIsOnlyIncreasedByOne) EXPECT_EQ(1, object.size()); } -TEST(JsonObjectTests, WhenAnIntegerIsStore_TheSameIntegerIsRetreived) +TEST(JsonObjectTests, GivenAnIntegerStored_WhenRetreivingTheValue_ThenTheValueIsTheSame) { StaticJsonBuffer<42> json; @@ -39,4 +39,17 @@ TEST(JsonObjectTests, WhenAnIntegerIsStore_TheSameIntegerIsRetreived) EXPECT_EQ(123, (int) object["hello"]); EXPECT_EQ(456, (int) object["world"]); +} + +TEST(JsonObjectTests, GivenAnDoubleStored_WhenRetreivingTheValue_ThenTheValueIsTheSame) +{ + StaticJsonBuffer<42> json; + + JsonObject object = json.createObject(); + + object["hello"] = 123.45; + object["world"] = 456.78; + + EXPECT_EQ(123.45, (double) object["hello"]); + EXPECT_EQ(456.78, (double) object["world"]); } \ No newline at end of file