mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-17 12:32:17 +02:00
Serialize floats in objects
This commit is contained in:
@ -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(',');
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
Reference in New Issue
Block a user