[JAVA-558] Long in $date-values aren't parsed correctly by JSON.parse() Created: 23/Apr/12  Updated: 25/Jun/13  Resolved: 18/May/12

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 2.7.2
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Adam Pordzik Assignee: Bryan Reinero
Resolution: Done Votes: 0
Labels: date, driver, java, parsing
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Attachments: Text File JSONCallback.java.patch    

 Description   

When dumping a database or collection with dates -at least on Mac OS X and from .NET gererated Output- all date-data-object are encodes as timestamps in the form:

 { when : { "$date" : 1234567890000 } } // an unqouted Long value 

when I try to parse such an String with JSON.parse(), I either get an java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.String, if this timestamp is an unquoted Long or silently a null, if it was an quoted Long as in '"1234567890"'. Requesting such object leads e.g. to:

 { "when" : null } 

I'm quite astonished that nobody found nor fixed this issue so far, since it is even an internally used format.

I fixed this in JSONCallback.java by assuming first that the value is a parsable Long which can be turned into a Date-object. In case of an NumberFormatException ISODate is will be tried by applying defined SimpleDateFormat (Which is also no correct ISODate, since incomplete format. But this is another issue.)

See attached patch.



 Comments   
Comment by Bryan Reinero [ 18/May/12 ]

Commit link:

https://github.com/mongodb/mongo-java-driver/commit/fe72f3f0a4523c584a990738520e1f39de439b25

Comment by Bryan Reinero [ 18/May/12 ]

https://github.com/mongodb/mongo-java-driver/commit/fe72f3f0a4523c584a990738520e1f39de439b25

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