// 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(double)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return bsonReader.ReadDouble(); case BsonType.Int32: return representationSerializationOptions.ToDouble(bsonReader.ReadInt32()); case BsonType.Int64: return representationSerializationOptions.ToDouble(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDouble(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Double from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { object value = null; var valueType = actualType.GetConceptValueType(); if (valueType == typeof(Guid)) { var guidBytes = new byte[16]; BsonBinarySubType subType; bsonReader.ReadBinaryData (out guidBytes, out subType); value = new Guid (guidBytes); } else if (valueType == typeof(double)) value = bsonReader.ReadDouble (); else if (valueType == typeof(float)) value = (float)bsonReader.ReadDouble (); else if (valueType == typeof(Int32)) value = bsonReader.ReadInt32 (); else if (valueType == typeof(Int64)) value = bsonReader.ReadInt64 (); else if (valueType == typeof(bool)) value = bsonReader.ReadBoolean (); else if (valueType == typeof(string)) value = bsonReader.ReadString (); else if (valueType == typeof(decimal)) value = decimal.Parse (bsonReader.ReadString ()); var concept = ConceptFactory.CreateConceptInstance(actualType, value); return concept; }
public override object Deserialize(BsonReader bsonReader, System.Type nominalType, System.Type actualType, IBsonSerializationOptions options) { bsonReader.ReadStartDocument(); var min = ReadVector(bsonReader, "Min"); var max = ReadVector(bsonReader, "Max"); var width = bsonReader.ReadDouble("Width"); var height = bsonReader.ReadDouble("Height"); bsonReader.ReadEndDocument(); return new Rectangle(min, max); }
// 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(bool)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Boolean: return bsonReader.ReadBoolean(); case BsonType.Double: return bsonReader.ReadDouble() != 0.0; case BsonType.Int32: return bsonReader.ReadInt32() != 0; case BsonType.Int64: return bsonReader.ReadInt64() != 0; case BsonType.Null: bsonReader.ReadNull(); return false; case BsonType.String: return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower()); default: var message = string.Format("Cannot deserialize Boolean 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(decimal)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null); var bits = new int[4]; bits[0] = array[0].AsInt32; bits[1] = array[1].AsInt32; bits[2] = array[2].AsInt32; bits[3] = array[3].AsInt32; return new decimal(bits); case BsonType.Double: return representationSerializationOptions.ToDecimal(bsonReader.ReadDouble()); case BsonType.Int32: return representationSerializationOptions.ToDecimal(bsonReader.ReadInt32()); case BsonType.Int64: return representationSerializationOptions.ToDecimal(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDecimal(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Decimal 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(TimeSpan)); // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions<TimeSpanSerializationOptions>(options); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return FromDouble(bsonReader.ReadDouble(), timeSpanSerializationOptions.Units); case BsonType.Int32: return FromInt32(bsonReader.ReadInt32(), timeSpanSerializationOptions.Units); case BsonType.Int64: return FromInt64(bsonReader.ReadInt64(), timeSpanSerializationOptions.Units); case BsonType.String: return TimeSpan.Parse(bsonReader.ReadString()); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan) default: var message = string.Format("Cannot deserialize TimeSpan 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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartArray(); var longitude = bsonReader.ReadDouble(); var latitude = bsonReader.ReadDouble(); bsonReader.ReadEndArray(); return new GeoJson2DGeographicCoordinates(longitude, latitude); } }
// 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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartArray(); var easting = bsonReader.ReadDouble(); var northing = bsonReader.ReadDouble(); bsonReader.ReadEndArray(); return new GeoJson2DProjectedCoordinates(easting, northing); } }
// 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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartArray(); var x = bsonReader.ReadDouble(); var y = bsonReader.ReadDouble(); var z = bsonReader.ReadDouble(); bsonReader.ReadEndArray(); return new GeoJson3DCoordinates(x, y, z); } }
protected double? ReadNullableDouble(BsonReader reader,string name) { reader.ReadName(name); var type = reader.GetCurrentBsonType(); if ( type == BsonType.Null) { reader.ReadNull(); return null; } return reader.ReadDouble(); }
// 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, // ignored IBsonSerializationOptions options) { VerifyDeserializeType(nominalType); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return Enum.ToObject(nominalType, bsonReader.ReadInt32()); case BsonType.Int64: return Enum.ToObject(nominalType, bsonReader.ReadInt64()); case BsonType.Double: return Enum.ToObject(nominalType, (long)bsonReader.ReadDouble()); case BsonType.String: return Enum.Parse(nominalType, bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, 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(BsonDouble)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return new BsonDouble(bsonReader.ReadDouble()); default: var message = string.Format("Cannot deserialize BsonDouble from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { switch (bsonReader.CurrentBsonType) { case BsonType.Boolean: return bsonReader.ReadBoolean(); case BsonType.Double: return bsonReader.ReadDouble() != 0.0; case BsonType.Int32: return bsonReader.ReadInt32() != 0; case BsonType.Int64: return bsonReader.ReadInt64() != 0; case BsonType.Null: bsonReader.ReadNull(); return false; case BsonType.String: return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower()); default: var message = string.Format("Cannot deserialize Boolean from BsonType: {0}", bsonReader.CurrentBsonType); throw new FileFormatException(message); } }
/// <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 representationOptions = (RepresentationSerializationOptions) options ?? defaultRepresentationOptions; var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Array: var array = BsonArray.ReadFrom(bsonReader); var bits = new int[4]; bits[0] = array[0].AsInt32; bits[1] = array[1].AsInt32; bits[2] = array[2].AsInt32; bits[3] = array[3].AsInt32; return new decimal(bits); case BsonType.Double: return representationOptions.ToDecimal(bsonReader.ReadDouble()); case BsonType.Int32: return representationOptions.ToDecimal(bsonReader.ReadInt32()); case BsonType.Int64: return representationOptions.ToDecimal(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDecimal(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Decimal from BsonType: {0}", bsonType); throw new FileFormatException(message); } }
//_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 TestDouble() { var json = "1.5"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.Double, bsonReader.ReadBsonType()); Assert.AreEqual(1.5, bsonReader.ReadDouble()); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<double>(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(TimeSpan)); // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions<TimeSpanSerializationOptions>(options); BsonType bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.String) { return TimeSpan.Parse(bsonReader.ReadString()); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan) } else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks) { long ticks; switch (bsonType) { case BsonType.Double: ticks = (long)bsonReader.ReadDouble(); break; case BsonType.Int32: ticks = (long)bsonReader.ReadInt32(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } return new TimeSpan(ticks); } else { double interval; switch (bsonType) { case BsonType.Double: interval = bsonReader.ReadDouble(); break; case BsonType.Int32: interval = bsonReader.ReadInt32(); break; case BsonType.Int64: interval = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } switch (timeSpanSerializationOptions.Units) { case TimeSpanUnits.Days: return TimeSpan.FromDays(interval); case TimeSpanUnits.Hours: return TimeSpan.FromHours(interval); case TimeSpanUnits.Minutes: return TimeSpan.FromMinutes(interval); case TimeSpanUnits.Seconds: return TimeSpan.FromSeconds(interval); case TimeSpanUnits.Milliseconds: return TimeSpan.FromMilliseconds(interval); case TimeSpanUnits.Nanoseconds: return TimeSpan.FromMilliseconds(interval / 1000.0); default: var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units); throw new BsonSerializationException(message); } } }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var versionAsDouble = bsonReader.ReadDouble(); var version = EventSourceVersion.FromCombined(versionAsDouble); return version; }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { double doubleValue; var bsonType = bsonReader.CurrentBsonType; bool lostData = false; switch (bsonType) { case BsonType.Double: doubleValue = bsonReader.ReadDouble(); break; case BsonType.Int32: var int32Value = bsonReader.ReadInt32(); doubleValue = int32Value; lostData = (int) doubleValue != int32Value; break; case BsonType.Int64: var int64Value = bsonReader.ReadInt64(); doubleValue = int64Value; lostData = (long) doubleValue != int64Value; break; case BsonType.String: doubleValue = XmlConvert.ToDouble(bsonReader.ReadString()); break; default: var message = string.Format("Cannot deserialize Single from BsonType: {0}", bsonType); throw new FileFormatException(message); } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to Single", bsonType); throw new FileFormatException(message); } var floatValue = (doubleValue == double.MinValue) ? float.MinValue : (doubleValue == double.MaxValue) ? float.MaxValue : (float) doubleValue; return floatValue; }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { long value; bool lostData = false; var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Double: var doubleValue = bsonReader.ReadDouble(); value = (long) doubleValue; lostData = (double) value != doubleValue; break; case BsonType.Int32: value = bsonReader.ReadInt32(); break; default: value = bsonReader.ReadInt64(); break; } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to Int64", bsonType); throw new FileFormatException(message); } return value; }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { short value; var bsonType = bsonReader.CurrentBsonType; var lostData = false; switch (bsonType) { case BsonType.Double: var doubleValue = bsonReader.ReadDouble(); value = (short) doubleValue; lostData = (double) value != doubleValue; break; case BsonType.Int32: var int32Value = bsonReader.ReadInt32(); value = (short) int32Value; lostData = (int) value != int32Value; break; case BsonType.Int64: var int64Value = bsonReader.ReadInt64(); value = (short) int64Value; lostData = (long) value != int64Value; break; case BsonType.String: value = XmlConvert.ToInt16(bsonReader.ReadString()); break; default: var message = string.Format("Cannot deserialize Int16 from BsonType: {0}", bsonType); throw new FileFormatException(message); } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to Int16", bsonType); throw new FileFormatException(message); } return value; }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { return bsonReader.ReadDouble(); }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { BsonType bsonType = bsonReader.CurrentBsonType; double doubleValue; if (bsonType == BsonType.Double) { doubleValue = bsonReader.ReadDouble(); } else { var message = string.Format("Cannot deserialize Single from BsonType: {0}", bsonType); throw new FileFormatException(message); } return doubleValue == double.MinValue ? float.MinValue : doubleValue == double.MaxValue ? float.MaxValue : (float) doubleValue; }
// 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(ushort)); var representationOptions = (RepresentationSerializationOptions)options ?? defaultRepresentationOptions; var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Double: return representationOptions.ToUInt16(bsonReader.ReadDouble()); case BsonType.Int32: return representationOptions.ToUInt16(bsonReader.ReadInt32()); case BsonType.Int64: return representationOptions.ToUInt16(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToUInt16(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize uInt16 from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else { return BsonDouble.Create(bsonReader.ReadDouble()); } }