jakartaee / jakartaee/jsonp-api

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

Open
#209 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
160
Forks
64
Avg merge
4d 5h
Merged PRs (30d)
4

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:

- https://github.com/eclipse-ee4j/jsonp/issues/160
- https://github.com/eclipse-ee4j/yasson/issues/306

Contributor guide

Open the contributing guide

Research direction

Start with the JsonGenerator write(double value) API and the requested NAN_AS_STRINGS configuration property. Review the related jsonp-api issue 160 and Yasson issue 306, then establish consistent production and consumption of NaN and signed Infinity values for Java number types; done means the API behavior and configuration are specified and covered for the requested cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.