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.
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:
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.