From c1ab55f9d9069850ab8783c59265007fcd6bd466 Mon Sep 17 00:00:00 2001 From: Benoit Blanchon Date: Tue, 30 Sep 2014 17:24:14 +0200 Subject: [PATCH] Serialize integer values in object --- srcs/JsonNode.h | 2 +- srcs/JsonObject.cpp | 14 ++++++++++++-- tests/JsonObjectSerializationTests.cpp | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/srcs/JsonNode.h b/srcs/JsonNode.h index 9ea81e97..263a2e4b 100644 --- a/srcs/JsonNode.h +++ b/srcs/JsonNode.h @@ -28,7 +28,7 @@ struct JsonNode { bool asBoolean; double asDouble; - int asInteger; + long asInteger; const char* asString; struct diff --git a/srcs/JsonObject.cpp b/srcs/JsonObject.cpp index 045d8866..23b74073 100644 --- a/srcs/JsonObject.cpp +++ b/srcs/JsonObject.cpp @@ -108,11 +108,21 @@ size_t JsonObject::printTo(Print& p) const for (JsonNode* child = firstChild; child; child = child->next) { const char* childKey = child->content.asKey.key; - const char* childValue = child->content.asKey.value->content.asString; + JsonNode* childValue = child->content.asKey.value; n += EscapedString::printTo(childKey, p); n += p.write(':'); - n += EscapedString::printTo(childValue, p); + + switch (childValue->type) + { + case JSON_STRING: + n += EscapedString::printTo(childValue->content.asString, p); + break; + + case JSON_INTEGER: + n += p.print(childValue->content.asInteger); + break; + } if (child->next) { diff --git a/tests/JsonObjectSerializationTests.cpp b/tests/JsonObjectSerializationTests.cpp index 02e77103..3e526905 100644 --- a/tests/JsonObjectSerializationTests.cpp +++ b/tests/JsonObjectSerializationTests.cpp @@ -89,13 +89,13 @@ TEST_F(JsonObjectSerializationTests, OneStringOverCapacity) outputMustBe("{\"key1\":\"value1\",\"key2\":\"value2\"}"); } -/* + TEST_F(JsonObjectSerializationTests, OneInteger) { object["key"] = 1; outputMustBe("{\"key\":1}"); } - +/* TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits) { object["key"].set<4>(3.14159265358979323846);