Put in a strategy to determine how NaN/Infinity is handled #69
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently encoding from a double or float with value NaN or +/- Inf results in invalid JSON - any doubles are simply encoded as String(value) which leads to e.g.
Worse, attempting to reparse the output from SimpleJSON throws a serializationException;
Clearly, this does not appear to be a problem for too many people; otherwise it would have been altered already, and I've assumed that making these automatically into strings would be unacceptable - certainly it would risk breaking existing behaviour.
This pull request implements an additional feature to the
SerializerStrategy
, that gives the user the option to define how these values are handled, the functionobject CoerceInfiniteOrNaN(object value)
. This allows the value being serialized to be coerced to a different (valid JSON) form - as an example, an extra strategyInfinityAsStringJsonSerializerStrategy
is included which simply ensures the value is encoded as a JSON-string in the NaN/Infinity cases. I can imagine depending on the target application, the user might want to e.g. cap to a large value, change to zero, or some other behavior.