hjson-java icon indicating copy to clipboard operation
hjson-java copied to clipboard

JsonValue.valueOf( float / double ) should check NaN and ±Infinite values

Open biziclop opened this issue 5 years ago • 1 comments

Otherwise
JsonValue.valueOf( Math.log(-1)).toString();
will throw up a NumberFormatException like a drunken sailor, because inside JsonNumber.toString() the called function BigDecimal.valueOf() doesn't really like non-finite values.

biziclop avatar Mar 07 '19 17:03 biziclop

Adding this,

if (Double.isNaN(value) || Double.isInfinite(value)) {
      return Double.toString(value);
}

under line 41, would help prevent throwing a NumberFormatException, as it skips BigDecimal.valueOf(), and just returns its own value.

EhWhoAmI avatar Sep 30 '20 06:09 EhWhoAmI

Sorry for the very late response.

I'll have a look at improving documentation instead of changing this behavior. Reasons:

  1. Existing apps might depend on hjson-java throwing an error instead of outputting Inf or NaN.
  2. The Hjson specification does not consider Inf or NaN to be valid representations for numbers (https://hjson.github.io/syntax.html)
  3. The JSON specification does not consider Inf or NaN to be valid representations for numbers, so JsonWriter should still throw an error for them.

trobro avatar Aug 12 '23 14:08 trobro