This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

GSON and NaN

 
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use GSON to serialize a MAP that is keyed on an object. For the most part, it is working, but I get the error:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: JSON forbids NaN and infinities: NaN

I believe that GSON should accept NaN. Here is an old coderanch question where the suggestion was to use gson:

https://coderanch.com/t/528867/java/JSON-Parsing-NaN-Infinity

Here is my code. First the attempt to convert the map to a GSON string and back and then a simplified example of the class that contains the key. I thought that "serializeSpecialFloatingPointValues()" would allow using NaN, but it is not working. If I change the DataEntry class and removed the Double.NaN assignment, I get a GSON string, but, of course, the class no longer works correctly in the application.

Any thoughts on what I am missing? I am using gson 2.8.5.


 
Marshal
Posts: 3727
534
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wrote a simple application to try it myself - seems like registering the serializeSpecialFloatingPointValues() method does support NaN and +/- Infinity - both serializing and deserializing.
 
Ron McLeod
Marshal
Posts: 3727
534
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like you have run in to the Serialization of NaN/Inf with complex map key serialization and special floating point values enabled is broken issue - which appears to still be unresolved.

You can work-around this by defining a TypeAdapter for your key class to serialize and deserialize the key - for example:
Change the JSON representation to whatever is needed if the key needs to be compatible with other applications.

The adapter needs to be registered with your Gson instances - for example:
 
Jon Swanson
Ranch Hand
Posts: 253
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's great, thank you. The TypeAdapter seems to resolve my problem nicely. The actual class (optionally) takes an instance of another class as an argument, i.e. DataEntry( Point p, double v) but that other class is quite simple (just created to implement Comparable) so it should just be a matter of writing the read/write methods for DataEntry to handle that situation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic