|
We no longer plan to fix anything in the JSON utility classes, as it has been superseded by the JsonReader class in the 3.x driver series. I confirmed that JsonReader can generate and parse positive and negative infinity, and NaN. This code:
BasicDBObject doc = new BasicDBObject()
|
.append("pinf", Double.POSITIVE_INFINITY)
|
.append("ninf", Double.NEGATIVE_INFINITY)
|
.append("nan", Double.NaN);
|
|
String json = doc.toJson();
|
System.out.println(json);
|
|
BasicDBObject parsed = BasicDBObject.parse(json);
|
System.out.println( parsed);
|
outputs:
{ "pinf" : Infinity, "ninf" : -Infinity, "nan" : NaN }
|
{ "pinf" : Infinity , "ninf" : -Infinity , "nan" : NaN}
|
Other JSON parsers may reject this this though, but note that in the 3.5 release we're adding a new JsonMode that generates JSON text that any JSON library will be able to parse:
BasicDBObject doc = new BasicDBObject()
|
.append("pinf", Double.POSITIVE_INFINITY)
|
.append("ninf", Double.NEGATIVE_INFINITY)
|
.append("nan", Double.NaN);
|
|
String json = doc.toJson(JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build());
|
System.out.println(json);
|
|
BasicDBObject parsed = BasicDBObject.parse(json);
|
System.out.println( parsed);
|
will output:
{ "pinf" : { "$numberDouble" : "Infinity" }, "ninf" : { "$numberDouble" : "-Infinity" }, "nan" : { "$numberDouble" : "NaN" } }
|
{ "pinf" : Infinity , "ninf" : -Infinity , "nan" : NaN}
|
|