Serialize floats in objects

This commit is contained in:
Benoit Blanchon
2014-09-30 17:32:45 +02:00
parent c1ab55f9d9
commit f251563af1
4 changed files with 15 additions and 6 deletions

View File

@ -124,6 +124,9 @@ size_t JsonObject::printTo(Print& p) const
break; break;
} }
if (childValue->type >= JSON_DOUBLE_0_DECIMALS)
n += p.print(childValue->content.asDouble, childValue->type - JSON_DOUBLE_0_DECIMALS);
if (child->next) if (child->next)
{ {
n += p.write(','); n += p.write(',');

View File

@ -19,10 +19,15 @@ void JsonValue::operator=(char const* value)
} }
void JsonValue::operator=(double value) void JsonValue::operator=(double value)
{
set(value, 2);
}
void JsonValue::set(double value, int decimals)
{ {
if (!_node) return; if (!_node) return;
_node->type = JSON_DOUBLE_2_DECIMALS; _node->type = (JsonNodeType) (JSON_DOUBLE_0_DECIMALS + decimals);
_node->content.asDouble = value; _node->content.asDouble = value;
} }

View File

@ -29,10 +29,11 @@ public:
operator int() const; operator int() const;
operator JsonObject() const; operator JsonObject() const;
void set(double value, int decimals);
private: private:
JsonNode* _node; JsonNode* _node;
void setAsProxyTo(JsonNode*); void setAsProxyTo(JsonNode*);
JsonNode* getActualNode() const; JsonNode* getActualNode() const;
}; };

View File

@ -95,10 +95,10 @@ TEST_F(JsonObjectSerializationTests, OneInteger)
object["key"] = 1; object["key"] = 1;
outputMustBe("{\"key\":1}"); outputMustBe("{\"key\":1}");
} }
/*
TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits) TEST_F(JsonObjectSerializationTests, OneDoubleFourDigits)
{ {
object["key"].set<4>(3.14159265358979323846); object["key"].set(3.14159265358979323846, 4);
outputMustBe("{\"key\":3.1416}"); outputMustBe("{\"key\":3.1416}");
} }
@ -107,7 +107,7 @@ TEST_F(JsonObjectSerializationTests, OneDoubleDefaultDigits)
object["key"] = 3.14159265358979323846; object["key"] = 3.14159265358979323846;
outputMustBe("{\"key\":3.14}"); outputMustBe("{\"key\":3.14}");
} }
/*
TEST_F(JsonObjectSerializationTests, OneNull) TEST_F(JsonObjectSerializationTests, OneNull)
{ {
object["key"] = (char*) 0; object["key"] = (char*) 0;