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;
}
if (childValue->type >= JSON_DOUBLE_0_DECIMALS)
n += p.print(childValue->content.asDouble, childValue->type - JSON_DOUBLE_0_DECIMALS);
if (child->next)
{
n += p.write(',');

View File

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

View File

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

View File

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