// 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) { 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 list = new List <T>(); var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(T)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(T)); var serializer = BsonSerializer.LookupSerializer(elementType); var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, itemSerializationOptions); list.Add(element); } bsonReader.ReadEndArray(); return(CreateInstance(actualType, list)); 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 void TestNestedArray() { var json = "{ \"a\" : [1, 2] }"; using (_bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Array, _bsonReader.ReadBsonType()); Assert.AreEqual("a", _bsonReader.ReadName()); _bsonReader.ReadStartArray(); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual(2, _bsonReader.ReadInt32()); _bsonReader.ReadEndArray(); _bsonReader.ReadEndDocument(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonDocument>(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 bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: bsonReader.ReadStartArray(); var stack = new Stack(); 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); stack.Push(element); } bsonReader.ReadEndArray(); return(stack); 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); } }
/// <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(T[])); var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: bsonReader.ReadStartArray(); var list = new List <T>(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var element = BsonSerializer.Deserialize <T>(bsonReader); list.Add(element); } bsonReader.ReadEndArray(); return(list.ToArray()); 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}.", actualType.FullName, bsonType); throw new FileFormatException(message); } }
public void TestBsonAwesome() { string byteString = @"1\x00\x00\x00\x04BSON\x00&\x00\x00\x00\x020\x00\x08\x00\x00\x00awesome\x00\x011\x00333333\x14@\x102\x00\xc2\x07\x00\x00\x00\x00"; byte[] bytes = DecodeByteString(byteString); MemoryStream stream = new MemoryStream(bytes); using (BsonReader bsonReader = BsonReader.Create(stream)) { bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType()); Assert.AreEqual("BSON", bsonReader.ReadName()); bsonReader.ReadStartArray(); Assert.AreEqual(BsonType.String, bsonReader.ReadBsonType()); Assert.AreEqual("awesome", bsonReader.ReadString()); Assert.AreEqual(BsonType.Double, bsonReader.ReadBsonType()); Assert.AreEqual(5.05, bsonReader.ReadDouble()); Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType()); Assert.AreEqual(1986, bsonReader.ReadInt32()); bsonReader.ReadEndArray(); bsonReader.ReadEndDocument(); } }
// 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 { var positions = new List <TCoordinates>(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var position = (TCoordinates)_coordinatesSerializer.Deserialize(bsonReader, typeof(TCoordinates), null); positions.Add(position); } bsonReader.ReadEndArray(); return(new GeoJsonMultiPointCoordinates <TCoordinates>(positions)); } }
// 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 { var lineStrings = new List <GeoJsonLineStringCoordinates <TCoordinates> >(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var lineString = (GeoJsonLineStringCoordinates <TCoordinates>)_lineStringCoordinatesSerializer.Deserialize(bsonReader, typeof(TCoordinates), null); lineStrings.Add(lineString); } bsonReader.ReadEndArray(); return(new GeoJsonMultiLineStringCoordinates <TCoordinates>(lineStrings)); } }
// 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 { var holes = new List <GeoJsonLinearRingCoordinates <TCoordinates> >(); bsonReader.ReadStartArray(); var exterior = (GeoJsonLinearRingCoordinates <TCoordinates>)_linearRingSerializer.Deserialize(bsonReader, typeof(TCoordinates), null); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var hole = (GeoJsonLinearRingCoordinates <TCoordinates>)_linearRingSerializer.Deserialize(bsonReader, typeof(TCoordinates), null); holes.Add(hole); } bsonReader.ReadEndArray(); return(new GeoJsonPolygonCoordinates <TCoordinates>(exterior, holes)); } }
// 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) { var dictionarySerializationOptions = EnsureSerializationOptions(options); var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else if (bsonType == BsonType.Document) { if (nominalType == typeof(object)) { bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, options); // recursive call replacing nominalType with actualType bsonReader.ReadEndDocument(); return(value); } var dictionary = CreateInstance(actualType); var valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var key = bsonReader.ReadName(); var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, keyValuePairSerializationOptions.ValueSerializationOptions); dictionary.Add(key, value); } bsonReader.ReadEndDocument(); return(dictionary); } else if (bsonType == BsonType.Array) { var dictionary = CreateInstance(actualType); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var keyValuePair = (KeyValuePair <object, object>)_keyValuePairSerializer.Deserialize( bsonReader, typeof(KeyValuePair <object, object>), keyValuePairSerializationOptions); dictionary.Add(keyValuePair.Key, keyValuePair.Value); } bsonReader.ReadEndArray(); return(dictionary); } else { var message = string.Format("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new Exception(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(T[, ])); var arraySerializationOptions = EnsureSerializationOptions <ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: var itemNominalType = typeof(T); var itemNominalTypeIsValueType = itemNominalType.IsValueType; var itemNominalTypeSerializer = BsonSerializer.LookupSerializer(itemNominalType); var itemDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(itemNominalType); Type lastItemType = null; IBsonSerializer lastItemSerializer = null; // if itemNominalType is a value type then these assignments are final var itemActualType = itemNominalType; var itemActualTypeSerializer = itemNominalTypeSerializer; bsonReader.ReadStartArray(); var outerList = new List <List <T> >(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var innerList = new List <T>(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { if (!itemNominalTypeIsValueType) { itemActualType = itemDiscriminatorConvention.GetActualType(bsonReader, itemNominalType); if (itemActualType == itemNominalType) { itemActualTypeSerializer = itemNominalTypeSerializer; } else if (itemActualType == lastItemType) { itemActualTypeSerializer = lastItemSerializer; } else { itemActualTypeSerializer = BsonSerializer.LookupSerializer(itemActualType); lastItemType = itemActualType; lastItemSerializer = itemActualTypeSerializer; } } var item = (T)itemActualTypeSerializer.Deserialize(bsonReader, itemNominalType, itemActualType, itemSerializationOptions); innerList.Add(item); } bsonReader.ReadEndArray(); outerList.Add(innerList); } bsonReader.ReadEndArray(); var length1 = outerList.Count; var length2 = (length1 == 0) ? 0 : outerList[0].Count; var array = new T[length1, length2]; for (int i = 0; i < length1; i++) { var innerList = outerList[i]; if (innerList.Count != length2) { message = string.Format("Inner list {0} is of length {1} but should be of length {2}.", i, innerList.Count, length2); throw new FileFormatException(message); } for (int j = 0; j < length2; j++) { array[i, j] = innerList[j]; } } return(array); 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: message = string.Format("Can't deserialize a {0} from BsonType {1}.", actualType.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) { 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 FileFormatException(message); } }
public bool Read() { do { WriteLine(2, "state {0}", _reader.State); switch (_reader.State) { case BsonReaderState.Done: return(false); case BsonReaderState.Initial: case BsonReaderState.Type: _name = null; _value = null; _bsonType = _reader.ReadBsonType(); WriteLine(2, "ReadBsonType type {0}", _bsonType); break; case BsonReaderState.EndOfArray: WriteLine(2, "ReadEndArray"); //_indent -= 2; Indent(-2); //WriteLine(1, "{0}]", "".PadRight(_indent)); WriteLine(1, "{0}]", _indentString); _reader.ReadEndArray(); _type = PBBsonReaderType.EndOfArray; break; case BsonReaderState.EndOfDocument: WriteLine(2, "ReadEndDocument"); //_indent -= 2; Indent(-2); //WriteLine(1, "{0}}}", "".PadRight(_indent)); WriteLine(1, "{0}}}", _indentString); _reader.ReadEndDocument(); _type = PBBsonReaderType.EndOfDocument; break; case BsonReaderState.Name: _name = _reader.ReadName(); WriteLine(2, "ReadName : name {0}", _name); //Trace.Write("{0}{1}: ", "".PadRight(_indent), _name); break; case BsonReaderState.Value: //Write(1, "{0}", "".PadRight(_indent)); Write(1, "{0}", _indentString); if (_name != null) { Write(1, "{0}: ", _name); } ReadValue(); if (_type == PBBsonReaderType.Value) { WriteLine(2, "ReadValue : value {0} ({1})", _value, _value.BsonType); WriteLine(1, "{0} ({1})", _value, _value.BsonType); } break; } } while (_reader.State != BsonReaderState.Type && _reader.State != BsonReaderState.Done); return(true); }
// 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(T[, , ])); var arraySerializationOptions = EnsureSerializationOptions <ArraySerializationOptions>(options); var itemSerializationOptions = arraySerializationOptions.ItemSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: bsonReader.ReadStartArray(); var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(T)); var outerList = new List <List <List <T> > >(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var middleList = new List <List <T> >(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var innerList = new List <T>(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var elementType = discriminatorConvention.GetActualType(bsonReader, typeof(T)); var serializer = BsonSerializer.LookupSerializer(elementType); var element = (T)serializer.Deserialize(bsonReader, typeof(T), elementType, itemSerializationOptions); innerList.Add(element); } bsonReader.ReadEndArray(); middleList.Add(innerList); } bsonReader.ReadEndArray(); outerList.Add(middleList); } bsonReader.ReadEndArray(); var length1 = outerList.Count; var length2 = (length1 == 0) ? 0 : outerList[0].Count; var length3 = (length2 == 0) ? 0 : outerList[0][0].Count; var array = new T[length1, length2, length3]; for (int i = 0; i < length1; i++) { var middleList = outerList[i]; if (middleList.Count != length2) { message = string.Format("Middle list {0} is of length {1} but should be of length {2}.", i, middleList.Count, length2); throw new FileFormatException(message); } for (int j = 0; j < length2; j++) { var innerList = middleList[j]; if (innerList.Count != length3) { message = string.Format("Inner list {0} is of length {1} but should be of length {2}.", j, innerList.Count, length3); throw new FileFormatException(message); } for (int k = 0; k < length3; k++) { array[i, j, k] = innerList[k]; } } } return(array); 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: message = string.Format("Can't deserialize a {0} from BsonType {1}.", actualType.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) { var dictionarySerializationOptions = EnsureSerializationOptions(options); var dictionaryRepresentation = dictionarySerializationOptions.Representation; var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return null; } else if (bsonType == BsonType.Document) { if (nominalType == typeof(object)) { bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, options); // recursive call replacing nominalType with actualType bsonReader.ReadEndDocument(); return value; } var dictionary = CreateInstance(actualType); var valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var key = bsonReader.ReadName(); var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, keyValuePairSerializationOptions.ValueSerializationOptions); dictionary.Add(key, value); } bsonReader.ReadEndDocument(); return dictionary; } else if (bsonType == BsonType.Array) { var dictionary = CreateInstance(actualType); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var keyValuePair = (KeyValuePair<object, object>)_keyValuePairSerializer.Deserialize( bsonReader, typeof(KeyValuePair<object, object>), keyValuePairSerializationOptions); dictionary.Add(keyValuePair.Key, keyValuePair.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 static WebHeaderSerializer Instance { get { return __instance; } } public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (_trace) { pb.Trace.WriteLine("WebHeaderSerializer.Deserialize()"); } var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else if (bsonType == BsonType.Document) { if (nominalType == typeof(object)) { bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, options); // recursive call replacing nominalType with actualType bsonReader.ReadEndDocument(); return(value); } var headers = new WebHeaderCollection(); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { string key = bsonReader.ReadName(); bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.String) { string value = bsonReader.ReadString(); headers.Add(key, value); } else if (bsonType == BsonType.Array) { bsonReader.ReadStartArray(); bsonType = bsonReader.ReadBsonType(); while (bsonType != BsonType.EndOfDocument) { if (bsonType != BsonType.String) { throw new PBException("invalid BsonType {0} for header array \"{1}\" value deserialize WebHeaderCollection.", bsonType, key); } string value = bsonReader.ReadString(); headers.Add(key, value); bsonType = bsonReader.ReadBsonType(); } bsonReader.ReadEndArray(); } else { throw new PBException("error deserialize WebHeaderCollection, invalid BsonType {0} for \"{1}\".", bsonType, key); } } bsonReader.ReadEndDocument(); return(headers); } else { throw new PBException("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType); } }
// 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 bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else if (bsonType == BsonType.Document) { if (nominalType == typeof(object)) { bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, options); // recursive call replacing nominalType with actualType bsonReader.ReadEndDocument(); return(value); } 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) { if (bsonReader.CurrentBsonType == BsonType.Array) { 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); } else if (bsonReader.CurrentBsonType == BsonType.Document) { bsonReader.ReadStartDocument(); object key = null; object value = null; bool keyFound = false, valueFound = false; while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "k": var keyType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var keySerializer = BsonSerializer.LookupSerializer(keyType); key = keySerializer.Deserialize(bsonReader, typeof(object), keyType, null); keyFound = true; break; case "v": var valueType = discriminatorConvention.GetActualType(bsonReader, typeof(object)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, null); valueFound = true; break; default: var message = string.Format("Element '{0}' is not valid for Dictionary items (expecting 'k' or 'v').", name); throw new FileFormatException(message); } } bsonReader.ReadEndDocument(); if (!keyFound) { throw new FileFormatException("Dictionary item was missing the 'k' element."); } if (!valueFound) { throw new FileFormatException("Dictionary item was missing the 'v' element."); } dictionary.Add(key, value); } else { var message = string.Format("Expected document or array for Dictionary item, not {0}.", bsonReader.CurrentBsonType); throw new FileFormatException(message); } } 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 static properties /// <summary> /// Gets an instance of the DictionarySerializer class. /// </summary> //[Obsolete("Use constructor instead.")] //public static DictionarySerializer Instance //{ // get { return __instance; } //} // 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 (_trace) { pb.Trace.WriteLine("NameValueCollectionSerializer.Deserialize()"); } var dictionarySerializationOptions = EnsureSerializationOptions(options); var dictionaryRepresentation = dictionarySerializationOptions.Representation; var keyValuePairSerializationOptions = dictionarySerializationOptions.KeyValuePairSerializationOptions; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else if (bsonType == BsonType.Document) { // dont know why nominalType can be an object if (nominalType == typeof(object)) { bsonReader.ReadStartDocument(); bsonReader.ReadString("_t"); // skip over discriminator bsonReader.ReadName("_v"); var value = Deserialize(bsonReader, actualType, options); // recursive call replacing nominalType with actualType bsonReader.ReadEndDocument(); return(value); } //var dictionary = CreateInstance(actualType); //var valueDiscriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(object)); // { "toto1" : "tata1", "toto2" : "tata2" } NameValueCollection nameValueCollection = new NameValueCollection(); bsonReader.ReadStartDocument(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { //var key = bsonReader.ReadName(); //var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(object)); //var valueSerializer = BsonSerializer.LookupSerializer(valueType); //var value = valueSerializer.Deserialize(bsonReader, typeof(object), valueType, keyValuePairSerializationOptions.ValueSerializationOptions); //dictionary.Add(key, value); var key = bsonReader.ReadName(); var value = bsonReader.ReadString(); nameValueCollection.Add(key, value); } bsonReader.ReadEndDocument(); return(nameValueCollection); } else if (bsonType == BsonType.Array) { //var dictionary = CreateInstance(actualType); // DictionaryRepresentation.ArrayOfArrays // [["toto1", "tata1"], ["toto2", "tata2"]] // DictionaryRepresentation.ArrayOfDocuments // [{ "k" : "toto1", "v" : "tata1" }, { "k" : "toto2", "v" : "tata2" }] NameValueCollection nameValueCollection = new NameValueCollection(); //KeyValuePairSerializer<string, string> keyValuePairSerializer = new KeyValuePairSerializer<string, string>(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { //var keyValuePair = (KeyValuePair<object, object>)_keyValuePairSerializer.Deserialize( // bsonReader, // typeof(KeyValuePair<object, object>), // keyValuePairSerializationOptions); //dictionary.Add(keyValuePair.Key, keyValuePair.Value); var keyValuePair = (KeyValuePair <string, string>)_keyValuePairSerializer.Deserialize(bsonReader, typeof(KeyValuePair <string, string>), keyValuePairSerializationOptions); nameValueCollection.Add(keyValuePair.Key, keyValuePair.Value); } bsonReader.ReadEndArray(); return(nameValueCollection); } else { throw new PBException("Can't deserialize a {0} from BsonType {1}.", nominalType.FullName, bsonType); } }
/// <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 ) { VerifyType(nominalType); var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { bsonReader.ReadStartArray(); var outerList = new List <List <List <T> > >(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var middleList = new List <List <T> >(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var innerList = new List <T>(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var element = BsonSerializer.Deserialize <T>(bsonReader); innerList.Add(element); } bsonReader.ReadEndArray(); middleList.Add(innerList); } bsonReader.ReadEndArray(); outerList.Add(middleList); } bsonReader.ReadEndArray(); var length1 = outerList.Count; var length2 = (length1 == 0) ? 0 : outerList[0].Count; var length3 = (length2 == 0) ? 0 : outerList[0][0].Count; var array = new T[length1, length2, length3]; for (int i = 0; i < length1; i++) { var middleList = outerList[i]; if (middleList.Count != length2) { var message = string.Format("Middle list {0} is of wrong length: {1} (should be: {2})", i, middleList.Count, length2); throw new FileFormatException(message); } for (int j = 0; j < length2; j++) { var innerList = middleList[j]; if (innerList.Count != length3) { var message = string.Format("Inner list {0} is of wrong length: {1} (should be: {2})", j, innerList.Count, length3); throw new FileFormatException(message); } for (int k = 0; k < length3; k++) { array[i, j, k] = innerList[k]; } } } return(array); } }
/// <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(T[, ])); var bsonType = bsonReader.CurrentBsonType; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: bsonReader.ReadStartArray(); var outerList = new List <List <T> >(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); var innerList = new List <T>(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var element = BsonSerializer.Deserialize <T>(bsonReader); innerList.Add(element); } bsonReader.ReadEndArray(); outerList.Add(innerList); } bsonReader.ReadEndArray(); var length1 = outerList.Count; var length2 = (length1 == 0) ? 0 : outerList[0].Count; var array = new T[length1, length2]; for (int i = 0; i < length1; i++) { var innerList = outerList[i]; if (innerList.Count != length2) { message = string.Format("Inner list {0} is of length {1} but should be of length {2}.", i, innerList.Count, length2); throw new FileFormatException(message); } for (int j = 0; j < length2; j++) { array[i, j] = innerList[j]; } } return(array); 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: message = string.Format("Can't deserialize a {0} from BsonType {1}.", actualType.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(KeyValuePair <TKey, TValue>)); var keyValuePairSerializationOptions = EnsureSerializationOptions <KeyValuePairSerializationOptions>(options); var keyDiscriminatorConvention = GetKeyDiscriminatorConvention(); var valueDiscriminatorConvention = GetValueDiscriminatorConvention(); TKey key; TValue value; var bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.Array) { bsonReader.ReadStartArray(); bsonReader.ReadBsonType(); var keyType = keyDiscriminatorConvention.GetActualType(bsonReader, typeof(TKey)); var keySerializer = GetKeySerializer(keyType); key = (TKey)keySerializer.Deserialize(bsonReader, typeof(TKey), keyType, keyValuePairSerializationOptions.KeySerializationOptions); bsonReader.ReadBsonType(); var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue)); var valueSerializer = GetValueSerializer(valueType); value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), valueType, keyValuePairSerializationOptions.ValueSerializationOptions); bsonReader.ReadEndArray(); } else if (bsonType == BsonType.Document) { bsonReader.ReadStartDocument(); key = default(TKey); value = default(TValue); bool keyFound = false, valueFound = false; bool elementFound; bool elementIsKey; while (bsonReader.ReadBsonType(__bsonTrie, out elementFound, out elementIsKey) != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); if (elementFound) { if (elementIsKey) { var keyType = keyDiscriminatorConvention.GetActualType(bsonReader, typeof(TKey)); var keySerializer = GetValueSerializer(keyType); key = (TKey)keySerializer.Deserialize(bsonReader, typeof(TKey), keyType, keyValuePairSerializationOptions.KeySerializationOptions); keyFound = true; } else { var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue)); var valueSerializer = GetValueSerializer(valueType); value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), valueType, keyValuePairSerializationOptions.ValueSerializationOptions); valueFound = true; } } else { var message = string.Format("Element '{0}' is not valid for KeyValuePairs (expecting 'k' or 'v').", name); throw new BsonSerializationException(message); } } bsonReader.ReadEndDocument(); if (!keyFound) { throw new FileFormatException("KeyValuePair item was missing the 'k' element."); } if (!valueFound) { throw new FileFormatException("KeyValuePair item was missing the 'v' element."); } } else { var message = string.Format( "Cannot deserialize '{0}' from BsonType {1}.", BsonUtils.GetFriendlyTypeName(typeof(KeyValuePair <TKey, TValue>)), bsonType); throw new FileFormatException(message); } return(new KeyValuePair <TKey, TValue>(key, 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) { 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 stack = new Stack(); 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); stack.Push(element); } bsonReader.ReadEndArray(); return stack; 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); } }