bytesize icon indicating copy to clipboard operation
bytesize copied to clipboard

Human-readable serde impl loses precision, serializes bytes incorrectly

Open sunshowers opened this issue 2 years ago • 0 comments

Hi there, thanks for the crate!

While developing against the crate I found that human-readable serde impl's serializer is incorrect. For example, the following program:

use bytesize::ByteSize;

fn main() {
    let json = serde_json::to_string(&ByteSize::mib(1)).unwrap();
    println!("serialized JSON: {json}");

    let deserialized: ByteSize = serde_json::from_str(&json).unwrap();
    println!("deserialized: {}", deserialized.0);
}

prints out:

serialized JSON: "1048.6 KB"
deserialized: 1048600

Which means that serialization/deserialization does not roundtrip.

I think serialization should either be to an integer, or to exact values (e.g. "1048.576 KB"). What do you think?

sunshowers avatar Jan 07 '24 03:01 sunshowers