[JAVA-1841] JSON serializer will write out Inifinity but JSON.parse fails to parse it Created: 26/May/15  Updated: 09/Mar/17  Resolved: 09/Mar/17

Status: Closed
Project: Java Driver
Component/s: JSON
Affects Version/s: 2.12.0, 3.0.0
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: Matt Arpidone Assignee: Unassigned
Resolution: Won't Fix Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Infinity is not part of the JSON spec, but neither is NaN, and the Mongo driver's parser seems to handle that case so should it not also handle Infinity?

This program demonstrates the problem.

TestMongoInfinity.java

public class TestMongoInfinity {
	public static void test(BasicDBObject obj) {
		String serializedObj = null;
		try {
			serializedObj = JSONSerializers.getStrict().serialize(obj);
		} catch (Throwable t) {
			System.out.println("Error during serialization");
			t.printStackTrace();
		}
		BasicDBObject deserialized = null;
		try {
			deserialized = (BasicDBObject)JSON.parse(serializedObj);
		} catch (Throwable t) {
			System.out.println("Error during deserialization");
			t.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		test(new BasicDBObject("val", Double.POSITIVE_INFINITY));
		test(new BasicDBObject("val", Double.NEGATIVE_INFINITY));
	}
}



 Comments   
Comment by Jeffrey Yemin [ 09/Mar/17 ]

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}

Generated at Thu Feb 08 08:55:38 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.