0

The server side successfully generates JSON object for Nan values as I am using serializeSpecialFloatingPointValues() method. I am getting an error due to Nan on the client side. Is there a option which I can include on the client side to handle Nan similar to the one available on server side in jQuery ?

Server Side

Server side Gson successfully generate JSON object

GsonBuilder gsonBuilder = new GsonBuilder();  
gsonBuilder.serializeSpecialFloatingPointValues();  
Gson gson = gsonBuilder.setPrettyPrinting().create();  
String jsonString = gson.toJson(Object, HashMap.class);  
response.getWriter().write(jsonString);  
response.getWriter().flush();  

Output Generated from Server

{   
      "name": "ABC",  
      "data1": 511,  
      "data2": NaN  
}   

Client Side

 $.ajax({
     url: "AbcServlet",
     data: {
         input: abc,
     },
     dataType: "json",
     type: "GET",
     async: false,
     success: function (parsed_json) {
         evaluate(parsed_json);
     },
     error: function (request, status, error) {
         alert("Error " + error);
     }
 });

Note: Client side works perfectly if Nan in the json response output is replaced by a number.

My question is if com.google.gson.Gson has has the option to handle or generate Nan, there must be some option to handle it on the client side as well.

2sb
  • 629
  • 2
  • 12
  • 26
  • 1
    Please provide a sample of the JSON produced by the server-side statement. I'm curious how it encodes NaN, considering there's no literal syntax for the value AFAIK... – Heretic Monkey Oct 26 '12 at 19:38
  • See http://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript – Barmar Oct 26 '12 at 20:27
  • @MikeMcCaughan The only difference is the value which appears like NaN without quotes unlike string. instead of number or string. Adding similar output in my query. – 2sb Oct 29 '12 at 15:45
  • 1
    The only option to enable support for NaN in JSON is to write your own parser, as @Barmar's answer indicates. See also Issue 81 at the Gson project site ( http://code.google.com/p/google-gson/issues/detail?id=81 ), which indicates the problems with adding support of things which are explicitly disallowed by the standard. – Heretic Monkey Oct 29 '12 at 16:14
  • @MikeMcCaughan thanks but writing a new parser for just Double.Nan doesn't make sense. There must be an option to override jquery parser if it encounters Nan in json response, may be using filter.http://api.jquery.com/extending-ajax/.Still looking for a better solution. – 2sb Oct 29 '12 at 16:44
  • Don't see anything in the jQuery documentation that would help. The converter option is used to say what function should be used to parse the response, but it doesn't actually provide the alternate parser; you still have to write it yourself. – Barmar Oct 29 '12 at 16:59
  • @Barmar I finally found an easier option(workaround). Register typeAdapter for serializing double data type. It is good workaround which will make NaN as string "NaN". http://code.google.com/p/google-gson/issues/detail?id=378. Thanks for the response though. – 2sb Oct 29 '12 at 17:06

0 Answers0