public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { object value = null; if (bsonReader.CurrentBsonType == BsonType.Null) { bsonReader.ReadNull(); } else { bsonReader.ReadStartArray(); var idList = new List<ObjectId>(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var id = (ObjectId)BsonSerializer.Deserialize(bsonReader, typeof(ObjectId)); idList.Add(id); } bsonReader.ReadEndArray(); if (idList.Count > 0) { var cursor = MongoDbProvider.Database.GetCollection(DocumentType, DocumentType.Name) .FindAs(DocumentType, Query.In("_id", BsonArray.Create(idList))); var documents = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(DocumentType)); foreach (var document in cursor) { documents.Add(document); } value = documents; } } return value; }
public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (_trace) pb.Trace.WriteLine("ZStringArraySerializer.Deserialize()"); VerifyTypes(nominalType, actualType, typeof(ZStringArray)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); //return new ZString(bsonReader.ReadString()); var array = new List<string>(); bsonType = bsonReader.ReadBsonType(); while (bsonType != BsonType.EndOfDocument) { if (bsonType != BsonType.String) throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType); var value = bsonReader.ReadString(); array.Add(value); bsonType = bsonReader.ReadBsonType(); } bsonReader.ReadEndArray(); return new ZStringArray(array.ToArray()); default: throw new PBException("error cannot deserialize ZStringArray from BsonType {0}.", bsonType); } }
public void TestBookmark() { var json = "{ \"x\" : 1, \"y\" : 2 }"; using (bsonReader = BsonReader.Create(json)) { // do everything twice returning to bookmark in between var bookmark = bsonReader.GetBookmark(); Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType()); bookmark = bsonReader.GetBookmark(); bsonReader.ReadStartDocument(); bsonReader.ReturnToBookmark(bookmark); bsonReader.ReadStartDocument(); bookmark = bsonReader.GetBookmark(); Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType()); bookmark = bsonReader.GetBookmark(); Assert.AreEqual("x", bsonReader.ReadName()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual("x", bsonReader.ReadName()); bookmark = bsonReader.GetBookmark(); Assert.AreEqual(1, bsonReader.ReadInt32()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual(1, bsonReader.ReadInt32()); bookmark = bsonReader.GetBookmark(); Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType()); bookmark = bsonReader.GetBookmark(); Assert.AreEqual("y", bsonReader.ReadName()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual("y", bsonReader.ReadName()); bookmark = bsonReader.GetBookmark(); Assert.AreEqual(2, bsonReader.ReadInt32()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual(2, bsonReader.ReadInt32()); bookmark = bsonReader.GetBookmark(); Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType()); bsonReader.ReturnToBookmark(bookmark); Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType()); bookmark = bsonReader.GetBookmark(); bsonReader.ReadEndDocument(); bsonReader.ReturnToBookmark(bookmark); bsonReader.ReadEndDocument(); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDocument>(new StringReader(json)).ToJson()); }
public void TestArrayEmpty() { var json = "[]"; using (bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType()); bsonReader.ReadStartArray(); Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType()); bsonReader.ReadEndArray(); Assert.AreEqual(BsonReaderState.Done, bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<BsonArray>(new StringReader(json)).ToJson()); }
public void TestArrayOneElement() { var json = "[1]"; using (_bsonReader = new JsonReader(json)) { Assert.AreEqual(BsonType.Array, _bsonReader.ReadBsonType()); _bsonReader.ReadStartArray(); Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndArray(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<BsonArray>(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="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 OXmlParagraphElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlParagraphElement paragraph = new OXmlParagraphElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; //if (bsonType != BsonType.String) // throw new PBException("error ZStringArray cannot contain value of type {0}", bsonType); //var value = bsonReader.ReadString(); string name = bsonReader.ReadName(); switch (name.ToLower()) { case "type": if (bsonType != BsonType.String) throw new PBException($"wrong type value {bsonType}"); string type = bsonReader.ReadString(); if (type.ToLower() != "paragraph") throw new PBException($"invalid Type {type} when deserialize OXmlParagraphElement"); break; case "style": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong style value {bsonType}"); paragraph.Style = bsonReader.ReadString(); break; default: throw new PBException($"unknow Paragraph value \"{name}\""); } } return paragraph; }
// 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(BsonArray)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); var array = new BsonArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var value = (BsonValue)BsonValueSerializer.Instance.Deserialize(bsonReader, typeof(BsonValue), null); array.Add(value); } bsonReader.ReadEndArray(); return array; default: var message = string.Format("Cannot deserialize BsonArray from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public override object Deserialize( BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options ) { var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } var nvc = new NameValueCollection(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var key = (string)StringSerializer.Instance.Deserialize(bsonReader, typeof(string), options); var val = (string)StringSerializer.Instance.Deserialize(bsonReader, typeof(string), options); bsonReader.ReadEndArray(); nvc.Add(key, val); } bsonReader.ReadEndArray(); return nvc; }
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; }
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.Array) { bsonReader.ReadStartArray(); var list = new ArrayList(); var discriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var serializer = BsonSerializer.LookupSerializer(elementType); var element = serializer.Deserialize(bsonReader, typeof(object), elementType, null); list.Add(element); } bsonReader.ReadEndArray(); return list; } else { var message = string.Format("Can't deserialize a {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) { 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 OXmlOpenHeaderElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlOpenHeaderElement element = new OXmlOpenHeaderElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; string name = bsonReader.ReadName(); switch (name.ToLower()) { case "type": if (bsonType != BsonType.String) throw new PBException($"wrong type value {bsonType}"); string type = bsonReader.ReadString(); //"openfooter" if (type.ToLower() != "openheader") throw new PBException($"invalid Type {type} when deserialize OXmlOpenHeader"); break; case "headertype": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong HeaderType value {bsonType}"); element.HeaderType = bsonReader.ReadString().zParseEnum<HeaderFooterValues>(); break; default: //OpenHeaderFooter throw new PBException($"unknow OpenHeader value \"{name}\""); } } return element; }
public OXmlSimpleFieldElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlSimpleFieldElement element = new OXmlSimpleFieldElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; string name = bsonReader.ReadName(); switch (name.ToLower()) { case "type": if (bsonType != BsonType.String) throw new PBException($"wrong type value {bsonType}"); string type = bsonReader.ReadString(); if (type.ToLower() != "simplefield") throw new PBException($"invalid Type {type} when deserialize OXmlSimpleFieldElement"); break; case "instruction": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong Instruction value {bsonType}"); element.Instruction = bsonReader.ReadString(); break; default: throw new PBException($"unknow SimpleField value \"{name}\""); } } return element; }
/// <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 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.ReadStartArray(); bsonReader.ReadBsonType(); var keyType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var keySerializer = BsonSerializer.LookupSerializer(keyType); var key = keySerializer.Deserialize(bsonReader, typeof(object), keyType, null); bsonReader.ReadBsonType(); 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 void TestArrayTwoElements() { var json = "[1, 2]"; using (_bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.Array, _bsonReader.ReadBsonType()); _bsonReader.ReadStartArray(); Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.AreEqual(2, _bsonReader.ReadInt32()); Assert.AreEqual(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndArray(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize<BsonArray>(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) { var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Array: var instance = CreateInstance(actualType); var itemDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); Type lastItemType = null; IBsonSerializer lastItemSerializer = null; bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var itemType = itemDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); IBsonSerializer itemSerializer; if (itemType == lastItemType) { itemSerializer = lastItemSerializer; } else { itemSerializer = BsonSerializer.LookupSerializer(itemType); lastItemType = itemType; lastItemSerializer = itemSerializer; } var item = itemSerializer.Deserialize(bsonReader, typeof(object), itemType, itemSerializationOptions); AddItem(instance, item); } bsonReader.ReadEndArray(); return FinalizeResult(instance, actualType); case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, actualType, options); bsonReader.ReadEndDocument(); return value; default: var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new Exception(message); } }
static IList ReadArray(BsonReader bsonReader) { var array = new ArrayList(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) array.Add(ReadObject(bsonReader)); bsonReader.ReadEndArray(); return array; }
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 OXmlDocSectionElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlDocSectionElement element = new OXmlDocSectionElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; string name = bsonReader.ReadName(); switch (name.ToLower()) { case "type": if (bsonType != BsonType.String) throw new PBException($"wrong type value {bsonType}"); string type = bsonReader.ReadString(); if (type.ToLower() != "docsection") throw new PBException($"invalid Type {type} when deserialize OXmlDocSectionElement"); break; case "pagesize": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Document) throw new PBException($"wrong PageSize value {bsonType}"); element.PageSize = ReadPageSize(bsonReader); break; case "pagemargin": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Document) throw new PBException($"wrong PageMargin value {bsonType}"); element.PageMargin = ReadPageMargin(bsonReader); break; case "pagenumberstart": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Int32) throw new PBException($"wrong PageNumberStart value {bsonType}"); element.PageNumberStart = bsonReader.ReadInt32(); break; default: throw new PBException($"unknow DocSection value \"{name}\""); } } return element; }
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 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) { var arraySerializationOptions = EnsureSerializationOptions<ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Array: bsonReader.ReadStartArray(); var queue = new Queue(); var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var serializer = BsonSerializer.LookupSerializer(elementType); var element = serializer.Deserialize(bsonReader, typeof(object), elementType, itemSerializationOptions); queue.Enqueue(element); } bsonReader.ReadEndArray(); return queue; case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, actualType, options); bsonReader.ReadEndDocument(); return value; default: var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
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 OXmlTextElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlTextElement element = new OXmlTextElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; string name = bsonReader.ReadName(); switch (name.ToLower()) { case "type": if (bsonType != BsonType.String) throw new PBException($"wrong type value {bsonType}"); string type = bsonReader.ReadString(); if (type.ToLower() != "text") throw new PBException($"invalid Type {type} when deserialize OXmlTextElement"); break; case "text": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong text value {bsonType}"); element.Text = bsonReader.ReadString(); break; case "preservespace": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Boolean) throw new PBException($"wrong PreserveSpace value {bsonType}"); element.PreserveSpace = bsonReader.ReadBoolean(); break; default: throw new PBException($"unknow Text value \"{name}\""); } } return element; }
public OXmlDocDefaultsRunPropertiesElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options) { OXmlDocDefaultsRunPropertiesElement element = new OXmlDocDefaultsRunPropertiesElement(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) break; string name = bsonReader.ReadName(); switch (name.ToLower()) { case "type": if (bsonType != BsonType.String) throw new PBException($"wrong type value {bsonType}"); string type = bsonReader.ReadString(); if (type.ToLower() != "docdefaultsrunproperties") throw new PBException($"invalid Type {type} when deserialize OXmlDocDefaultsRunPropertiesElement"); break; case "runfonts": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.Document) throw new PBException($"wrong RunFonts value {bsonType}"); element.RunFonts = OXmlCommonSerializer.ReadRunFonts(bsonReader); break; case "fontsize": if (bsonType == BsonType.Null) break; if (bsonType != BsonType.String) throw new PBException($"wrong FontSize value {bsonType}"); element.FontSize = bsonReader.ReadString(); break; default: throw new PBException($"unknow DocDefaultsRunProperties value \"{name}\""); } } return element; }
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; }
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); } }