Skip to content

Decimal values are serialized as floats #1040

@matthiasbeyer

Description

@matthiasbeyer

#[cfg(feature = "decimal")]
#[cfg(not(feature = "f32_float"))]
Union::Decimal(ref x, ..) => {
use rust_decimal::prelude::ToPrimitive;
match x.to_f64() {
Some(v) => ser.serialize_f64(v),
None => ser.serialize_str(&x.to_string()),
}
}
#[cfg(feature = "decimal")]
#[cfg(feature = "f32_float")]
Union::Decimal(ref x, ..) => {
use rust_decimal::prelude::ToPrimitive;
match x.to_f32() {
Some(v) => ser.serialize_f32(v),
_ => ser.serialize_str(&x.to_string()),
}
}

This converts a Decimal to a float, which is an issue for roundtripping serialized to deserialized and back.

The issue where this appeared for me is:

I have an ID which gets correctly parsed from a blob of data (with deserializing a map of Dynamic) to a Decimal, which is enabled as a feature. But when sending a serialized blob again, where that Decimal is a part of, the Serialize implementation of Dynamic causes it to be serialized as f64.

You mentioned in #587 (comment) that one can consider such numbers as i64 as well, but that does not work with serialization and deserialization, if one of that converts to float.

Do you know of a way how I can make it to serialize as a integer number? Is there currently a way with rhai that I am missing?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions