// 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(DateTimeOffset));

            BsonType bsonType = bsonReader.GetCurrentBsonType();
            long ticks;
            TimeSpan offset;
            switch (bsonType)
            {
                case BsonType.Array:
                    bsonReader.ReadStartArray();
                    ticks = bsonReader.ReadInt64();
                    offset = TimeSpan.FromMinutes(bsonReader.ReadInt32());
                    bsonReader.ReadEndArray();
                    return new DateTimeOffset(ticks, offset);
                case BsonType.Document:
                    bsonReader.ReadStartDocument();
                    bsonReader.ReadDateTime("DateTime"); // ignore value
                    ticks = bsonReader.ReadInt64("Ticks");
                    offset = TimeSpan.FromMinutes(bsonReader.ReadInt32("Offset"));
                    bsonReader.ReadEndDocument();
                    return new DateTimeOffset(ticks, offset);
                case BsonType.String:
                    return XmlConvert.ToDateTimeOffset(bsonReader.ReadString());
                default:
                    var message = string.Format("Cannot deserialize DateTimeOffset from BsonType {0}.", bsonType);
                    throw new Exception(message);
            }
        }
 public void TestArrayTwoElements() {
     var json = "[1, 2]";
     using (bsonReader = BsonReader.Create(json)) {
         Assert.AreEqual(BsonType.Array, bsonReader.ReadBsonType());
         bsonReader.ReadStartArray();
         Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
         Assert.AreEqual(1, bsonReader.ReadInt32());
         Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
         Assert.AreEqual(2, bsonReader.ReadInt32());
         Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType());
         bsonReader.ReadEndArray();
         Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
     }
     Assert.AreEqual(json, BsonSerializer.Deserialize<BsonArray>(new StringReader(json)).ToJson());
 }
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        public override object Deserialize(
            BsonReader bsonReader,
            Type nominalType,
            Type actualType,
            IBsonSerializationOptions options)
        {
            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 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);
            }
        }
 private static OXmlPageSize ReadPageSize(BsonReader bsonReader)
 {
     bsonReader.ReadStartDocument();
     OXmlPageSize value = new OXmlPageSize();
     while (true)
     {
         BsonType bsonType = bsonReader.ReadBsonType();
         if (bsonType == BsonType.EndOfDocument)
             break;
         string name = bsonReader.ReadName();
         switch (name.ToLower())
         {
             case "width":
                 if (bsonType == BsonType.Null)
                     break;
                 if (bsonType != BsonType.Int32)
                     throw new PBException($"wrong PageSize width value {bsonType}");
                 value.Width = bsonReader.ReadInt32();
                 break;
             case "height":
                 if (bsonType == BsonType.Null)
                     break;
                 if (bsonType != BsonType.Int32)
                     throw new PBException($"wrong PageSize height value {bsonType}");
                 value.Height = bsonReader.ReadInt32();
                 break;
             default:
                 throw new PBException($"unknow PageSize value \"{name}\"");
         }
     }
     bsonReader.ReadEndDocument();
     return value;
 }
        // public 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 FileFormatException(message);
            }
        }
		public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
		{
			var id = bsonReader.ReadInt32();
			if (id == 0)
				return null;
			return database.IdentityMap.GetOrCreate(id, (i) => database.GetCollection<ContentItem>().FindOne(Query.EQ("_id", i)));
		}
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (bsonReader.CurrentBsonType == BsonType.Int32)
                return bsonReader.ReadInt32().ToString();

            throw new InvalidOperationException();
        }
예제 #10
0
        public object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            object value = null;
            var valueType = actualType.GetConceptValueType();
			if (valueType == typeof(Guid)) {
				var guidBytes = new byte[16];
				BsonBinarySubType subType;
				bsonReader.ReadBinaryData (out guidBytes, out subType);
				value = new Guid (guidBytes);
			} 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;
        }
예제 #11
0
		public override object Deserialize(BsonReader bsonReader, Type nominalType, IBsonSerializationOptions options)
		{
			var id = bsonReader.ReadInt32();
			if (id == 0)
				return ContentRelation.Empty;
			
			return new ContentRelation(id, Get);
		}
 static object Deserialize(BsonReader bsonReader)
 {
     var currentBsonType = bsonReader.GetCurrentBsonType();
     switch (currentBsonType)
     {
         case BsonType.Null:
             return null;
         case BsonType.Document:
             bsonReader.ReadStartDocument();
             var hour = bsonReader.ReadInt32("Hour");
             var minute = bsonReader.ReadInt32("Minute");
             var second = bsonReader.ReadInt32("Second");
             bsonReader.ReadEndDocument();
             return new TimeOfDay(hour, minute, second);
         default:
             throw new NotSupportedException(
                 string.Format("Bson type : {0} is not supported for TimeOfDay type property", currentBsonType));
     }
 }
 #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
 public override object Deserialize(
     BsonReader bsonReader,
     Type nominalType,
     IBsonSerializationOptions options
 ) {
     BsonType bsonType = bsonReader.CurrentBsonType;
     BitArray bitArray;
     byte[] bytes;
     BsonBinarySubType subType;
     string message;
     switch (bsonType) {
         case BsonType.Null:
             bsonReader.ReadNull();
             return null;
         case BsonType.Binary:
             bsonReader.ReadBinaryData(out bytes, out subType);
             if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
                 message = string.Format("Invalid Binary sub type: {0}", subType);
                 throw new FileFormatException(message);
             }
             return new BitArray(bytes);
         case BsonType.Document:
             bsonReader.ReadStartDocument();
             var length = bsonReader.ReadInt32("Length");
             bsonReader.ReadBinaryData("Bytes", out bytes, out subType);
             if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
                 message = string.Format("Invalid Binary sub type: {0}", subType);
                 throw new FileFormatException(message);
             }
             bsonReader.ReadEndDocument();
             bitArray = new BitArray(bytes);
             bitArray.Length = length;
             return bitArray;
         case BsonType.String:
             var s = bsonReader.ReadString();
             bitArray = new BitArray(s.Length);
             for (int i = 0; i < s.Length; i++) {
                 var c = s[i];
                 switch (c) {
                     case '0':
                         break;
                     case '1':
                         bitArray[i] = true;
                         break;
                     default:
                         throw new FileFormatException("String value is not a valid BitArray");
                 }
             }
             return bitArray;
         default:
             message = string.Format("Cannot deserialize Byte[] from BsonType: {0}", bsonType);
             throw new FileFormatException(message);
     }
 }
예제 #14
0
        public override object Deserialize(BsonReader bsonReader,
            Type nominalType, Type actualType, IBsonSerializationOptions options) {
            if (nominalType != typeof(Category) || actualType != typeof(Category)) {
                throw new BsonSerializationException("This serializer can only serialize type PingApp.Entity.Category");
            }

            if (bsonReader.GetCurrentBsonType() != BsonType.Int32) {
                throw new FormatException("Category should be serialized to Int32");
            }

            int id = bsonReader.ReadInt32();
            return Category.Get(id);
        }
예제 #15
0
        public override object Deserialize(BsonReader bsonReader,
            Type nominalType, Type actualType, IBsonSerializationOptions options) {
            if (nominalType != typeof(AppBrief) || actualType != typeof(AppBrief)) {
                throw new BsonSerializationException("This serializer can only serialize type PingApp.Entity.AppBrief");
            }

            if (bsonReader.GetCurrentBsonType() != BsonType.Int32) {
                throw new FormatException("AppBrief should be serialized to Int32");
            }

            int id = bsonReader.ReadInt32();
            return new AppBrief() { Id = id };
        }
        // 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(byte));

            var bsonType = bsonReader.GetCurrentBsonType();
            byte value;
            var lostData = false;
            switch (bsonType)
            {
                case BsonType.Binary:
                    var bytes = bsonReader.ReadBytes();
                    if (bytes.Length != 1)
                    {
                        throw new FileFormatException("Binary data for Byte must be exactly one byte long.");
                    }
                    value = bytes[0];
                    break;
                case BsonType.Int32:
                    var int32Value = bsonReader.ReadInt32();
                    value = (byte)int32Value;
                    lostData = (int)value != int32Value;
                    break;
                case BsonType.Int64:
                    var int64Value = bsonReader.ReadInt64();
                    value = (byte)int64Value;
                    lostData = (int)value != int64Value;
                    break;
                case BsonType.String:
                    var s = bsonReader.ReadString();
                    if (s.Length == 1)
                    {
                        s = "0" + s;
                    }
                    value = byte.Parse(s, NumberStyles.HexNumber);
                    break;
                default:
                    var message = string.Format("Cannot deserialize Byte from BsonType {0}.", bsonType);
                    throw new FileFormatException(message);
            }
            if (lostData)
            {
                var message = string.Format("Data loss occurred when trying to convert from {0} to Byte.", bsonType);
                throw new FileFormatException(message);
            }

            return value;
        }
 public void TestArrayOneElement()
 {
     var json = "[1]";
     using (_bsonReader = new JsonReader(json))
     {
         Assert.AreEqual(BsonType.Array, _bsonReader.ReadBsonType());
         _bsonReader.ReadStartArray();
         Assert.AreEqual(BsonType.Int32, _bsonReader.ReadBsonType());
         Assert.AreEqual(1, _bsonReader.ReadInt32());
         Assert.AreEqual(BsonType.EndOfDocument, _bsonReader.ReadBsonType());
         _bsonReader.ReadEndArray();
         Assert.AreEqual(BsonReaderState.Done, _bsonReader.State);
     }
     Assert.AreEqual(json, BsonSerializer.Deserialize<BsonArray>(json).ToJson());
 }
        // 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);
            }
        }
예제 #20
0
 // 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 OXmlDocSectionElement _Deserialize(BsonReader bsonReader, IBsonSerializationOptions options)
 {
     OXmlDocSectionElement element = new OXmlDocSectionElement();
     while (true)
     {
         BsonType bsonType = bsonReader.ReadBsonType();
         if (bsonType == BsonType.EndOfDocument)
             break;
         string name = bsonReader.ReadName();
         switch (name.ToLower())
         {
             case "type":
                 if (bsonType != BsonType.String)
                     throw new PBException($"wrong type value {bsonType}");
                 string type = bsonReader.ReadString();
                 if (type.ToLower() != "docsection")
                     throw new PBException($"invalid Type {type} when deserialize OXmlDocSectionElement");
                 break;
             case "pagesize":
                 if (bsonType == BsonType.Null)
                     break;
                 if (bsonType != BsonType.Document)
                     throw new PBException($"wrong PageSize value {bsonType}");
                 element.PageSize = ReadPageSize(bsonReader);
                 break;
             case "pagemargin":
                 if (bsonType == BsonType.Null)
                     break;
                 if (bsonType != BsonType.Document)
                     throw new PBException($"wrong PageMargin value {bsonType}");
                 element.PageMargin = ReadPageMargin(bsonReader);
                 break;
             case "pagenumberstart":
                 if (bsonType == BsonType.Null)
                     break;
                 if (bsonType != BsonType.Int32)
                     throw new PBException($"wrong PageNumberStart value {bsonType}");
                 element.PageNumberStart = bsonReader.ReadInt32();
                 break;
             default:
                 throw new PBException($"unknow DocSection value \"{name}\"");
         }
     }
     return element;
 }
        // public 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);
            }
        }
예제 #23
0
        // 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);
            }
        }
예제 #24
0
        // 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);
            }
        }
 #pragma warning disable 618 // about obsolete BsonBinarySubType.OldBinary
 public override object Deserialize(
     BsonReader bsonReader,
     Type nominalType
 ) {
     BsonType bsonType = bsonReader.CurrentBsonType;
     if (bsonType == BsonType.Null) {
         bsonReader.ReadNull();
         return null;
     } else if (bsonType == BsonType.Binary) {
         byte[] bytes;
         BsonBinarySubType subType;
         bsonReader.ReadBinaryData(out bytes, out subType);
         if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
             var message = string.Format("Invalid Binary sub type: {0}", subType);
             throw new FileFormatException(message);
         }
         return new BitArray(bytes);
     } else if (bsonType == BsonType.Document) {
         bsonReader.ReadStartDocument();
         var length = bsonReader.ReadInt32("Length");
         byte[] bytes;
         BsonBinarySubType subType;
         bsonReader.ReadBinaryData("Bytes", out bytes, out subType);
         if (subType != BsonBinarySubType.Binary && subType != BsonBinarySubType.OldBinary) {
             var message = string.Format("Invalid Binary sub type: {0}", subType);
             throw new FileFormatException(message);
         }
         bsonReader.ReadEndDocument();
         var bitArray = new BitArray(bytes);
         bitArray.Length = length;
         return bitArray;
     } else {
         var message = string.Format("Cannot deserialize Byte[] from BsonType: {0}", bsonType);
         throw new FileFormatException(message);
     }
 }
        public void TestBookmark() {
            var json = "{ \"x\" : 1, \"y\" : 2 }";
            using (bsonReader = BsonReader.Create(json)) {
                // do everything twice returning to bookmark in between
                var bookmark = bsonReader.GetBookmark();
                Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType());

                bookmark = bsonReader.GetBookmark();
                bsonReader.ReadStartDocument();
                bsonReader.ReturnToBookmark(bookmark);
                bsonReader.ReadStartDocument();

                bookmark = bsonReader.GetBookmark();
                Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());

                bookmark = bsonReader.GetBookmark();
                Assert.AreEqual("x", bsonReader.ReadName());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual("x", bsonReader.ReadName());

                bookmark = bsonReader.GetBookmark();
                Assert.AreEqual(1, bsonReader.ReadInt32());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual(1, bsonReader.ReadInt32());

                bookmark = bsonReader.GetBookmark();
                Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());

                bookmark = bsonReader.GetBookmark();
                Assert.AreEqual("y", bsonReader.ReadName());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual("y", bsonReader.ReadName());

                bookmark = bsonReader.GetBookmark();
                Assert.AreEqual(2, bsonReader.ReadInt32());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual(2, bsonReader.ReadInt32());

                bookmark = bsonReader.GetBookmark();
                Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType());
                bsonReader.ReturnToBookmark(bookmark);
                Assert.AreEqual(BsonType.EndOfDocument, bsonReader.ReadBsonType());

                bookmark = bsonReader.GetBookmark();
                bsonReader.ReadEndDocument();
                bsonReader.ReturnToBookmark(bookmark);
                bsonReader.ReadEndDocument();

                Assert.AreEqual(BsonReaderState.Done, bsonReader.State);

            }
            Assert.AreEqual(json, BsonSerializer.Deserialize<BsonDocument>(new StringReader(json)).ToJson());
        }
 public void TestNestedDocument() {
     var json = "{ \"a\" : { \"b\" : 1, \"c\" : 2 } }";
     using (bsonReader = BsonReader.Create(json)) {
         Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType());
         bsonReader.ReadStartDocument();
         Assert.AreEqual(BsonType.Document, bsonReader.ReadBsonType());
         Assert.AreEqual("a", bsonReader.ReadName());
         bsonReader.ReadStartDocument();
         Assert.AreEqual("b", bsonReader.ReadName());
         Assert.AreEqual(1, bsonReader.ReadInt32());
         Assert.AreEqual("c", bsonReader.ReadName());
         Assert.AreEqual(2, bsonReader.ReadInt32());
         bsonReader.ReadEndDocument();
         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 void TestInt32() {
     var json = "123";
     using (bsonReader = BsonReader.Create(json)) {
         Assert.AreEqual(BsonType.Int32, bsonReader.ReadBsonType());
         Assert.AreEqual(123, bsonReader.ReadInt32());
         Assert.AreEqual(BsonReaderState.Done, bsonReader.State);
     }
     Assert.AreEqual(json, BsonSerializer.Deserialize<int>(new StringReader(json)).ToJson());
 }
 public void TestDocumentTwoElements() {
     var json = "{ \"x\" : 1, \"y\" : 2 }";
     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.Int32, bsonReader.ReadBsonType());
         Assert.AreEqual("y", bsonReader.ReadName());
         Assert.AreEqual(2, 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());
 }