libmir / asdf

JSON library
http://asdf.libmir.org
Boost Software License 1.0
20 stars 8 forks source link

Double values that looks like integers in JSON #22

Closed cyrusmsk closed 1 year ago

cyrusmsk commented 1 year ago

In case the JSON file has values like [-75, 1.234] - how can I present it with my serialization function as [-75.0, 1.234]?

9il commented 1 year ago

I am not sure I understood the question. Please elaborate.

cyrusmsk commented 1 year ago

With help from community I found how to solve the issue. The example could be found in this paste https://paste.myst.rs/qd8vsk37 (different results for Python and D here). So if we declaring, that the data usually has double type, when it looks like integer (without point) some other languages use integer interpretation in deserialization. So to fix that I've changed type of the data to Variant!(int, double) and provided my own deserializer for this type (similar to asdf example with mir.algebraic).

The interesting point: it seems that many other languages use this behavior by default. The original problem is here: https://programming-language-benchmarks.vercel.app/problem/json-serde You can find that other languages do not specify the specific integer/double processing (like Go, Zig, C#). Rust uses this specific serialization rule (i64/f64) too.