[JAVA-3316] Unable to Parse Date String in Json String using Document.parse() Created: 11/Jun/19  Updated: 30/Mar/22

Status: Backlog
Project: Java Driver
Component/s: JSON
Affects Version/s: None
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: shen Chen Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Windows 10



 Description   

Code:

static String dateStr = "{ \"name\" : \"Bruce Lee\", \"age\" : 100, \"LossDate\":{\"$date\":'1970-01-01'}}";

Document doc = Document.parse(dateStr);

 

 

org.bson.json.JsonParseException: Failed to parse string as a date
at org.bson.json.JsonReader.visitDateTimeExtendedJson(JsonReader.java:1057)
at org.bson.json.JsonReader.visitExtendedJSON(JsonReader.java:618)
at org.bson.json.JsonReader.readBsonType(JsonReader.java:148)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:149)
at org.bson.codecs.DocumentCodec.decode(DocumentCodec.java:45)



 Comments   
Comment by Jeffrey Yemin [ 11/Oct/19 ]

If we did something I would want to do it as a specification change so that all drivers accepted the same formats.

Comment by shen Chen [ 25/Jun/19 ]

HI Ross,

Thank you for the info. Looks like I either need to create custom JsonReader or pass in the date string in the accepted Java Date Format Pattern String: yyyy-MM-dd'T'HH:mm:ss.SSSXXX or yyyy-MM-dd'T'HH:mm:ss.SSS'Z' or just simply a long number.

Comment by Ross Lawley [ 25/Jun/19 ]

Hi shen915,

Currently, there is no easy way to add custom parsers to the JsonReader.

Ross

Comment by shen Chen [ 24/Jun/19 ]

Hi Ross,

Is there a way allow me to inject my own date parser into the Document.parse method? Thank you for the response.

Comment by Ross Lawley [ 24/Jun/19 ]

Hi shen915,

The driver json specification only supports iso date times and not iso dates. This is because there is no corresponding BSON type that represents just a date.

I hope that helps clarify the reasons why the example fails.

Ross

Comment by shen Chen [ 11/Jun/19 ]

MongoDB Java Driver 3.4.1. Also tested using newer version of MongoDB Java Driver.

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