Closed
Description
Description
When using rfl::yaml::write to serialize a double, if the double's value is an integer, it gets serialized as an integer instead of a floating-point number.
auto obj = rfl::Generic::Object();
double age = 10;
obj["age"] = age;
auto s = rfl::yaml::write(obj);
std::cout << s << std::endl; // age: 10 (expect: age: 10.0)
This leads to an error during deserialization:
s = "age: 10";
obj = rfl::yaml::read<rfl::Generic::Object>(s).value();
age = rfl::from_generic<double>(obj.get("age").value()).value();
// error is:
// terminate called after throwing an instance of 'std::runtime_error'
// what(): rfl::Generic: Could not cast the underlying value to a double.
// zsh: IOT instruction (core dumped) ./build/xxx
JSON serialization does not exhibit this problem; it correctly serializes the value as {"age":10.0}.
Expected Behavior
The double value should be serialized as a floating-point number (e.g., "age: 10.0") in YAML, allowing for successful deserialization and extraction as a double.
Thank you for your efforts!
Metadata
Metadata
Assignees
Labels
No labels