public object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { EvaluationResult er = new EvaluationResult(); bsonReader.ReadStartDocument(); er.Id = bsonReader.ReadObjectId(); er.DurationInSeconds = bsonReader.ReadDouble(); er.startTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); er.endTime = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); er.Result = EvaluationStatus.Parse(bsonReader.ReadString()); er.JSON = bsonReader.ReadString(); bsonReader.ReadEndDocument(); return(er); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTimeOffset)); var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTimeOffset value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = BsonDateTime.Create(bsonReader.ReadDateTime()).Value; break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTimeUTC"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeSerializationOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new FormatException(message); } return(value); }
public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTimeOffset)); BsonType bsonType = bsonReader.GetCurrentBsonType(); long ticks; TimeSpan offset; switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); ticks = bsonReader.ReadInt64(); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32()); bsonReader.ReadEndArray(); return(new DateTimeOffset(ticks, offset).Add(offset)); case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); ticks = bsonReader.ReadInt64("Ticks"); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset")); bsonReader.ReadEndDocument(); return(new DateTimeOffset(ticks, offset).Add(offset)); default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new ArgumentException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTimeOffset)); BsonType bsonType = bsonReader.GetCurrentBsonType(); long ticks; TimeSpan offset; switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); ticks = bsonReader.ReadInt64(); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32()); bsonReader.ReadEndArray(); return new DateTimeOffset(ticks, offset); case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); // ignore value ticks = bsonReader.ReadInt64("Ticks"); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset")); bsonReader.ReadEndDocument(); return new DateTimeOffset(ticks, offset); case BsonType.String: return XmlConvert.ToDateTimeOffset(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTimeOffset)); var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTimeOffset value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = (new BsonDateTime(bsonReader.ReadDateTime())).ToUniversalTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTimeUTC"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeSerializationOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new FormatException(message); } return value; }
public void TestDateTimeStrict() { var json = "{ \"$date\" : 0 }"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(BsonConstants.UnixEpoch, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <DateTime>(new StringReader(json)).ToJson()); }
public void TestDateTimeMaxBson() { var json = "new Date(9223372036854775807)"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(9223372036854775807, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonDateTime>(new StringReader(json)).ToJson()); }
public void TestDateTimeMinBson() { var json = "new Date(-9223372036854775808)"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(-9223372036854775808, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonDateTime>(json).ToJson()); }
/// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns> /// An object. /// </returns> public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(ZonedDateTime)); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.DateTime) { var millisecondsSinceEpoch = bsonReader.ReadDateTime(); return(new Instant(millisecondsSinceEpoch).InUtc()); } throw new InvalidOperationException(string.Format("Cannot deserialize ZonedDateTime from BsonType {0}.", bsonType)); }
public override DateTimeOffset Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) { BsonReader bsonReader = (BsonReader)context.Reader; var bsonType = bsonReader.GetCurrentBsonType(); DateTimeOffset value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = (new BsonDateTime(bsonReader.ReadDateTime())).ToUniversalTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTimeUTC"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc); break; case BsonType.String: var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); break; default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new FormatException(message); } return(value); }
public void TestDateTimeShell() { var json = "ISODate(\"1970-01-01T00:00:00Z\")"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(0, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; Assert.AreEqual(json, BsonSerializer.Deserialize <DateTime>(new StringReader(json)).ToJson(jsonSettings)); }
public void TestDateTimeTengen() { var json = "new Date(0)"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(0, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.TenGen }; Assert.AreEqual(json, BsonSerializer.Deserialize <DateTime>(new StringReader(json)).ToJson(jsonSettings)); }
public void TestDateTimeStrict() { var json = "{ \"$date\" : 0 }"; using (_bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(0, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.AreEqual(json, BsonSerializer.Deserialize <DateTime>(new StringReader(json)).ToJson(jsonSettings)); }
public void TestDateTimeStrictIso8601() { var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(0, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.AreEqual(expected, BsonSerializer.Deserialize <DateTime>(json).ToJson(jsonSettings)); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BsonDateTime)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.DateTime: var millisecondsSinceEpoch = bsonReader.ReadDateTime(); return new BsonDateTime(millisecondsSinceEpoch); default: var message = string.Format("Cannot deserialize BsonDateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BsonDateTime)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.DateTime: var millisecondsSinceEpoch = bsonReader.ReadDateTime(); return(new BsonDateTime(millisecondsSinceEpoch)); default: var message = string.Format("Cannot deserialize BsonDateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public void TestDateTime() { var json = "{ \"$date\" : 0 }"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(BsonConstants.UnixEpoch, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReadState.Done, bsonReader.ReadState); } Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(new StringReader(json)).ToJson()); }
public void TestDateTimeStrictIso8601() { var json = "{ \"$date\" : \"1970-01-01T00:00:00Z\" }"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(0, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } var expected = "{ \"$date\" : 0 }"; // it's still not ISO8601 on the way out var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.AreEqual(expected, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
public void TestDateTimeStrict() { var json = "{ \"$date\" : 0 }"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(0, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(json).ToJson(jsonSettings)); }
public void TestDateTimeMinBson() { var json = "new Date(-9223372036854775808)"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.DateTime, _bsonReader.ReadBsonType()); Assert.AreEqual(-9223372036854775808, _bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDateTime>(json).ToJson()); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(SystemProfileInfo)); if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null) { bsonReader.ReadNull(); return(null); } else { var profileInfo = new SystemProfileInfo(); bsonReader.ReadStartDocument(); BsonType bsonType; while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "abbreviated": profileInfo.Abbreviated = bsonReader.ReadString(); break; case "client": profileInfo.Client = bsonReader.ReadString(); break; case "command": profileInfo.Command = BsonDocument.ReadFrom(bsonReader); break; case "cursorid": profileInfo.CursorId = BsonValue.ReadFrom(bsonReader).ToInt64(); break; case "err": profileInfo.Error = bsonReader.ReadString(); break; case "exception": profileInfo.Exception = bsonReader.ReadString(); break; case "exceptionCode": profileInfo.ExceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "exhaust": profileInfo.Exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmod": profileInfo.FastMod = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmodinsert": profileInfo.FastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "idhack": profileInfo.IdHack = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "info": profileInfo.Info = bsonReader.ReadString(); break; case "keyUpdates": profileInfo.KeyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "millis": profileInfo.Duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble()); break; case "moved": profileInfo.Moved = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "nreturned": profileInfo.NumberReturned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ns": profileInfo.Namespace = bsonReader.ReadString(); break; case "nscanned": profileInfo.NumberScanned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoreturn": profileInfo.NumberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoskip": profileInfo.NumberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "op": profileInfo.Op = bsonReader.ReadString(); break; case "query": profileInfo.Query = BsonDocument.ReadFrom(bsonReader); break; case "responseLength": profileInfo.ResponseLength = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "scanAndOrder": profileInfo.ScanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "ts": profileInfo.Timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); break; case "updateobj": profileInfo.UpdateObject = BsonDocument.ReadFrom(bsonReader); break; case "upsert": profileInfo.Upsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "user": profileInfo.User = bsonReader.ReadString(); break; default: break; // ignore unknown elements } } bsonReader.ReadEndDocument(); return(profileInfo); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTime)); var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTime value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = new BsonDateTime(bsonReader.ReadDateTime()).ToUniversalTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeSerializationOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType); throw new Exception(message); } if (dateTimeSerializationOptions.DateOnly) { if (value.TimeOfDay != TimeSpan.Zero) { throw new Exception("TimeOfDay component for DateOnly DateTime value is not zero."); } value = DateTime.SpecifyKind(value, dateTimeSerializationOptions.Kind); // not ToLocalTime or ToUniversalTime! } else { switch (dateTimeSerializationOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: value = DateTime.SpecifyKind(BsonUtils.ToLocalTime(value), dateTimeSerializationOptions.Kind); break; case DateTimeKind.Utc: value = BsonUtils.ToUniversalTime(value); break; } } return(value); }
public void TestDateTimeMaxBson() { var json = "new Date(9223372036854775807)"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(9223372036854775807, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDateTime>(new StringReader(json)).ToJson()); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(SystemProfileInfo)); if (bsonReader.GetCurrentBsonType() == Bson.BsonType.Null) { bsonReader.ReadNull(); return null; } else { var profileInfo = new SystemProfileInfo(); bsonReader.ReadStartDocument(); BsonType bsonType; while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "abbreviated": profileInfo.Abbreviated = bsonReader.ReadString(); break; case "client": profileInfo.Client = bsonReader.ReadString(); break; case "command": profileInfo.Command = BsonDocument.ReadFrom(bsonReader); break; case "cursorid": profileInfo.CursorId = BsonValue.ReadFrom(bsonReader).ToInt64(); break; case "err": profileInfo.Error = bsonReader.ReadString(); break; case "exception": profileInfo.Exception = bsonReader.ReadString(); break; case "exceptionCode": profileInfo.ExceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "exhaust": profileInfo.Exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmod": profileInfo.FastMod = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmodinsert": profileInfo.FastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "idhack": profileInfo.IdHack = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "info": profileInfo.Info = bsonReader.ReadString(); break; case "keyUpdates": profileInfo.KeyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "millis": profileInfo.Duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble()); break; case "moved": profileInfo.Moved = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "nreturned": profileInfo.NumberReturned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ns": profileInfo.Namespace = bsonReader.ReadString(); break; case "nscanned": profileInfo.NumberScanned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoreturn": profileInfo.NumberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoskip": profileInfo.NumberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "op": profileInfo.Op = bsonReader.ReadString(); break; case "query": profileInfo.Query = BsonDocument.ReadFrom(bsonReader); break; case "responseLength": profileInfo.ResponseLength = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "scanAndOrder": profileInfo.ScanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "ts": profileInfo.Timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); break; case "updateobj": profileInfo.UpdateObject = BsonDocument.ReadFrom(bsonReader); break; case "upsert": profileInfo.Upsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "user": profileInfo.User = bsonReader.ReadString(); break; default: bsonReader.SkipValue(); // ignore unknown elements break; } } bsonReader.ReadEndDocument(); return profileInfo; } }
//_120509_173140 keep consistent static object ReadObject(BsonReader bsonReader) { switch (bsonReader.GetCurrentBsonType()) { case BsonType.Array: return ReadArray(bsonReader); // replacement case BsonType.Binary: var binary = BsonSerializer.Deserialize<BsonValue>(bsonReader); return BsonTypeMapper.MapToDotNetValue(binary) ?? binary; // byte[] or Guid else self case BsonType.Boolean: return bsonReader.ReadBoolean(); case BsonType.DateTime: return BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); case BsonType.Document: return ReadCustomObject(bsonReader); // replacement case BsonType.Double: return bsonReader.ReadDouble(); case BsonType.Int32: return bsonReader.ReadInt32(); case BsonType.Int64: return bsonReader.ReadInt64(); case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.ObjectId: return bsonReader.ReadObjectId(); case BsonType.String: return bsonReader.ReadString(); default: return BsonSerializer.Deserialize<BsonValue>(bsonReader); } }
public void TestDateTimeTengen() { var json = "new Date(0)"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(0, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.TenGen }; Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(new StringReader(json)).ToJson(jsonSettings)); }
/// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions) options; long? millisecondsSinceEpoch = null; long? ticks = null; switch (bsonType) { case BsonType.DateTime: millisecondsSinceEpoch = bsonReader.ReadDateTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime"); bsonReader.ReadName("Ticks"); var ticksValue = BsonValue.ReadFrom(bsonReader); if (!ticksValue.IsBsonUndefined) { ticks = ticksValue.ToInt64(); } bsonReader.ReadEndDocument(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono DateTime dateTime; if (dateTimeOptions.DateOnly) { dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", }; dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } ticks = dateTime.Ticks; break; default: var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } BsonDateTime bsonDateTime; if (ticks.HasValue) { bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc)); } else { bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value); } if (dateTimeOptions.DateOnly) { var dateTime = bsonDateTime.Value; if (dateTime.TimeOfDay != TimeSpan.Zero) { throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero."); } bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeOptions.Kind)); // not ToLocalTime or ToUniversalTime! } else { if (bsonDateTime.IsValidDateTime) { var dateTime = bsonDateTime.Value; switch (dateTimeOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: dateTime = BsonUtils.ToLocalTime(dateTime, dateTimeOptions.Kind); break; case DateTimeKind.Utc: dateTime = BsonUtils.ToUniversalTime(dateTime); break; } bsonDateTime = BsonDateTime.Create(dateTime); } else { if (dateTimeOptions.Kind != DateTimeKind.Utc) { throw new FileFormatException("BsonDateTime is outside the range of .NET DateTime."); } } } return bsonDateTime; } }
private void ReadValue() { //bool value = true; _type = PBBsonReaderType.Value; switch (_bsonType) { case BsonType.Document: WriteLine(2, "ReadStartDocument"); WriteLine(1, "{"); //_indent += 2; Indent(2); _reader.ReadStartDocument(); _type = PBBsonReaderType.Document; //value = false; break; case BsonType.Array: WriteLine(2, "ReadStartArray"); WriteLine(1, "["); //_indent += 2; Indent(2); _reader.ReadStartArray(); _type = PBBsonReaderType.Array; //value = false; break; case BsonType.Binary: _value = BsonValue.Create(_reader.ReadBytes()); break; case BsonType.Boolean: _value = BsonValue.Create(_reader.ReadBoolean()); break; case BsonType.DateTime: _value = BsonValue.Create(_reader.ReadDateTime()); break; case BsonType.Double: _value = BsonValue.Create(_reader.ReadDouble()); break; case BsonType.Int32: _value = BsonValue.Create(_reader.ReadInt32()); break; case BsonType.Int64: _value = BsonValue.Create(_reader.ReadInt64()); break; case BsonType.JavaScript: _value = BsonValue.Create(_reader.ReadJavaScript()); break; case BsonType.JavaScriptWithScope: _value = BsonValue.Create(_reader.ReadJavaScriptWithScope()); break; case BsonType.MaxKey: _reader.ReadMaxKey(); _value = BsonMaxKey.Value; break; case BsonType.MinKey: _reader.ReadMinKey(); _value = BsonMinKey.Value; break; case BsonType.Null: _reader.ReadNull(); _value = BsonNull.Value; break; case BsonType.ObjectId: _value = BsonValue.Create(_reader.ReadObjectId()); break; case BsonType.RegularExpression: _value = BsonValue.Create(_reader.ReadRegularExpression()); break; case BsonType.String: _value = BsonValue.Create(_reader.ReadString()); break; case BsonType.Symbol: _value = BsonValue.Create(_reader.ReadSymbol()); break; case BsonType.Timestamp: _value = BsonValue.Create(_reader.ReadTimestamp()); break; case BsonType.Undefined: _reader.ReadUndefined(); _value = BsonUndefined.Value; break; default: throw new PBException("error unable to read value type {0}", _bsonType); } //return value; }
/// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { DateTime value; var bsonType = bsonReader.CurrentBsonType; var dateTimeOptions = (options == null) ? DateTimeSerializationOptions.Defaults : (DateTimeSerializationOptions)options; switch (bsonType) { case BsonType.DateTime: value = bsonReader.ReadDateTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); // ignore value (use Ticks instead) value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64("Ticks")), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTime from BsonType: {0}", bsonType); throw new FileFormatException(message); } if (dateTimeOptions.DateOnly) { if (value.TimeOfDay != TimeSpan.Zero) { throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero"); } value = DateTime.SpecifyKind(value, dateTimeOptions.Kind); // not ToLocalTime or ToUniversalTime! } else { switch (dateTimeOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: value = ToLocalTimeHelper(value, dateTimeOptions.Kind); break; case DateTimeKind.Utc: value = ToUniversalTimeHelper(value); break; } } return(value); }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BsonDateTime)); var dateTimeSerializationOptions = EnsureSerializationOptions <DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { long?millisecondsSinceEpoch = null; long?ticks = null; switch (bsonType) { case BsonType.DateTime: millisecondsSinceEpoch = bsonReader.ReadDateTime(); break; case BsonType.Document: bsonReader.ReadStartDocument(); millisecondsSinceEpoch = bsonReader.ReadDateTime("DateTime"); bsonReader.ReadName("Ticks"); var ticksValue = BsonValue.ReadFrom(bsonReader); if (!ticksValue.IsBsonUndefined) { ticks = ticksValue.ToInt64(); } bsonReader.ReadEndDocument(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono DateTime dateTime; if (dateTimeSerializationOptions.DateOnly) { dateTime = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK", }; dateTime = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } ticks = dateTime.Ticks; break; default: var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } BsonDateTime bsonDateTime; if (ticks.HasValue) { bsonDateTime = BsonDateTime.Create(new DateTime(ticks.Value, DateTimeKind.Utc)); } else { bsonDateTime = BsonDateTime.Create(millisecondsSinceEpoch.Value); } if (dateTimeSerializationOptions.DateOnly) { var dateTime = bsonDateTime.Value; if (dateTime.TimeOfDay != TimeSpan.Zero) { throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero."); } bsonDateTime = BsonDateTime.Create(DateTime.SpecifyKind(dateTime, dateTimeSerializationOptions.Kind)); // not ToLocalTime or ToUniversalTime! } else { if (bsonDateTime.IsValidDateTime) { var dateTime = bsonDateTime.Value; switch (dateTimeSerializationOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(BsonUtils.ToLocalTime(dateTime), dateTimeSerializationOptions.Kind); break; case DateTimeKind.Utc: dateTime = BsonUtils.ToUniversalTime(dateTime); break; } bsonDateTime = BsonDateTime.Create(dateTime); } else { if (dateTimeSerializationOptions.Kind != DateTimeKind.Utc) { throw new FileFormatException("BsonDateTime is outside the range of .NET DateTime."); } } } return(bsonDateTime); } }
public void TestDateTimeShell() { var json = "ISODate(\"1970-01-01T00:00:00Z\")"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.DateTime, bsonReader.ReadBsonType()); Assert.AreEqual(0, bsonReader.ReadDateTime()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Shell }; Assert.AreEqual(json, BsonSerializer.Deserialize<DateTime>(new StringReader(json)).ToJson(jsonSettings)); }
static object ReadObject(BsonReader bsonReader) //_120509_173140 keep consistent { switch (bsonReader.GetCurrentBsonType()) { case BsonType.Array: return(ReadArray(bsonReader)); // replacement case BsonType.Binary: var binary = BsonSerializer.Deserialize <BsonValue>(bsonReader); return(BsonTypeMapper.MapToDotNetValue(binary) ?? binary); // byte[] or Guid else self case BsonType.Boolean: return(bsonReader.ReadBoolean()); case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime())); case BsonType.Document: return(ReadCustomObject(bsonReader)); // replacement case BsonType.Double: return(bsonReader.ReadDouble()); case BsonType.Int32: return(bsonReader.ReadInt32()); case BsonType.Int64: return(bsonReader.ReadInt64()); case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.ObjectId: return(bsonReader.ReadObjectId()); case BsonType.String: return(bsonReader.ReadString()); default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader)); } }
// explicit interface implementation object IBsonSerializable.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { if (bsonReader.CurrentBsonType == Bson.BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartDocument(); BsonType bsonType; while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "abbreviated": _abbreviated = bsonReader.ReadString(); break; case "client": _client = bsonReader.ReadString(); break; case "command": _command = BsonDocument.ReadFrom(bsonReader); break; case "cursorid": _cursorId = BsonValue.ReadFrom(bsonReader).ToInt64(); break; case "err": _error = bsonReader.ReadString(); break; case "exception": _exception = bsonReader.ReadString(); break; case "exceptionCode": _exceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "exhaust": _exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmod": _fastMod = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmodinsert": _fastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "idhack": _idHack = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "info": _info = bsonReader.ReadString(); break; case "keyUpdates": _keyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "millis": _duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble()); break; case "moved": _moved = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "nreturned": _numberReturned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ns": _namespace = bsonReader.ReadString(); break; case "nscanned": _numberScanned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoreturn": _numberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoskip": _numberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "op": _op = bsonReader.ReadString(); break; case "query": _query = BsonDocument.ReadFrom(bsonReader); break; case "responseLength": _responseLength = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "scanAndOrder": _scanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "ts": _timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); break; case "updateobj": _updateObject = BsonDocument.ReadFrom(bsonReader); break; case "upsert": _upsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "user": _user = bsonReader.ReadString(); break; default: break; // ignore unknown elements } } bsonReader.ReadEndDocument(); return this; } }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { return BsonDateTime.Create(bsonReader.ReadDateTime()); } }
// explicit interface implementation object IBsonSerializable.Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { if (bsonReader.CurrentBsonType == Bson.BsonType.Null) { bsonReader.ReadNull(); return(null); } else { bsonReader.ReadStartDocument(); BsonType bsonType; while ((bsonType = bsonReader.ReadBsonType()) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "abbreviated": abbreviated = bsonReader.ReadString(); break; case "client": client = bsonReader.ReadString(); break; case "command": command = BsonDocument.ReadFrom(bsonReader); break; case "cursorid": cursorId = BsonValue.ReadFrom(bsonReader).ToInt64(); break; case "err": error = bsonReader.ReadString(); break; case "exception": exception = bsonReader.ReadString(); break; case "exceptionCode": exceptionCode = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "exhaust": exhaust = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmod": fastMod = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "fastmodinsert": fastModInsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "idhack": idHack = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "info": info = bsonReader.ReadString(); break; case "keyUpdates": keyUpdates = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "millis": duration = TimeSpan.FromMilliseconds(BsonValue.ReadFrom(bsonReader).ToDouble()); break; case "moved": moved = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "nreturned": numberReturned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ns": @namespace = bsonReader.ReadString(); break; case "nscanned": numberScanned = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoreturn": numberToReturn = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "ntoskip": numberToSkip = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "op": op = bsonReader.ReadString(); break; case "query": query = BsonDocument.ReadFrom(bsonReader); break; case "responseLength": responseLength = BsonValue.ReadFrom(bsonReader).ToInt32(); break; case "scanAndOrder": scanAndOrder = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "ts": timestamp = BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()); break; case "updateobj": updateObject = BsonDocument.ReadFrom(bsonReader); break; case "upsert": upsert = BsonValue.ReadFrom(bsonReader).ToBoolean(); break; case "user": user = bsonReader.ReadString(); break; default: break; // ignore unknown elements } } bsonReader.ReadEndDocument(); return(this); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTime)); var dateTimeSerializationOptions = EnsureSerializationOptions<DateTimeSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); DateTime value; switch (bsonType) { case BsonType.DateTime: // use an intermediate BsonDateTime so MinValue and MaxValue are handled correctly value = BsonDateTime.Create(bsonReader.ReadDateTime()).Value; break; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); // ignore value (use Ticks instead) value = new DateTime(bsonReader.ReadInt64("Ticks"), DateTimeKind.Utc); bsonReader.ReadEndDocument(); break; case BsonType.Int64: value = DateTime.SpecifyKind(new DateTime(bsonReader.ReadInt64()), DateTimeKind.Utc); break; case BsonType.String: // note: we're not using XmlConvert because of bugs in Mono if (dateTimeSerializationOptions.DateOnly) { value = DateTime.SpecifyKind(DateTime.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd", null), DateTimeKind.Utc); } else { var formats = new string[] { "yyyy-MM-ddK", "yyyy-MM-ddTHH:mm:ssK", "yyyy-MM-ddTHH:mm:ss.FFFFFFFK" }; value = DateTime.ParseExact(bsonReader.ReadString(), formats, null, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal); } break; default: var message = string.Format("Cannot deserialize DateTime from BsonType {0}.", bsonType); throw new FileFormatException(message); } if (dateTimeSerializationOptions.DateOnly) { if (value.TimeOfDay != TimeSpan.Zero) { throw new FileFormatException("TimeOfDay component for DateOnly DateTime value is not zero."); } value = DateTime.SpecifyKind(value, dateTimeSerializationOptions.Kind); // not ToLocalTime or ToUniversalTime! } else { switch (dateTimeSerializationOptions.Kind) { case DateTimeKind.Local: case DateTimeKind.Unspecified: value = DateTime.SpecifyKind(BsonUtils.ToLocalTime(value), dateTimeSerializationOptions.Kind); break; case DateTimeKind.Utc: value = BsonUtils.ToUniversalTime(value); break; } } return value; }