[CSHARP-599] Add option to JsonWriter which allows to output the datetimes in ISO-8601 format Created: 11/Oct/12  Updated: 20/May/19  Resolved: 20/May/19

Status: Closed
Project: C# Driver
Component/s: Feature Request
Affects Version/s: 1.6
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Mikael Koskinen Assignee: Unassigned
Resolution: Duplicate Votes: 1
Labels: driver, json
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates CSHARP-1918 Implement Extended JSON specification Closed
Related
is related to CSHARP-1858 Add user configurable output converte... Closed

 Description   

Currently the JsonOutputMode-enum offers few different options which allows one to configure how the DateTimes are formatted with JsonWriter. To provide a better compatibility with other frameworks, it would be great if the JsonWriter could be configured to output the dates in ISO-8601 format. This format is currently the default format used by the ASP.NET Web Api: http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx

I forked the drivers and added this option in here: https://github.com/mikoskinen/mongo-csharp-driver/commit/a04fe07dd13ac8990b516e4dcdf9d9edd8fb502a

I added a new boolean UseISO8601DateFormat to JsonWriter which allows you to change the formatting. Is this a good place for it or should there be a new enum option in the JsonOutputMode? Also, I've enable the new formatting only when the flag UseISO8601DateFormat is set to true AND the JsonOutputMode is Shell. I'm not that familiar with the difference of these modes so I'm not sure if this option should be enable for others too.



 Comments   
Comment by Ian Whalen (Inactive) [ 20/May/19 ]

Given that we don't plan to implement this specifically but will solve this via CSHARP-1918, we're closing this as a duplicate and would like to redirect any watchers here to follow CSHARP-1918 and CSHARP-1858.

Comment by Robert Stam [ 02/Dec/16 ]

This issue will be resolved as part of CSHARP-1858.

Comment by Dwayne Bull [ 29/Nov/16 ]

Me checking in for the yearly update
I've been watching this for almost 3 years now.
Any progress?

Comment by Robert Stam [ 17/Aug/15 ]

Hi Dwayne, no updates yet.

This is a special case of the more general problem of wanting to exert control over the shape of the generated JSON, and in particular about what to do with BSON values (like DateTime) that don't have corresponding JSON equivalents.

We've made several experimental attempts to provide more control over the generated JSON, mostly in the context of CSHARP-999, but all attempts have floundered when it came to designing exactly how one would configure a JsonWriter to control the generated output. There is an inherent conflict between wanting to keep the API simple (fewer knobs) and allowing more configuration options (more knobs). So far we haven't come up with the right API with the right balance.

Comment by Dwayne Bull [ 15/Aug/15 ]

Me again, just wondering if there are any updates here.

Comment by Dwayne Bull [ 07/Mar/14 ]

Has there been any progress on this issue?

Comment by jonathan roeder [ 30/Nov/13 ]

Is there a solution to outputting dates as ISO-8601 when calling the ToJson extension method on a BsonDocument, preferably without having to know any particular element name? Am I supposed to add a Convention to BsonDocument?

Comment by Mikael Koskinen [ 18/Oct/12 ]

Thanks Robert for the comment and for the alternative solution.

It's true that this probably prevents the round-tripping from working. I'll try to sort that out.

Comment by Robert Stam [ 12/Oct/12 ]

Looks like this change would prevent DateTime values from round-tripping.

Did you try to read the JSON document back to see if you could successfully get back the document you started with?

An existing alternative is to tell the serializer to use a string representation for a particular DateTime, and it will be output in ISO-8601 format. If you do it this way, it will round-trip.

public class Post
{
    // other properties
 
    [BsonRepresentation(BsonType.String)]
    public DateTime PublishedAt { get; set; }
}

Comment by Mikael Koskinen [ 11/Oct/12 ]

And here's a bug fix without so many bugs in few lines of code

https://github.com/mikoskinen/mongo-csharp-driver/commit/309bac1fc9f8c624af83d204e477b200be7231ef

Comment by Mikael Koskinen [ 11/Oct/12 ]

"I added a new boolean UseISO8601DateFormat to JsonWriter" should have been "I added a new boolean UseISO8601DateFormat to JsonWriterSettings".

Generated at Wed Feb 07 21:37:17 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.