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)(new StringSerializer().Deserialize(bsonReader, typeof(string), options)); var val = (string)(new StringSerializer().Deserialize(bsonReader, typeof(string), options)); bsonReader.ReadEndArray(); nvc.Add(key, val); } bsonReader.ReadEndArray(); return(nvc); }
/// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, // ignored IBsonSerializationOptions options ) { var bsonType = bsonReader.CurrentBsonType; if (bsonType == BsonType.Null) { bsonReader.ReadNull(); return(null); } else if (bsonType == BsonType.Document) { var dictionary = CreateInstance(nominalType); bsonReader.ReadStartDocument(); var valueDiscriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(TValue)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var key = (TKey)(object)bsonReader.ReadName(); var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), valueType, null); dictionary.Add(key, value); } bsonReader.ReadEndDocument(); return(dictionary); } else if (bsonType == BsonType.Array) { var dictionary = CreateInstance(nominalType); bsonReader.ReadStartArray(); var keyDiscriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(TKey)); var valueDiscriminatorConvention = BsonDefaultSerializer.LookupDiscriminatorConvention(typeof(TValue)); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { bsonReader.ReadStartArray(); bsonReader.ReadBsonType(); var keyType = keyDiscriminatorConvention.GetActualType(bsonReader, typeof(TKey)); var keySerializer = BsonSerializer.LookupSerializer(keyType); var key = (TKey)keySerializer.Deserialize(bsonReader, typeof(TKey), keyType, null); bsonReader.ReadBsonType(); var valueType = valueDiscriminatorConvention.GetActualType(bsonReader, typeof(TValue)); var valueSerializer = BsonSerializer.LookupSerializer(valueType); var value = (TValue)valueSerializer.Deserialize(bsonReader, typeof(TValue), 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); } }
/// <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 <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) { var message = string.Format("Inner list {0} is of wrong length: {1} (should be: {2})", i, innerList.Count, length2); throw new FileFormatException(message); } for (int j = 0; j < length2; j++) { array[i, j] = innerList[j]; } } return(array); } }
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 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.Array) { bsonReader.ReadStartArray(); var list = (nominalType == typeof(List <T>) || nominalType.IsInterface) ? new List <T>() : (ICollection <T>)Activator.CreateInstance(nominalType); var discriminatorConvention = BsonDefaultSerializer.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, 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); } }
/// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, // ignored IBsonSerializationOptions options ) { 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 override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(DateTimeOffset)); BsonType bsonType = bsonReader.GetCurrentBsonType(); long ticks; TimeSpan offset; switch (bsonType) { case BsonType.Array: bsonReader.ReadStartArray(); ticks = bsonReader.ReadInt64(); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32()); bsonReader.ReadEndArray(); return(new DateTimeOffset(ticks, offset).Add(offset)); case BsonType.Document: bsonReader.ReadStartDocument(); bsonReader.ReadDateTime("DateTime"); ticks = bsonReader.ReadInt64("Ticks"); offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset")); bsonReader.ReadEndDocument(); return(new DateTimeOffset(ticks, offset).Add(offset)); default: var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType); throw new ArgumentException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(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); } }
/// <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(); List <T> list = new List <T>(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var element = BsonSerializer.Deserialize <T>(bsonReader); list.Add(element); } bsonReader.ReadEndArray(); return(list.ToArray()); } }
// 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: 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 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); } }
/// <summary> /// Reads a BsonArray from a BsonReader. /// </summary> /// <param name="bsonReader">The reader.</param> /// <returns>A BsonArray.</returns> public static new BsonArray ReadFrom(BsonReader bsonReader) { var array = new BsonArray(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var value = BsonValue.ReadFrom(bsonReader); array.Add(value); } bsonReader.ReadEndArray(); return(array); }
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()); }
static IList ReadArray(BsonReader bsonReader) { var array = new ArrayList(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { array.Add(ReadObject(bsonReader)); } bsonReader.ReadEndArray(); return(array); }
// private methods private IEnumerable <TValue> ReadValues(BsonReader bsonReader) { var values = new List <TValue>(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { values.Add((TValue)_valueSerializer.Deserialize(bsonReader, typeof(TValue), _valueSerializationOptions)); } bsonReader.ReadEndArray(); return(values); }
// private methods private List <GeoJsonFeature <TCoordinates> > DeserializeFeatures(BsonReader bsonReader) { var features = new List <GeoJsonFeature <TCoordinates> >(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var feature = (GeoJsonFeature <TCoordinates>)_featureSerializer.Deserialize(bsonReader, typeof(GeoJsonFeature <TCoordinates>), null); features.Add(feature); } bsonReader.ReadEndArray(); return(features); }
// private methods private List <GeoJsonGeometry <TCoordinates> > DeserializeGeometries(BsonReader bsonReader) { var geometries = new List <GeoJsonGeometry <TCoordinates> >(); bsonReader.ReadStartArray(); while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var geometry = (GeoJsonGeometry <TCoordinates>)_geometrySerializer.Deserialize(bsonReader, typeof(GeoJsonGeometry <TCoordinates>), null); geometries.Add(geometry); } bsonReader.ReadEndArray(); return(geometries); }
public void TestArrayOneElement() { var json = "[1]"; 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.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) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { bsonReader.ReadStartArray(); var easting = bsonReader.ReadDouble(); var northing = bsonReader.ReadDouble(); bsonReader.ReadEndArray(); return(new GeoJson2DProjectedCoordinates(easting, northing)); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns> /// An object. /// </returns> public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { bsonReader.ReadStartArray(); var longitude = bsonReader.ReadDouble(); var latitude = bsonReader.ReadDouble(); bsonReader.ReadEndArray(); return(new GeoJson2DGeographicCoordinates(longitude, latitude)); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns> /// An object. /// </returns> public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { if (bsonReader.GetCurrentBsonType() == BsonType.Null) { bsonReader.ReadNull(); return(null); } else { bsonReader.ReadStartArray(); var x = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null); var y = (double)__doubleSerializer.Deserialize(bsonReader, typeof(double), null); bsonReader.ReadEndArray(); return(new GeoJson2DCoordinates(x, y)); } }
// 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(); switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Array: bsonReader.ReadStartArray(); var discriminatorConvention = BsonSerializer.LookupDiscriminatorConvention(typeof(T)); var list = 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); 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 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 void TestArrayTwoElements() { var json = "[1, 2]"; 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.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>(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 <T>(); var discriminatorConvention = BsonDefaultSerializer.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, 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); } }
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 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 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 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) { 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); } }
// 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 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 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); } }