Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-2233

BsonDocument.Parse fails to recognize ISO 8601 dates

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 2.5
    • Component/s: Json
    • Labels:

      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
      Observed: the BsonDocument treats the date as a string

      Here's a quick repro that illustrates what I'm seeing:

      public void SerializeDates()
      {
          var now = DateTime.Parse("2018-04-01T23:43:24.1234567-04:00", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);
          var obj = new {now};
          var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj);
          var bsonFromJson = BsonDocument.Parse(json);
          var bson = obj.ToBsonDocument();
          Console.WriteLine("JSON          : {0}", json);          // prints {"now":"2018-04-02T03:43:24.1234567Z"}
          Console.WriteLine("BSON from JSON: {0}", bsonFromJson);  // prints { "now" : "2018-04-02T03:43:24.1234567Z" }
          Console.WriteLine("BSON          : {0}", bson);          // prints { "now" : ISODate("2018-04-02T03:43:24.123Z") }
      }
      

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            hughbiquitous Hugh Williams
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: