// 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) { VerifyTypes(nominalType, actualType, typeof(decimal)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null); var bits = new int[4]; bits[0] = array[0].AsInt32; bits[1] = array[1].AsInt32; bits[2] = array[2].AsInt32; bits[3] = array[3].AsInt32; return new decimal(bits); case BsonType.Double: return representationSerializationOptions.ToDecimal(bsonReader.ReadDouble()); case BsonType.Int32: return representationSerializationOptions.ToDecimal(bsonReader.ReadInt32()); case BsonType.Int64: return representationSerializationOptions.ToDecimal(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDecimal(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Decimal from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(double)); var representationSerializationOptions = EnsureSerializationOptions<RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return bsonReader.ReadDouble(); case BsonType.Int32: return representationSerializationOptions.ToDouble(bsonReader.ReadInt32()); case BsonType.Int64: return representationSerializationOptions.ToDouble(bsonReader.ReadInt64()); case BsonType.String: return XmlConvert.ToDouble(bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize Double from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(bool)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Boolean: return bsonReader.ReadBoolean(); case BsonType.Double: return bsonReader.ReadDouble() != 0.0; case BsonType.Int32: return bsonReader.ReadInt32() != 0; case BsonType.Int64: return bsonReader.ReadInt64() != 0; case BsonType.Null: bsonReader.ReadNull(); return false; case BsonType.String: return XmlConvert.ToBoolean(bsonReader.ReadString().ToLower()); default: var message = string.Format("Cannot deserialize Boolean from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object of type System.Drawing.Size from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(System.Drawing.Size)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Document: bsonReader.ReadStartDocument(); var width = bsonReader.ReadInt32("Width"); var height = bsonReader.ReadInt32("Height"); bsonReader.ReadEndDocument(); return new System.Drawing.Size(width, height); default: var message = string.Format("Cannot deserialize Size from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// 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(sbyte)); var bsonType = bsonReader.GetCurrentBsonType(); var lostData = false; sbyte value; switch (bsonType) { case BsonType.Binary: var bytes = bsonReader.ReadBytes(); if (bytes.Length != 1) { throw new FileFormatException("Binary data for SByte must be exactly one byte long."); } value = (sbyte)bytes[0]; break; case BsonType.Int32: var int32Value = bsonReader.ReadInt32(); value = (sbyte)int32Value; lostData = (int)value != int32Value; break; case BsonType.Int64: var int64Value = bsonReader.ReadInt64(); value = (sbyte)int64Value; lostData = (int)value != int64Value; break; case BsonType.String: var s = bsonReader.ReadString(); if (s.Length == 1) { s = "0" + s; } value = (sbyte)byte.Parse(s, NumberStyles.HexNumber); break; default: var message = string.Format("Cannot deserialize SByte from BsonType {0}.", bsonType); throw new FileFormatException(message); } if (lostData) { var message = string.Format("Data loss occurred when trying to convert from {0} to SByte.", bsonType); throw new FileFormatException(message); } return value; }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BitArray)); BsonType bsonType = bsonReader.GetCurrentBsonType(); BitArray bitArray; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Binary: return new BitArray(bsonReader.ReadBytes()); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); var bytes = bsonReader.ReadBytes("Bytes"); bsonReader.ReadEndDocument(); bitArray = new BitArray(bytes); bitArray.Length = length; return bitArray; case BsonType.String: var s = bsonReader.ReadString(); bitArray = new BitArray(s.Length); for (int i = 0; i < s.Length; i++) { var c = s[i]; switch (c) { case '0': break; case '1': bitArray[i] = true; break; default: throw new FileFormatException("String value is not a valid BitArray."); } } return bitArray; default: var message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
public void TestDocumentOneElement() { var json = "{ \"x\" : 1 }"; using (_bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.Document, _bsonReader.ReadBsonType()); _bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.AreEqual("x", _bsonReader.ReadName()); Assert.AreEqual(1, _bsonReader.ReadInt32()); Assert.AreEqual(BsonType.EndOfDocument, _bsonReader.ReadBsonType()); _bsonReader.ReadEndDocument(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonDocument>(new StringReader(json)).ToJson()); }
public void TestJavaScriptWithScope() { string json = "{ \"$code\" : \"function f() { return n; }\", \"$scope\" : { \"n\" : 1 } }"; using (_bsonReader = BsonReader.Create(json)) { Assert.AreEqual(BsonType.JavaScriptWithScope, _bsonReader.ReadBsonType()); Assert.AreEqual("function f() { return n; }", _bsonReader.ReadJavaScriptWithScope()); _bsonReader.ReadStartDocument(); Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType()); Assert.AreEqual("n", _bsonReader.ReadName()); Assert.AreEqual(1, _bsonReader.ReadInt32()); _bsonReader.ReadEndDocument(); Assert.AreEqual(BsonReaderState.Done, _bsonReader.State); } Assert.AreEqual(json, BsonSerializer.Deserialize <BsonJavaScriptWithScope>(new StringReader(json)).ToJson()); }
public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { object value = null; var valueType = actualType.GetConceptValueType(); if (valueType == typeof(Guid)) { var binaryData = bsonReader.ReadBinaryData(); value = binaryData.ToGuid(); } else if (valueType == typeof(double)) { value = bsonReader.ReadDouble(); } else if (valueType == typeof(float)) { value = (float)bsonReader.ReadDouble(); } else if (valueType == typeof(Int32)) { value = bsonReader.ReadInt32(); } else if (valueType == typeof(Int64)) { value = bsonReader.ReadInt64(); } else if (valueType == typeof(bool)) { value = bsonReader.ReadBoolean(); } else if (valueType == typeof(string)) { value = bsonReader.ReadString(); } else if (valueType == typeof(decimal)) { value = decimal.Parse(bsonReader.ReadString()); } var concept = ConceptFactory.CreateConceptInstance(actualType, value); return(concept); }
// public 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(BsonInt32)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return(new BsonInt32(bsonReader.ReadInt32())); default: var message = string.Format("Cannot deserialize BsonInt32 from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(char)); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return (char)bsonReader.ReadInt32(); case BsonType.String: return (char)bsonReader.ReadString()[0]; default: var message = string.Format("Cannot deserialize Char 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, // ignored IBsonSerializationOptions options) { VerifyDeserializeType(nominalType); var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Int32: return(Enum.ToObject(nominalType, bsonReader.ReadInt32())); case BsonType.Int64: return(Enum.ToObject(nominalType, bsonReader.ReadInt64())); case BsonType.Double: return(Enum.ToObject(nominalType, (long)bsonReader.ReadDouble())); case BsonType.String: return(Enum.Parse(nominalType, bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
public 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 override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.String: return(bsonReader.ReadString()); case BsonType.Int32: return(bsonReader.ReadInt32().ToString(CultureInfo.InvariantCulture)); default: var message = string.Format("Cannot deserialize BsonString or BsonInt32 from BsonType {0}.", bsonType); throw new BsonSerializationException(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 (_trace) { pb.Trace.WriteLine("ZIntSerializer.Deserialize()"); } VerifyTypes(nominalType, actualType, typeof(ZInt)); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return(new ZInt(bsonReader.ReadInt32())); default: //var message = string.Format("Cannot deserialize BsonInt32 from BsonType {0}.", bsonType); //throw new FileFormatException(message); throw new PBException("error cannot deserialize ZInt 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) { VerifyTypes(nominalType, actualType, typeof(decimal)); var representationSerializationOptions = EnsureSerializationOptions <RepresentationSerializationOptions>(options); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Array: var array = (BsonArray)BsonArraySerializer.Instance.Deserialize(bsonReader, typeof(BsonArray), null); var bits = new int[4]; bits[0] = array[0].AsInt32; bits[1] = array[1].AsInt32; bits[2] = array[2].AsInt32; bits[3] = array[3].AsInt32; return(new decimal(bits)); case BsonType.Double: return(representationSerializationOptions.ToDecimal(bsonReader.ReadDouble())); case BsonType.Int32: return(representationSerializationOptions.ToDecimal(bsonReader.ReadInt32())); case BsonType.Int64: return(representationSerializationOptions.ToDecimal(bsonReader.ReadInt64())); case BsonType.String: return(XmlConvert.ToDecimal(bsonReader.ReadString())); default: var message = string.Format("Cannot deserialize Decimal from BsonType {0}.", bsonType); throw new 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(TimeSpan)); // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Double: return(FromDouble(bsonReader.ReadDouble(), timeSpanSerializationOptions.Units)); case BsonType.Int32: return(FromInt32(bsonReader.ReadInt32(), timeSpanSerializationOptions.Units)); case BsonType.Int64: return(FromInt64(bsonReader.ReadInt64(), timeSpanSerializationOptions.Units)); case BsonType.String: return(TimeSpan.Parse(bsonReader.ReadString())); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan) default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new 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(char)); BsonType bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return((char)bsonReader.ReadInt32()); case BsonType.String: return((char)bsonReader.ReadString()[0]); default: var message = string.Format("Cannot deserialize Char from BsonType {0}.", bsonType); throw new Exception(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(bool)); var bsonType = bsonReader.CurrentBsonType; switch (bsonType) { case BsonType.Boolean: return(bsonReader.ReadBoolean()); case BsonType.Double: return(bsonReader.ReadDouble() != 0.0); case BsonType.Int32: return(bsonReader.ReadInt32() != 0); case BsonType.Int64: return(bsonReader.ReadInt64() != 0); case BsonType.Null: bsonReader.ReadNull(); return(false); case BsonType.String: return(XmlConvert.ToBoolean(bsonReader.ReadString().ToLower())); default: var message = string.Format("Cannot deserialize Boolean from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(BitArray)); BsonType bsonType = bsonReader.GetCurrentBsonType(); BitArray bitArray; byte[] bytes; BsonBinarySubType subType; string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Binary: bsonReader.ReadBinaryData(out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { message = string.Format("Invalid Binary sub type {0}.", subType); throw new FileFormatException(message); } return(new BitArray(bytes)); case BsonType.Document: bsonReader.ReadStartDocument(); var length = bsonReader.ReadInt32("Length"); bsonReader.ReadBinaryData("Bytes", out bytes, out subType); if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) { message = string.Format("Invalid Binary sub type {0}.", subType); throw new FileFormatException(message); } bsonReader.ReadEndDocument(); bitArray = new BitArray(bytes); bitArray.Length = length; return(bitArray); case BsonType.String: var s = bsonReader.ReadString(); bitArray = new BitArray(s.Length); for (int i = 0; i < s.Length; i++) { var c = s[i]; switch (c) { case '0': break; case '1': bitArray[i] = true; break; default: throw new FileFormatException("String value is not a valid BitArray."); } } return(bitArray); default: message = string.Format("Cannot deserialize Byte[] from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(TimeSpan)); // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions<TimeSpanSerializationOptions>(options); BsonType bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.String) { return TimeSpan.Parse(bsonReader.ReadString()); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan) } else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks) { long ticks; switch (bsonType) { case BsonType.Double: ticks = (long)bsonReader.ReadDouble(); break; case BsonType.Int32: ticks = (long)bsonReader.ReadInt32(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } return new TimeSpan(ticks); } else { double interval; switch (bsonType) { case BsonType.Double: interval = bsonReader.ReadDouble(); break; case BsonType.Int32: interval = bsonReader.ReadInt32(); break; case BsonType.Int64: interval = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } switch (timeSpanSerializationOptions.Units) { case TimeSpanUnits.Days: return TimeSpan.FromDays(interval); case TimeSpanUnits.Hours: return TimeSpan.FromHours(interval); case TimeSpanUnits.Minutes: return TimeSpan.FromMinutes(interval); case TimeSpanUnits.Seconds: return TimeSpan.FromSeconds(interval); case TimeSpanUnits.Milliseconds: return TimeSpan.FromMilliseconds(interval); case TimeSpanUnits.Nanoseconds: return TimeSpan.FromMilliseconds(interval / 1000.0); default: var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units); throw new BsonSerializationException(message); } } }
public 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 methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, // ignored IBsonSerializationOptions options) { VerifyDeserializeType(nominalType); var bsonType = bsonReader.GetCurrentBsonType(); switch (bsonType) { case BsonType.Int32: return Enum.ToObject(nominalType, bsonReader.ReadInt32()); case BsonType.Int64: return Enum.ToObject(nominalType, bsonReader.ReadInt64()); case BsonType.Double: return Enum.ToObject(nominalType, (long)bsonReader.ReadDouble()); case BsonType.String: return Enum.Parse(nominalType, bsonReader.ReadString()); default: var message = string.Format("Cannot deserialize {0} from BsonType {1}.", nominalType.FullName, bsonType); throw new FileFormatException(message); } }
// public methods /// <summary> /// Deserializes an object from a BsonReader. /// </summary> /// <param name="bsonReader">The BsonReader.</param> /// <param name="nominalType">The nominal type of the object.</param> /// <param name="actualType">The actual type of the object.</param> /// <param name="options">The serialization options.</param> /// <returns>An object.</returns> public override object Deserialize( BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options) { VerifyTypes(nominalType, actualType, typeof(Version)); BsonType bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return(null); case BsonType.Document: bsonReader.ReadStartDocument(); int major = -1, minor = -1, build = -1, revision = -1; while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "Major": major = bsonReader.ReadInt32(); break; case "Minor": minor = bsonReader.ReadInt32(); break; case "Build": build = bsonReader.ReadInt32(); break; case "Revision": revision = bsonReader.ReadInt32(); break; default: message = string.Format("Unrecognized element '{0}' while deserializing a Version value.", name); throw new FileFormatException(message); } } bsonReader.ReadEndDocument(); if (major == -1) { message = string.Format("Version missing Major element."); throw new FileFormatException(message); } else if (minor == -1) { message = string.Format("Version missing Minor element."); throw new FileFormatException(message); } else if (build == -1) { return(new Version(major, minor)); } else if (revision == -1) { return(new Version(major, minor, build)); } else { return(new Version(major, minor, build, revision)); } case BsonType.String: return(new Version(bsonReader.ReadString())); default: message = string.Format("Cannot deserialize Version from BsonType {0}.", bsonType); throw new FileFormatException(message); } }
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); }
private static OXmlPageMargin ReadPageMargin(BsonReader bsonReader) { bsonReader.ReadStartDocument(); OXmlPageMargin value = new OXmlPageMargin(); while (true) { BsonType bsonType = bsonReader.ReadBsonType(); if (bsonType == BsonType.EndOfDocument) { break; } string name = bsonReader.ReadName(); switch (name.ToLower()) { case "top": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.Int32) { throw new PBException($"wrong PageMargin Top value {bsonType}"); } value.Top = bsonReader.ReadInt32(); break; case "bottom": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.Int32) { throw new PBException($"wrong PageMargin Bottom value {bsonType}"); } value.Bottom = bsonReader.ReadInt32(); break; case "left": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.Int32) { throw new PBException($"wrong PageMargin Left value {bsonType}"); } value.Left = bsonReader.ReadInt32(); break; case "right": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.Int32) { throw new PBException($"wrong PageMargin Right value {bsonType}"); } value.Right = bsonReader.ReadInt32(); break; case "header": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.Int32) { throw new PBException($"wrong PageMargin Header value {bsonType}"); } value.Header = bsonReader.ReadInt32(); break; case "footer": if (bsonType == BsonType.Null) { break; } if (bsonType != BsonType.Int32) { throw new PBException($"wrong PageMargin Footer value {bsonType}"); } value.Footer = bsonReader.ReadInt32(); break; default: throw new PBException($"unknow PageMargin value \"{name}\""); } } bsonReader.ReadEndDocument(); return(value); }
private void ReadValue() { //bool value = true; _type = PBBsonReaderType.Value; switch (_bsonType) { case BsonType.Document: WriteLine(2, "ReadStartDocument"); WriteLine(1, "{"); //_indent += 2; Indent(2); _reader.ReadStartDocument(); _type = PBBsonReaderType.Document; //value = false; break; case BsonType.Array: WriteLine(2, "ReadStartArray"); WriteLine(1, "["); //_indent += 2; Indent(2); _reader.ReadStartArray(); _type = PBBsonReaderType.Array; //value = false; break; case BsonType.Binary: _value = BsonValue.Create(_reader.ReadBytes()); break; case BsonType.Boolean: _value = BsonValue.Create(_reader.ReadBoolean()); break; case BsonType.DateTime: _value = BsonValue.Create(_reader.ReadDateTime()); break; case BsonType.Double: _value = BsonValue.Create(_reader.ReadDouble()); break; case BsonType.Int32: _value = BsonValue.Create(_reader.ReadInt32()); break; case BsonType.Int64: _value = BsonValue.Create(_reader.ReadInt64()); break; case BsonType.JavaScript: _value = BsonValue.Create(_reader.ReadJavaScript()); break; case BsonType.JavaScriptWithScope: _value = BsonValue.Create(_reader.ReadJavaScriptWithScope()); break; case BsonType.MaxKey: _reader.ReadMaxKey(); _value = BsonMaxKey.Value; break; case BsonType.MinKey: _reader.ReadMinKey(); _value = BsonMinKey.Value; break; case BsonType.Null: _reader.ReadNull(); _value = BsonNull.Value; break; case BsonType.ObjectId: _value = BsonValue.Create(_reader.ReadObjectId()); break; case BsonType.RegularExpression: _value = BsonValue.Create(_reader.ReadRegularExpression()); break; case BsonType.String: _value = BsonValue.Create(_reader.ReadString()); break; case BsonType.Symbol: _value = BsonValue.Create(_reader.ReadSymbol()); break; case BsonType.Timestamp: _value = BsonValue.Create(_reader.ReadTimestamp()); break; case BsonType.Undefined: _reader.ReadUndefined(); _value = BsonUndefined.Value; break; default: throw new PBException("error unable to read value type {0}", _bsonType); } //return 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) { VerifyTypes(nominalType, actualType, typeof(TimeSpan)); // support RepresentationSerializationOptions for backward compatibility var representationSerializationOptions = options as RepresentationSerializationOptions; if (representationSerializationOptions != null) { options = new TimeSpanSerializationOptions(representationSerializationOptions.Representation); } var timeSpanSerializationOptions = EnsureSerializationOptions <TimeSpanSerializationOptions>(options); BsonType bsonType = bsonReader.GetCurrentBsonType(); if (bsonType == BsonType.String) { return(TimeSpan.Parse(bsonReader.ReadString())); // not XmlConvert.ToTimeSpan (we're using .NET's format for TimeSpan) } else if (timeSpanSerializationOptions.Units == TimeSpanUnits.Ticks) { long ticks; switch (bsonType) { case BsonType.Double: ticks = (long)bsonReader.ReadDouble(); break; case BsonType.Int32: ticks = (long)bsonReader.ReadInt32(); break; case BsonType.Int64: ticks = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } return(new TimeSpan(ticks)); } else { double interval; switch (bsonType) { case BsonType.Double: interval = bsonReader.ReadDouble(); break; case BsonType.Int32: interval = bsonReader.ReadInt32(); break; case BsonType.Int64: interval = bsonReader.ReadInt64(); break; default: var message = string.Format("Cannot deserialize TimeSpan from BsonType {0}.", bsonType); throw new FileFormatException(message); } switch (timeSpanSerializationOptions.Units) { case TimeSpanUnits.Days: return(TimeSpan.FromDays(interval)); case TimeSpanUnits.Hours: return(TimeSpan.FromHours(interval)); case TimeSpanUnits.Minutes: return(TimeSpan.FromMinutes(interval)); case TimeSpanUnits.Seconds: return(TimeSpan.FromSeconds(interval)); case TimeSpanUnits.Milliseconds: return(TimeSpan.FromMilliseconds(interval)); case TimeSpanUnits.Nanoseconds: return(TimeSpan.FromMilliseconds(interval / 1000.0)); default: var message = string.Format("'{0}' is not a valid TimeSpanUnits value.", timeSpanSerializationOptions.Units); throw new BsonSerializationException(message); } } }
// public 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(Version)); BsonType bsonType = bsonReader.GetCurrentBsonType(); string message; switch (bsonType) { case BsonType.Null: bsonReader.ReadNull(); return null; case BsonType.Document: bsonReader.ReadStartDocument(); int major = -1, minor = -1, build = -1, revision = -1; while (bsonReader.ReadBsonType() != BsonType.EndOfDocument) { var name = bsonReader.ReadName(); switch (name) { case "Major": major = bsonReader.ReadInt32(); break; case "Minor": minor = bsonReader.ReadInt32(); break; case "Build": build = bsonReader.ReadInt32(); break; case "Revision": revision = bsonReader.ReadInt32(); break; default: message = string.Format("Unrecognized element '{0}' while deserializing a Version value.", name); throw new FileFormatException(message); } } bsonReader.ReadEndDocument(); if (major == -1) { message = string.Format("Version missing Major element."); throw new FileFormatException(message); } else if (minor == -1) { message = string.Format("Version missing Minor element."); throw new FileFormatException(message); } else if (build == -1) { return new Version(major, minor); } else if (revision == -1) { return new Version(major, minor, build); } else { return new Version(major, minor, build, revision); } case BsonType.String: return new Version(bsonReader.ReadString()); default: message = string.Format("Cannot deserialize Version from BsonType {0}.", bsonType); throw new FileFormatException(message); } }