Skip to content

provide "NaN" and "Infinity" when (de)serializing Java Numbers #209

@nimo23

Description

@nimo23

Json-P uses this to (de)serialize Double-values to and from Json:

JsonGenerator write(double value);

Please provide the ability to (de)serialize "NaN", "+Infinity" and "-Infinity" with any type of Numbers (Double, etc) to produce and consume something like this

{
  "val1" : "NaN",
  "val2" : 1.0,
  "val3" : 0.0,
  "val4" : "+Infinity"
  "val5" : "-Infinity"
}

converter to produce json string:

try{
    // normal conversion to double
}
catch(NumberFormatException ex){
    if(Double.isNaN(val)) return "NaN";
    if(Double.isInfinite(val) && val>0) return "Infinity";
    if(Double.isInfinite(val) && val<0) return "-Infinity";
}

converter to consume "NaN" and "Infinity" from Json-String:

try{
    if(val == "NaN") return Double.NaN;
    if(val == "Infinity") return Double.POSITIVE_INFINITY;
    if(val == "-Infinity") return Double.NEGATIVE_INFINITY;
    // normal conversion to double
    ...
}

For example, Json-P could automatically use the Double-(de)serializer, if Double-Objects are used. If Json-P detects primitive double-types, the double-(de)serializer can be used.

Also, Json-P should provide the Json-P configuration property NAN_AS_STRINGS:

// uses strings for "NaN", "+Infinity", "-Infinity"
// instead of throwing NumberFormatException when (de)serializing
Maps.of(JsonGenerator.WRITE_NAN_AS_STRINGS, true)

Actually, Json-P are not able to consume and produce Java Numbers complete: "NaN" and "Infinity" are still valid information about numbers which should not be thrown with exceptions by default.

Related:

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