-
Notifications
You must be signed in to change notification settings - Fork 65
provide "NaN" and "Infinity" when (de)serializing Java Numbers #209
Description
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: