// 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.ReadStartDocument(); DeserializeType(bsonReader, "link"); bsonReader.ReadName("properties"); bsonReader.ReadStartDocument(); var href = bsonReader.ReadString("href"); string hrefType = null; if (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { hrefType = bsonReader.ReadString("type"); } bsonReader.ReadEndDocument(); bsonReader.ReadEndDocument(); return new GeoJsonLinkedCoordinateReferenceSystem(href, hrefType); } }
// 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(CultureInfo)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Document: bsonReader.ReadStartDocument(); var name = bsonReader.ReadString("Name"); var useUserOverride = bsonReader.ReadBoolean("UseUserOverride"); bsonReader.ReadEndDocument(); return new CultureInfo(name, useUserOverride); case BsonType.String: return new CultureInfo(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize CultureInfo 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(BsonNull)); var bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return BsonNull.Value; case BsonType.Document: bsonReader.ReadStartDocument(); var name = bsonReader.ReadName(); if (name == "_csharpnull" || name == "$csharpnull") { var csharpNull = bsonReader.ReadBoolean(); bsonReader.ReadEndDocument(); return csharpNull ? null : BsonNull.Value; } else { message = string.Format("Unexpected element name while deserializing a BsonNull: {0}.", name); throw new FileFormatException(message); } default: message = string.Format("Cannot deserialize BsonNull from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
private static OXmlPageSize ReadPageSize(BsonReader bsonReader) { bsonReader.ReadStartDocument(); OXmlPageSize value = new OXmlPageSize(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; string name = bsonReader.ReadName(); switch (name.ToLower()) { case "width": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Int32) throw new PBException($"wrong PageSize width value {bsonType}"); value.Width = bsonReader.ReadInt32(); break; case "height": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Int32) throw new PBException($"wrong PageSize height value {bsonType}"); value.Height = bsonReader.ReadInt32(); break; default: throw new PBException($"unknow PageSize value \"{name}\""); } } bsonReader.ReadEndDocument(); 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 ) { if (nominalType != typeof(object)) { var message = string.Format("ObjectSerializer called for nominal type {0}.", nominalType.FullName); throw new InvalidOperationException(message); } var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.ReadBsonType() == BsonType.EndOfDocument) { bsonReader.ReadEndDocument(); return new object(); } else { bsonReader.ReturnToBookmark(bookmark); } } var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); var actualType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); if (actualType == typeof(object)) { var message = string.Format("Unable to determine actual type of object to deserialize. NominalType is System.Object and BsonType is {0}.", bsonType); throw new FileFormatException(message); } var serializer = BsonSerializer.LookupSerializer(actualType); return serializer.Deserialize(bsonReader, nominalType, actualType, null); }
// 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 Exception(message); } }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { BsonType bsonType = bsonReader.CurrentBsonType; BitArray bitArray; byte[] bytes; BsonBinarySubType subType; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: bsonReader.ReadBinaryData(out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { message = string.Format("Invalid Binary sub type: {0}", subType); throw new FileFormatException(message); } return new BitArray(bytes); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); bsonReader.ReadBinaryData("Bytes", out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { message = string.Format("Invalid Binary sub type: {0}", subType); throw new FileFormatException(message); } bsonReader.ReadEndDocument(); bitArray = new BitArray(bytes); bitArray.Length = length; return bitArray; case BsonType.String: var s = bsonReader.ReadString(); bitArray = new BitArray(s.Length); for (int i = 0; i < s.Length; i++) { var c = s[i]; switch (c) { case '0': break; case '1': bitArray[i] = true; break; default: throw new FileFormatException("String value is not a valid BitArray"); } } return bitArray; default: message = string.Format("Cannot deserialize Byte[] from BsonType: {0}", bsonType); throw new FileFormatException(message); } }
// public methods public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return null; } else { bsonReader.ReadStartDocument(); DeserializeType(bsonReader, "name"); bsonReader.ReadName("properties"); bsonReader.ReadStartDocument(); var name = bsonReader.ReadString("name"); bsonReader.ReadEndDocument(); bsonReader.ReadEndDocument(); return new GeoJsonNamedCoordinateReferenceSystem(name); } }
public Type GetActualType(BsonReader bsonReader, Type nominalType) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; if (bsonReader.FindElement("Type")) { var discriminator = BsonValue.ReadFrom(bsonReader); actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return actualType; }
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 override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { var dictionary = CreateInstance(nominalType); bsonReader.ReadStartDocument(); var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var key = bsonReader.ReadName(); var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null); dictionary.Add(key, value); } bsonReader.ReadEndDocument(); return dictionary; } else if (bsonType == BsonType.Array) { var dictionary = CreateInstance(nominalType); bsonReader.ReadStartArray(); var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.SkipName(); bsonReader.ReadStartArray(); bsonReader.ReadBsonType(); bsonReader.SkipName(); var keyType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var keySerializer = BsonSerializer.LookupSerializer(keyType); var key = keySerializer.Deserialize(bsonReader, typeof(object), keyType, null); bsonReader.ReadBsonType(); bsonReader.SkipName(); var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null); bsonReader.ReadEndArray(); dictionary.Add(key, value); } bsonReader.ReadEndArray(); return dictionary; } else { var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (_trace) pb.Trace.WriteLine("OXmlSimpleFieldElementSerializer.Deserialize()"); VerifyTypes(nominalType, actualType, typeof(OXmlSimpleFieldElement)); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType != BsonType.Document) throw new PBException($"deserialize OXmlSimpleFieldElement, invalid BsonType {bsonType}."); bsonReader.ReadStartDocument(); object value = _Deserialize(bsonReader, options); bsonReader.ReadEndDocument(); return value; }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <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(BitArray)); BsonType bsonType = bsonReader.GetCurrentBsonType(); BitArray bitArray; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: return new BitArray(bsonReader.ReadBytes()); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); var bytes = bsonReader.ReadBytes("Bytes"); bsonReader.ReadEndDocument(); bitArray = new BitArray(bytes); bitArray.Length = length; return bitArray; case BsonType.String: var s = bsonReader.ReadString(); bitArray = new BitArray(s.Length); for (int i = 0; i < s.Length; i++) { var c = s[i]; switch (c) { case '0': break; case '1': bitArray[i] = true; break; default: throw new FileFormatException("String value is not a valid BitArray."); } } return bitArray; default: var message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
static PSObject ReadCustomObject(BsonReader bsonReader) { var ps = new PSObject(); var properties = ps.Properties; bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); var value = ReadObject(bsonReader); properties.Add(new PSNoteProperty(name, value), true); //! true is faster } bsonReader.ReadEndDocument(); return ps; }
public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { var dictionary = CreateInstance(nominalType); bsonReader.ReadStartDocument(); var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var key = bsonReader.ReadName(); //TODO:对键进行字符串留用.Intern. if (key.Length < 16) { key = String.Intern(key); } var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null); //dictionary.Add(key, value); if (dictionary is IDictionary<string, object>) { ((IDictionary<string, object>)dictionary).Add(key, value); } else if (dictionary is IDictionary) { ((IDictionary)dictionary).Add(key, value); } } bsonReader.ReadEndDocument(); return dictionary; } else { var message = string.Format("Can't deserialize a {0} from BsonType {1}", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var nvc = new NameValueCollection(); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName().Replace("__period__", "."); var value = bsonReader.ReadString(); nvc.Add(name, value); } bsonReader.ReadEndDocument(); return nvc; }
// 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 override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { BsonType bsonType = bsonReader.CurrentBsonType; object result; if( bsonType == BsonType.Null ) { bsonReader.ReadNull(); result = null; } else { if( bsonType == BsonType.Document ) { var dictionary = new DynamicDictionary(); bsonReader.ReadStartDocument(); IDiscriminatorConvention valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention( typeof( object ) ); while( bsonReader.ReadBsonType() != BsonType.EndOfDocument ) { string key = bsonReader.ReadName(); Type valueType = valueDiscriminatorConvention.GetActualType( bsonReader, typeof( object ) ); IBsonSerializer valueSerializer = BsonSerializer.LookupSerializer( valueType ); object value = valueSerializer.Deserialize( bsonReader, typeof( object ), valueType, null ); if( key != "_t" ) { dictionary.Add( key.Replace( '\x03', '.' ), value ); } } bsonReader.ReadEndDocument(); result = dictionary; } else { string message = string.Format( "Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType ); throw new BsonException( message ); } } return result; }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (_trace) pb.Trace.WriteLine("OXmlParagraphElementSerializer.Deserialize()"); VerifyTypes(nominalType, actualType, typeof(OXmlParagraphElement)); var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType != BsonType.Document) throw new PBException($"deserialize OXmlParagraphElement, invalid BsonType {bsonType}."); bsonReader.ReadStartDocument(); //string type = bsonReader.FindStringElement("Type"); //if (type.ToLower() != "paragraph") // throw new PBException($"error deserialize OXmlParagraphElement, invalid Type {type}."); object value = _Deserialize(bsonReader, options); bsonReader.ReadEndDocument(); return value; }
static object Deserialize(BsonReader bsonReader) { var currentBsonType = bsonReader.GetCurrentBsonType(); switch (currentBsonType) { case BsonType.Null: return null; case BsonType.Document: bsonReader.ReadStartDocument(); var hour = bsonReader.ReadInt32("Hour"); var minute = bsonReader.ReadInt32("Minute"); var second = bsonReader.ReadInt32("Second"); bsonReader.ReadEndDocument(); return new TimeOfDay(hour, minute, second); default: throw new NotSupportedException( string.Format("Bson type : {0} is not supported for TimeOfDay type property", currentBsonType)); } }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { bsonReader.ReadStartDocument(); bsonReader.ReadName(); var typeName = bsonReader.ReadString(); bsonReader.ReadName(); var methodSignature = bsonReader.ReadString(); bsonReader.ReadEndDocument(); var type = Type.GetType(typeName); if (type != null) { var method = type.GetMethods().Where(m => GetMethodSignature(m) == methodSignature).SingleOrDefault(); return method; } return null; }
public Type GetActualType( BsonReader bsonReader, Type nominalType ) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; while(bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); if(name == "OnlyInB") { actualType = typeof(B); break; } else if(name == "OnlyInC") { actualType = typeof(C); break; } bsonReader.SkipValue(); } bsonReader.ReturnToBookmark(bookmark); return actualType; }
// 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(BsonDocument)); var bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Document: var documentSerializationOptions = (options ?? DocumentSerializationOptions.Defaults) as DocumentSerializationOptions; if (documentSerializationOptions == null) { message = string.Format( "Serialize method of BsonDocument expected serialization options of type {0}, not {1}.", BsonUtils.GetFriendlyTypeName(typeof(DocumentSerializationOptions)), BsonUtils.GetFriendlyTypeName(options.GetType())); throw new BsonSerializationException(message); } bsonReader.ReadStartDocument(); var document = new BsonDocument(documentSerializationOptions.AllowDuplicateNames); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); document.Add(name, value); } bsonReader.ReadEndDocument(); return document; default: message = string.Format("Cannot deserialize BsonDocument from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object of type System.Drawing.Size 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(System.Drawing.Size)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Document: bsonReader.ReadStartDocument(); var width = bsonReader.ReadInt32("Width"); var height = bsonReader.ReadInt32("Height"); bsonReader.ReadEndDocument(); return new System.Drawing.Size(width, height); default: var message = string.Format("Cannot deserialize Size from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// private methods private Type GetActualType(BsonReader bsonReader) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); if (bsonReader.FindElement("type")) { var type = bsonReader.ReadString(); bsonReader.ReturnToBookmark(bookmark); switch (type) { case "link": return typeof(GeoJsonLinkedCoordinateReferenceSystem); case "name": return typeof(GeoJsonNamedCoordinateReferenceSystem); default: var message = string.Format("The type field of the GeoJsonCoordinateReferenceSystem is not valid: '{0}'.", type); throw new FormatException(message); } } else { throw new FormatException("GeoJsonCoordinateReferenceSystem object is missing the type field."); } }
public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options) { dynamic item = new ebayItemDataForSeller(); IDictionary<String, Object> dict = new Dictionary<String, Object>(); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var elementName = bsonReader.ReadName(); var pi = nominalType.GetProperty(elementName); if (pi != null) { dynamic data = null; if (bsonReader.CurrentBsonType.Equals(BsonType.Null)) { bsonReader.ReadNull(); } else { data = BsonDefaultSerializer.Instance.GetSerializer(pi.PropertyType).Deserialize(bsonReader, pi.PropertyType, options); this.GetType().GetProperty(pi.Name).SetValue(item, data, null); } } else { if (bsonReader.CurrentBsonType.Equals(BsonType.Null)) { bsonReader.SkipValue(); } else dict[elementName] = bsonReader.ReadString(); } } bsonReader.ReadEndDocument(); item.SetDynamics(dict); return this; }
#pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary public override object Deserialize( BsonReader bsonReader, Type nominalType ) { BsonType bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Binary) { byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { var message = string.Format("Invalid Binary sub type: {0}", subType); throw new FileFormatException(message); } return new BitArray(bytes); } else if (bsonType == BsonType.Document) { bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData("Bytes", out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { var message = string.Format("Invalid Binary sub type: {0}", subType); throw new FileFormatException(message); } bsonReader.ReadEndDocument(); var bitArray = new BitArray(bytes); bitArray.Length = length; return bitArray; } else { var message = string.Format("Cannot deserialize Byte[] from BsonType: {0}", bsonType); throw new FileFormatException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType ) { if (nominalType != typeof(object)) { var message = string.Format("ObjectSerializer called for type: {0}", nominalType.FullName); throw new InvalidOperationException(message); } var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); switch (bsonReader.ReadBsonType()) { case BsonType.EndOfDocument: bsonReader.ReadEndDocument(); return new object(); default: bsonReader.ReturnToBookmark(bookmark); var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); var actualType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); if (actualType == typeof(object)) { throw new BsonSerializationException("Unable to determine actual type of document to deserialize"); } var serializer = BsonSerializer.LookupSerializer(actualType); return serializer.Deserialize(bsonReader, nominalType, actualType); } } else { var message = string.Format("Cannot deserialize an object from BsonType: {0}", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Gets the actual type of an object by reading the discriminator from a BsonReader. /// </summary> /// <param name="bsonReader">The reader.</param> /// <param name="nominalType">The nominal type.</param> /// <returns>The actual type.</returns> public Type GetActualType(BsonReader bsonReader, Type nominalType) { // the BsonReader is sitting at the value whose actual type needs to be found var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Document) { // ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer) BsonSerializer.EnsureKnownTypesAreRegistered(nominalType); // we can skip looking for a discriminator if nominalType has no discriminated sub types if (BsonSerializer.IsTypeDiscriminated(nominalType)) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; if (bsonReader.FindElement(_elementName)) { var context = BsonDeserializationContext.CreateRoot<BsonValue>(bsonReader); var discriminator = BsonValueSerializer.Instance.Deserialize(context); if (discriminator.IsBsonArray) { discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator } actualType = BsonSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return actualType; } } return nominalType; }
// public methods /// <summary> /// Gets the actual type of an object by reading the discriminator from a BsonReader. /// </summary> /// <param name="bsonReader">The reader.</param> /// <param name="nominalType">The nominal type.</param> /// <returns>The actual type.</returns> public Type GetActualType(BsonReader bsonReader, Type nominalType) { // the BsonReader is sitting at the value whose actual type needs to be found var bsonType = bsonReader.CurrentBsonType; if (bsonReader.State == BsonReaderState.Value) { Type primitiveType = null; switch (bsonType) { case BsonType.Boolean: primitiveType = typeof(bool); break; case BsonType.Binary: var bookmark = bsonReader.GetBookmark(); byte[] bytes; BsonBinarySubType subType; bsonReader.ReadBinaryData(out bytes, out subType); if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy) { primitiveType = typeof(Guid); } bsonReader.ReturnToBookmark(bookmark); break; case BsonType.DateTime: primitiveType = typeof(DateTime); break; case BsonType.Double: primitiveType = typeof(double); break; case BsonType.Int32: primitiveType = typeof(int); break; case BsonType.Int64: primitiveType = typeof(long); break; case BsonType.ObjectId: primitiveType = typeof(ObjectId); break; case BsonType.String: primitiveType = typeof(string); break; } if (primitiveType != null && nominalType.IsAssignableFrom(primitiveType)) { return primitiveType; } } if (bsonType == BsonType.Document) { // ensure KnownTypes of nominalType are registered (so IsTypeDiscriminated returns correct answer) BsonDefaultSerializer.EnsureKnownTypesAreRegistered(nominalType); // we can skip looking for a discriminator if nominalType has no discriminated sub types if (BsonDefaultSerializer.IsTypeDiscriminated(nominalType)) { var bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); var actualType = nominalType; if (bsonReader.FindElement(_elementName)) { var discriminator = BsonValue.ReadFrom(bsonReader); if (discriminator.IsBsonArray) { discriminator = discriminator.AsBsonArray.Last(); // last item is leaf class discriminator } actualType = BsonDefaultSerializer.LookupActualType(nominalType, discriminator); } bsonReader.ReturnToBookmark(bookmark); return actualType; } } return nominalType; }