From a9a51ec1e2e1c9e96df757427ecb8e4d89754b7f Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Sat, 27 Sep 2014 15:24:16 +0200 Subject: [PATCH] Test that string can be stored in JsonObject --- srcs/JsonNode.h | 1 + srcs/JsonValue.cpp | 15 +++++++++++++++ srcs/JsonValue.h | 2 ++ tests/JsonObjectTests.cpp | 10 +++++++++- 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/srcs/JsonNode.h b/srcs/JsonNode.h index 0e6f58ca..516b3828 100644 --- a/srcs/JsonNode.h +++ b/srcs/JsonNode.h @@ -26,6 +26,7 @@ struct JsonNode bool asBoolean; double asDouble; int asInteger; + const char* asString; struct { diff --git a/srcs/JsonValue.cpp b/srcs/JsonValue.cpp index e9add453..212cf7de 100644 --- a/srcs/JsonValue.cpp +++ b/srcs/JsonValue.cpp @@ -10,6 +10,14 @@ void JsonValue::operator=(bool value) _node->content.asBoolean = value; } +void JsonValue::operator=(char const* value) +{ + if (!_node) return; + + _node->type = JSON_STRING; + _node->content.asString = value; +} + void JsonValue::operator=(double value) { if (!_node) return; @@ -33,6 +41,13 @@ JsonValue::operator bool() return _node->content.asBoolean; } +JsonValue::operator char const*() +{ + if (!_node || _node->type != JSON_STRING) return 0; + + return _node->content.asString; +} + JsonValue::operator double() { if (!_node || _node->type < JSON_DOUBLE_0_DECIMALS) return 0; diff --git a/srcs/JsonValue.h b/srcs/JsonValue.h index d6f9cbe6..2afdba7a 100644 --- a/srcs/JsonValue.h +++ b/srcs/JsonValue.h @@ -14,10 +14,12 @@ public: } void operator=(bool); + void operator=(const char*); void operator=(double); void operator=(int); operator bool(); + operator const char*(); operator double(); operator int(); diff --git a/tests/JsonObjectTests.cpp b/tests/JsonObjectTests.cpp index 533fb2c7..6e690b36 100644 --- a/tests/JsonObjectTests.cpp +++ b/tests/JsonObjectTests.cpp @@ -50,7 +50,6 @@ TEST_F(JsonObjectTests, CanStoreDoubles) EXPECT_EQ(456.78, (double) object["world"]); } - TEST_F(JsonObjectTests, CanStoreBooleans) { object["hello"] = true; @@ -58,4 +57,13 @@ TEST_F(JsonObjectTests, CanStoreBooleans) EXPECT_TRUE((bool) object["hello"]); EXPECT_FALSE((bool) object["world"]); +} + +TEST_F(JsonObjectTests, CanStoreStrings) +{ + object["hello"] = "h3110"; + object["world"] = "w0r1d"; + + EXPECT_STREQ("h3110", (const char*) object["hello"]); + EXPECT_STREQ("w0r1d", (const char*) object["world"]); } \ No newline at end of file