Summary
The method org.bson.Document#parse(java.lang.String) fails for a valid JSON string when there is a null value for a $date field in the JSON string. Example below in How to Reproduce section.
Driver version: 4.8.2, 5.5.1
Server version: Not applicable
How to Reproduce
JSON String:
{
"name" : "abc",
"dateField" : {
"$date" : null
}
{color:#cccccc}}
This fails with
Exception in thread "main" org.bson.json.JsonParseException: JSON reader expected an integer or string but found 'null'.
at org.bson.json.JsonReader.visitDateTimeExtendedJson(JsonReader.java:1052)
at org.bson.json.JsonReader.visitExtendedJSON(JsonReader.java:626)
at org.bson.json.JsonReader.readBsonType(JsonReader.java:166)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:174)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:44)
at org.bson.Document.parse(Document.java:129)
at org.bson.Document.parse(Document.java:114)
The same JSON string when inserted via MongoDB Compass, is able to automatically detect that a null value is provided for the $date field and considers that as a JSON object and inserts it.
MongoDB shell works too:
db.test.insertOne({ dateField: {"$date" : null)}}
Ideally, the Document.parse method should be able to dynamically handle this as Compass and Mongo shell does.