[CSHARP-2233] BsonDocument.Parse fails to recognize ISO 8601 dates Created: 02/Apr/18 Updated: 27/Oct/23 Resolved: 02/Apr/18 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | Json |
| Affects Version/s: | 2.5 |
| Fix Version/s: | None |
| Type: | Task | Priority: | Minor - P4 |
| Reporter: | Hugh Williams | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
I need to take a JSON string that came out of JSON.NET and read it into a BsonDocument. Expected: the BsonDocument will contain an ISODate Here's a quick repro that illustrates what I'm seeing:
|
| Comments |
| Comment by Hugh Williams [ 02/Apr/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thanks, that makes sense. If anyone else finds it useful, the workaround code I'm going forward with is shared at https://gist.github.com/hughbiquitous/d1098da73767a6bbd5889bbb8c3dd1a8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Robert Stam [ 02/Apr/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Thank you for contacting us about this. The underlying issue is that JSON does not define a representation for DateTimes. Json.NET and MongoDB have solved this limitation in different ways. As you know, Json.NET decided to represent DateTimes using strings. But that makes reading strings ambiguous (is it really a string or is it really a DateTime?). MongoDB decided to represent DateTimes (and other BSON data types that JSON does not support) using what is called "Extended JSON". The C# driver also uses an easier to read representation called "Shell" representation that can be copy/pasted into the MongoDB shell. The result is that a DateTime is represented as either:
BsonDocument.Parse will read either of those correctly as a DateTime. But it will never convert a string to a DateTime. You will either have to preprocess the input JSON strings to use the Extended JSON format used by MongoDB, or postprocess the resulting BsonDocument after parsing it (as you have also suggested in the previous comment). Preprocessing the strings is hard, post processing is relatively easy so that's the best solution. I don't think we can change the behavior of the C# driver to look at every string it reads and see if it "looks" like a DateTime. That would be a backward breaking change and would be incompatible with what all other drivers and the MongoDB shell do. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Comment by Hugh Williams [ 02/Apr/18 ] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Workaround illustrated in an Xunit test (would still like to see BsonDocument.Parse do this for me though):
|