Skip to content

Serialization round-trip fails for floating-point numbers if they're serialized to null #19

@vpanteleev-sym

Description

@vpanteleev-sym

For example, the default value of double (which is a NaN) is serialized to null, which throws an error when attempting to deserialize:

/+dub.sdl:
 dependency "vibe-serialization" version="*"
+/
import std.stdio;
import vibe.data.json;

void main() {
	double d;
	d.serializeToJsonString                       .writeln; // prints "null"
	d.serializeToJsonString.deserializeJson!double.writeln; // throws
}

I think serialization libraries should try to ensure that a serialization and deserialization round-trip does not have such surprises.

We could do one of:

  • Throw on serialization if the value is not deserializable;
  • Serialize as a string (e.g. "NaN") and recognize it when deserializing
  • If it's just NaN, then understand null as NaN (but not sure about infinities).

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