Beispiel #1
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Boolean: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadBoolean() ? 1 : 0)));

            case BsonType.Decimal128: return(this.ToNullable(Enum.ToObject(this.enumType, (int)Reader.ReadDecimal128())));

            case BsonType.Double: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadDouble())));

            case BsonType.Int32: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadInt32())));

            case BsonType.Int64: return(this.ToNullable(Enum.ToObject(this.enumType, Reader.ReadInt64())));

            case BsonType.String: return(this.ToNullable(Enum.Parse(this.enumType, Reader.ReadString())));

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected an enum value.");
            }
        }
Beispiel #2
0
        static object ReadObject(IBsonReader bsonReader)         //_120509_173140 keep consistent
        {
            switch (bsonReader.GetCurrentBsonType())
            {
            case BsonType.Array: return(ReadArray(bsonReader));                                                                                               // replacement

            case BsonType.Binary: var binary = BsonSerializer.Deserialize <BsonValue>(bsonReader); return(BsonTypeMapper.MapToDotNetValue(binary) ?? binary); // byte[] or Guid else self

            case BsonType.Boolean: return(bsonReader.ReadBoolean());

            case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()));

            case BsonType.Document: return(ReadCustomObject(bsonReader));                    // replacement

            case BsonType.Double: return(bsonReader.ReadDouble());

            case BsonType.Int32: return(bsonReader.ReadInt32());

            case BsonType.Int64: return(bsonReader.ReadInt64());

            case BsonType.Null: bsonReader.ReadNull(); return(null);

            case BsonType.ObjectId: return(bsonReader.ReadObjectId());

            case BsonType.String: return(bsonReader.ReadString());

            default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader));
            }
        }
Beispiel #3
0
        /***************************************************/

        public override object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            IBsonReader reader          = context.Reader;
            BsonType    currentBsonType = reader.GetCurrentBsonType();

            switch (currentBsonType)
            {
            case BsonType.Array:
                if (context.DynamicArraySerializer != null)
                {
                    return(context.DynamicArraySerializer.Deserialize(context));
                }
                break;

            case BsonType.Binary:
            {
                BsonBinaryData    bsonBinaryData = reader.ReadBinaryData();
                BsonBinarySubType subType        = bsonBinaryData.SubType;
                if (subType == BsonBinarySubType.UuidStandard || subType == BsonBinarySubType.UuidLegacy)
                {
                    return(bsonBinaryData.ToGuid());
                }
                break;
            }

            case BsonType.Boolean:
                return(reader.ReadBoolean());

            case BsonType.DateTime:
                return(new BsonDateTime(reader.ReadDateTime()).ToUniversalTime());

            case BsonType.Decimal128:
                return(reader.ReadDecimal128());

            case BsonType.Document:
                return(DeserializeDiscriminatedValue(context, args));

            case BsonType.Double:
                return(reader.ReadDouble());

            case BsonType.Int32:
                return(reader.ReadInt32());

            case BsonType.Int64:
                return(reader.ReadInt64());

            case BsonType.Null:
                reader.ReadNull();
                return(null);

            case BsonType.ObjectId:
                return(reader.ReadObjectId());

            case BsonType.String:
                return(reader.ReadString());
            }

            Engine.Reflection.Compute.RecordError($"ObjectSerializer does not support BSON type '{currentBsonType}'.");
            return(null);
        }
Beispiel #4
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Boolean: return(Reader.ReadBoolean());

            case BsonType.Decimal128: return(Reader.ReadDecimal128() != 0);

            case BsonType.Double: return(Reader.ReadDouble() != 0);

            case BsonType.Int32: return(Reader.ReadInt32() != 0);

            case BsonType.Int64: return(Reader.ReadInt64() != 0);

            case BsonType.MinKey: Reader.ReadMinKey(); return(false);

            case BsonType.MaxKey: Reader.ReadMaxKey(); return(true);

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected a boolean value.");
            }
        }
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Decimal128: return((char?)Reader.ReadDecimal128());

            case BsonType.Double: return((char?)Reader.ReadDouble());

            case BsonType.Int32: return((char?)Reader.ReadInt32());

            case BsonType.Int64: return((char?)Reader.ReadInt64());

            case BsonType.MinKey: Reader.ReadMinKey(); return((char?)char.MinValue);

            case BsonType.MaxKey: Reader.ReadMaxKey(); return((char?)char.MaxValue);

            case BsonType.Null: Reader.ReadNull(); return(null);

            case BsonType.String:
                string s = Reader.ReadString();
                return((char?)(string.IsNullOrEmpty(s) ? (char?)0 : s[0]));

            default: throw new Exception("Expected a nullable char value.");
            }
        }
Beispiel #6
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Boolean: return(Reader.ReadBoolean() ? (byte?)1 : (byte?)0);

            case BsonType.Decimal128: return((byte?)Reader.ReadDecimal128());

            case BsonType.Double: return((byte?)Reader.ReadDouble());

            case BsonType.Int32: return((byte?)Reader.ReadInt32());

            case BsonType.Int64: return((byte?)Reader.ReadInt64());

            case BsonType.String: return((byte?)byte.Parse(Reader.ReadString()));

            case BsonType.MinKey: Reader.ReadMinKey(); return((byte?)byte.MinValue);

            case BsonType.MaxKey: Reader.ReadMaxKey(); return((byte?)byte.MaxValue);

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected a nullable byte value.");
            }
        }
        /// <summary>
        /// Reads a typed array.
        /// </summary>
        /// <param name="T">Element type.</param>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static Array ReadArray(Type T, MongoDBProvider Provider, IBsonReader Reader, BsonType FieldDataType)
        {
            switch (FieldDataType)
            {
            case BsonType.Array:
                List <object>     Elements = new List <object>();
                IObjectSerializer S        = Provider.GetObjectSerializer(T ?? typeof(GenericObject));

                Reader.ReadStartArray();
                while (Reader.State != BsonReaderState.EndOfArray)
                {
                    BsonType?ElementType = null;

                    if (Reader.State == BsonReaderState.Type)
                    {
                        ElementType = Reader.ReadBsonType();
                        if (ElementType == BsonType.EndOfDocument)
                        {
                            break;
                        }
                    }

                    Elements.Add(S.Deserialize(Reader, ElementType, true));
                }

                Reader.ReadEndArray();

                if (T is null)
                {
                    return(Elements.ToArray());
                }

                int   c      = Elements.Count;
                Array Result = Array.CreateInstance(T, c);
                Array.Copy(Elements.ToArray(), 0, Result, 0, c);

                return(Result);

            case BsonType.Binary:
                byte[] Bin = Reader.ReadBytes();

                if (T is null || T == typeof(byte))
                {
                    return(Bin);
                }

                c      = Bin.Length;
                Result = Array.CreateInstance(T, c);
                Array.Copy(Bin, 0, Result, 0, c);

                return(Result);

            case BsonType.Null:
                Reader.ReadNull();
                return(null);

            default:
                throw new Exception("Array expected.");
            }
        }
        /// <summary>
        /// Reads a string value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static string ReadString(IBsonReader Reader, BsonType FieldDataType)
        {
            switch (FieldDataType)
            {
            case BsonType.Boolean: return(Reader.ReadBoolean().ToString());

            case BsonType.DateTime: return(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()).ToString());

            case BsonType.Decimal128: return(Reader.ReadDecimal128().ToString());

            case BsonType.Double: return(Reader.ReadDouble().ToString());

            case BsonType.Int32: return(Reader.ReadInt32().ToString());

            case BsonType.Int64: return(Reader.ReadInt64().ToString());

            case BsonType.JavaScript: return(Reader.ReadJavaScript());

            case BsonType.JavaScriptWithScope: return(Reader.ReadJavaScriptWithScope());

            case BsonType.Null: Reader.ReadNull(); return(null);

            case BsonType.ObjectId: return(Reader.ReadObjectId().ToString());

            case BsonType.String: return(Reader.ReadString());

            case BsonType.Symbol: return(Reader.ReadSymbol());

            default:
                throw new ArgumentException("Expected a char value, but was a " +
                                            FieldDataType.ToString() + ".", nameof(FieldDataType));
            }
        }
Beispiel #9
0
        private void DeserializeDataPointAttribute(string attr, IBsonReader reader, ref DataPoint dataPoint)
        {
            switch (attr)
            {
            case "Value":
                dataPoint.Value = reader.ReadDecimal128().ToDecimal();
                break;

            case "Unit":
                if (reader.GetCurrentBsonType() == BsonType.Null)
                {
                    reader.ReadNull();
                    dataPoint.Unit = null;
                }
                else
                {
                    dataPoint.Unit = reader.ReadString();
                }
                break;

            case "Precision":
                if (reader.GetCurrentBsonType() == BsonType.Null)
                {
                    reader.ReadNull();
                    dataPoint.Precision = null;
                }

                dataPoint.Precision = reader.ReadDouble();
                break;

            case "Accuracy":
                if (reader.GetCurrentBsonType() == BsonType.Null)
                {
                    reader.ReadNull();
                    dataPoint.Accuracy = null;
                }

                dataPoint.Accuracy = reader.ReadDouble();
                break;

            default:
                throw new DatabaseException("Unknown document attribute", "Measurements");
            }
        }
        /// <summary>
        ///     安全地读入<c>null</c>类型的字段
        /// </summary>
        /// <param name="bsonReader">Bson读取器</param>
        /// <param name="expected">字段名</param>
        /// <param name="read">字段名缓存</param>
        /// <returns>是否成功</returns>
        public static bool ReadNull(this IBsonReader bsonReader, string expected, ref string read)
        {
            if (!bsonReader.ReadName(expected, ref read))
            {
                return(false);
            }

            bsonReader.ReadNull();
            return(true);
        }
Beispiel #11
0
        private JToken GenerateJToken(IBsonReader reader, JToken parent)
        {
            switch (reader.CurrentBsonType)
            {
            case BsonType.Symbol:
            case BsonType.JavaScriptWithScope:
            case BsonType.JavaScript:
            case BsonType.ObjectId:
            case BsonType.RegularExpression:
            case BsonType.DateTime:
            case BsonType.Decimal128:
            case BsonType.MinKey:
            case BsonType.MaxKey:
            case BsonType.String:
                return(reader.ReadString());

            case BsonType.Binary:
                return(reader.ReadBytes());

            case BsonType.Undefined:
                reader.ReadUndefined();
                return(JValue.CreateUndefined());

            case BsonType.Boolean:
                return(reader.ReadBoolean());

            case BsonType.Null:
                reader.ReadNull();
                return(JValue.CreateNull());

            case BsonType.Int32:
                return(reader.ReadInt32());

            case BsonType.Int64:
            case BsonType.Timestamp:
                return(reader.ReadInt64());

            case BsonType.Double:
                return(reader.ReadDouble());

            case BsonType.Document:
                return(GenerateJObject(reader, parent));

            case BsonType.Array:
                return(GenerateJArray(reader, parent));

            case BsonType.EndOfDocument:
                break;

            default:
                break;
            }
            return(null);
        }
        public void TestNull()
        {
            var json = "null";

            using (_bsonReader = new JsonReader(json))
            {
                Assert.Equal(BsonType.Null, _bsonReader.ReadBsonType());
                _bsonReader.ReadNull();
                Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
            }
            Assert.Equal(json, BsonSerializer.Deserialize <BsonNull>(json).ToJson());
        }
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.DateTime: return((DateTimeOffset?)ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()));

            case BsonType.String: return((DateTimeOffset?)DateTimeOffset.Parse(Reader.ReadString()));

            case BsonType.Document:
                DateTime TP = DateTime.MinValue;
                TimeSpan TZ = TimeSpan.Zero;

                Reader.ReadStartDocument();

                while (Reader.State == BsonReaderState.Type)
                {
                    BsonType BsonType = Reader.ReadBsonType();
                    if (BsonType == BsonType.EndOfDocument)
                    {
                        break;
                    }

                    string FieldName = Reader.ReadName();
                    switch (FieldName)
                    {
                    case "tp":
                        TP = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime());
                        break;

                    case "tz":
                        TZ = TimeSpan.Parse(Reader.ReadString());
                        break;
                    }
                }

                Reader.ReadEndDocument();

                return((DateTimeOffset?)new DateTimeOffset(TP, TZ));

            case BsonType.MinKey: Reader.ReadMinKey(); return((DateTimeOffset?)DateTimeOffset.MinValue);

            case BsonType.MaxKey: Reader.ReadMaxKey(); return((DateTimeOffset?)DateTimeOffset.MaxValue);

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected a nullable DateTimeOffset value.");
            }
        }
        public override T Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            IBsonReader r = context.Reader;

            if (r.CurrentBsonType == BsonType.Null)
            {
                r.ReadNull();
                return(null !);
            }

            return(DeserializeCore(r));
        }
        /// <summary>
        /// Reads a string value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static byte[] ReadByteArray(IBsonReader Reader, BsonType FieldDataType)
        {
            switch (FieldDataType)
            {
            case BsonType.Binary: return(Reader.ReadBinaryData().Bytes);

            case BsonType.Null: Reader.ReadNull(); return(null);

            default:
                throw new ArgumentException("Expected a byte array value, but was a " +
                                            FieldDataType.ToString() + ".", nameof(FieldDataType));
            }
        }
        public override EventID Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            IBsonReader r = context.Reader;

            if (r.CurrentBsonType == BsonType.Null)
            {
                r.ReadNull();
                return(null);
            }

            EnsureBsonTypeEquals(r, BsonType.Int64);
            long rawValue = r.ReadInt64();

            return(new EventID(rawValue));
        }
Beispiel #17
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.String: return(Guid.Parse(Reader.ReadString()));

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected a Guid value.");
            }
        }
Beispiel #18
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Binary: return(Reader.ReadBinaryData().Bytes);

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected a byte array.");
            }
        }
        /// <summary>
        /// Reads a nullable enum stored as a nullable 32-bit integer value.
        /// </summary>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <param name="EnumType">Enumeration type.</param>
        /// <returns>Nullable enumeration value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static object ReadNullableEnum(IBsonReader Reader, BsonType FieldDataType, Type EnumType)
        {
            switch (FieldDataType)
            {
            case BsonType.Int32: return(Enum.ToObject(EnumType, ReadInt32(Reader, FieldDataType)));

            case BsonType.Int64: return(Enum.ToObject(EnumType, ReadInt64(Reader, FieldDataType)));

            case BsonType.String: return(Enum.Parse(EnumType, Reader.ReadString()));

            case BsonType.Null: Reader.ReadNull(); return(null);

            default:
                throw new ArgumentException("Expected an enumerated value, but was a " + FieldDataType.ToString() + ".", nameof(FieldDataType));
            }
        }
Beispiel #20
0
    public override AssetItem Deserialize(IBsonReader bsonReader)
    {
        string read = null;

        bsonReader.ReadStartDocument();
        var vid = bsonReader.ReadObjectId("voucher", ref read);
        var dt  = bsonReader.ReadDateTime("date", ref read);
        var rmk = bsonReader.ReadString("remark", ref read);

        AssetItem item;
        double?   val;

        if ((val = bsonReader.ReadDouble("acq", ref read)).HasValue)
        {
            item = new AcquisitionItem {
                VoucherID = vid, Date = dt, Remark = rmk, OrigValue = val.Value
            }
        }
        ;
        else if ((val = bsonReader.ReadDouble("dep", ref read)).HasValue)
        {
            item = new DepreciateItem {
                VoucherID = vid, Date = dt, Remark = rmk, Amount = val.Value
            }
        }
        ;
        else if ((val = bsonReader.ReadDouble("devto", ref read)).HasValue)
        {
            item = new DevalueItem {
                VoucherID = vid, Date = dt, Remark = rmk, FairValue = val.Value
            }
        }
        ;
        else if (bsonReader.ReadNull("dispo", ref read))
        {
            item = new DispositionItem {
                VoucherID = vid, Date = dt, Remark = rmk
            }
        }
        ;
        else
        {
            item = null;
        }
        bsonReader.ReadEndDocument();
        return(item);
    }
Beispiel #21
0
        object GetDeserializedValue(Type valueType, ref IBsonReader bsonReader)
        {
            var bsonType = bsonReader.CurrentBsonType;

            if (bsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }

            if (valueType == typeof(Guid))
            {
                var binaryData = bsonReader.ReadBinaryData();
                return(binaryData.ToGuid());
            }
            else if (valueType == typeof(double))
            {
                return(bsonReader.ReadDouble());
            }
            else if (valueType == typeof(float))
            {
                return((float)bsonReader.ReadDouble());
            }
            else if (valueType == typeof(int))
            {
                return(bsonReader.ReadInt32());
            }
            else if (valueType == typeof(long))
            {
                return(bsonReader.ReadInt64());
            }
            else if (valueType == typeof(bool))
            {
                return(bsonReader.ReadBoolean());
            }
            else if (valueType == typeof(string))
            {
                return(bsonReader.ReadString());
            }
            else if (valueType == typeof(decimal))
            {
                return(decimal.Parse(bsonReader.ReadString(), CultureInfo.InvariantCulture));
            }

            throw new FailedConceptSerialization($"Could not deserialize the concept value to '{valueType.FullName}'");
        }
Beispiel #22
0
        object GetDeserializedValue(Type valueType, ref IBsonReader bsonReader)
        {
            var bsonType = bsonReader.CurrentBsonType;

            if (bsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            if (valueType == typeof(Guid))
            {
                var binaryData = bsonReader.ReadBinaryData();
                return(binaryData.ToGuid());
            }
            else if (valueType == typeof(double))
            {
                return(bsonReader.ReadDouble());
            }
            else if (valueType == typeof(float))
            {
                return((float)bsonReader.ReadDouble());
            }
            else if (valueType == typeof(Int32))
            {
                return(bsonReader.ReadInt32());
            }
            else if (valueType == typeof(Int64))
            {
                return(bsonReader.ReadInt64());
            }
            else if (valueType == typeof(bool))
            {
                return(bsonReader.ReadBoolean());
            }
            else if (valueType == typeof(string))
            {
                return(bsonReader.ReadString());
            }
            else if (valueType == typeof(decimal))
            {
                return(decimal.Parse(bsonReader.ReadString()));
            }

            throw new Exception();
        }
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.String: return((TimeSpan?)TimeSpan.Parse(Reader.ReadString()));

            case BsonType.MinKey: Reader.ReadMinKey(); return((TimeSpan?)TimeSpan.MinValue);

            case BsonType.MaxKey: Reader.ReadMaxKey(); return((TimeSpan?)TimeSpan.MaxValue);

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected a nullable TimeSpan value.");
            }
        }
        /// <summary>
        ///     做安全读入指定字段之前的准备工作
        /// </summary>
        /// <param name="bsonReader">Bson读取器</param>
        /// <param name="expected">字段名</param>
        /// <param name="read">字段名缓存</param>
        /// <returns>是否可以继续读入</returns>
        private static bool ReadPrep(this IBsonReader bsonReader, string expected, ref string read)
        {
            if (!bsonReader.ReadName(expected, ref read))
            {
                return(false);
            }

            switch (bsonReader.CurrentBsonType)
            {
            case BsonType.Null:
                bsonReader.ReadNull();
                return(false);

            case BsonType.Undefined:
                bsonReader.ReadUndefined();
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Reads a typed array.
        /// </summary>
        /// <typeparam name="T">Element type.</typeparam>
        /// <param name="Provider">Database provider object.</param>
        /// <param name="Reader">Binary reader.</param>
        /// <param name="FieldDataType">Field data type.</param>
        /// <returns>String value.</returns>
        /// <exception cref="ArgumentException">If the <paramref name="FieldDataType"/> was invalid.</exception>
        public static T[] ReadArray <T>(MongoDBProvider Provider, IBsonReader Reader, BsonType FieldDataType)
        {
            switch (FieldDataType)
            {
            case BsonType.Array:
                List <T>          Elements = new List <T>();
                IObjectSerializer S        = Provider.GetObjectSerializer(typeof(T));

                Reader.ReadStartArray();
                while (Reader.State != BsonReaderState.EndOfArray)
                {
                    BsonType?ElementType = null;

                    if (Reader.State == BsonReaderState.Type)
                    {
                        ElementType = Reader.ReadBsonType();
                        if (ElementType == BsonType.EndOfDocument)
                        {
                            break;
                        }
                    }

                    Elements.Add((T)S.Deserialize(Reader, ElementType, true));
                }

                Reader.ReadEndArray();

                return(Elements.ToArray());

            case BsonType.Binary:
                object Temp = Reader.ReadBytes();
                return((T[])Temp);

            case BsonType.Null:
                Reader.ReadNull();
                return(null);

            default:
                throw new Exception("Array expected.");
            }
        }
Beispiel #26
0
        static object ReadObject(IBsonReader bsonReader)         //_120509_173140 sync, test
        {
            switch (bsonReader.GetCurrentBsonType())
            {
            case BsonType.Array: return(ReadArray(bsonReader));                    // replacement

            case BsonType.Boolean: return(bsonReader.ReadBoolean());

            case BsonType.DateTime: return(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()));

            case BsonType.Decimal128: return(Decimal128.ToDecimal(bsonReader.ReadDecimal128()));

            case BsonType.Document: return(ReadCustomObject(bsonReader));                    // replacement

            case BsonType.Double: return(bsonReader.ReadDouble());

            case BsonType.Int32: return(bsonReader.ReadInt32());

            case BsonType.Int64: return(bsonReader.ReadInt64());

            case BsonType.Null: bsonReader.ReadNull(); return(null);

            case BsonType.ObjectId: return(bsonReader.ReadObjectId());

            case BsonType.String: return(bsonReader.ReadString());

            case BsonType.Binary:
                var data = bsonReader.ReadBinaryData();
                switch (data.SubType)
                {
                case BsonBinarySubType.UuidLegacy:
                case BsonBinarySubType.UuidStandard:
                    return(data.ToGuid());

                default:
                    return(data);
                }

            default: return(BsonSerializer.Deserialize <BsonValue>(bsonReader));
            }
        }
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public override object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.DateTime: return((DateTime?)ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()));

            case BsonType.String: return((DateTime?)DateTime.Parse(Reader.ReadString()));

            case BsonType.MinKey: Reader.ReadMinKey(); return((DateTime?)DateTime.MinValue);

            case BsonType.MaxKey: Reader.ReadMaxKey(); return((DateTime?)DateTime.MaxValue);

            case BsonType.Null: Reader.ReadNull(); return(null);

            default: throw new Exception("Expected a nullable DateTime value.");
            }
        }
        internal static object Deserialize(IBsonReader reader)
        {
            switch (reader.GetCurrentBsonType())
            {
            case BsonType.ObjectId:
                return(reader.ReadObjectId());

            case BsonType.Boolean:
                return(reader.ReadBoolean());

            case BsonType.DateTime:
                return(UnixTime.ToDateTime(reader.ReadDateTime()));

            case BsonType.Int32:
                return(reader.ReadInt32());

            case BsonType.Binary:
                return(reader.ReadBytes());

            case BsonType.Int64:
                return(reader.ReadInt64());

            case BsonType.Double:
                return(reader.ReadDouble());

            case BsonType.Null:
                reader.ReadNull();
                return(null);

            case BsonType.String:
                return(reader.ReadString());

            default:
                throw new InvalidOperationException(
                          $"Cannot deserialize {reader.GetCurrentBsonType()} to native value.");
            }
        }
Beispiel #29
0
        public override TimeSpan Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
        {
            IBsonReader reader   = context.Reader;
            BsonType    bsonType = reader.GetCurrentBsonType();

            switch (bsonType)
            {
            case BsonType.Null:
                reader.ReadNull();
                return(TimeSpan.Zero);

            case BsonType.Int32:
                return(TimeSpan.FromMilliseconds((double)reader.ReadInt32()));

            case BsonType.Int64:
                return(TimeSpan.FromMilliseconds((double)reader.ReadInt64()));

            case BsonType.Double:
                return(TimeSpan.FromMilliseconds(reader.ReadDouble()));

            default:
                throw base.CreateCannotDeserializeFromBsonTypeException(bsonType);
            }
        }
Beispiel #30
0
        /// <summary>
        /// Deserializes an object from a binary source.
        /// </summary>
        /// <param name="Reader">Binary deserializer.</param>
        /// <param name="DataType">Optional datatype. If not provided, will be read from the binary source.</param>
        /// <param name="Embedded">If the object is embedded into another.</param>
        /// <returns>Deserialized object.</returns>
        public object Deserialize(IBsonReader Reader, BsonType?DataType, bool Embedded)
        {
            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Boolean: return(new CaseInsensitiveString(Reader.ReadBoolean().ToString()));

            case BsonType.DateTime: return(new CaseInsensitiveString(ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime()).ToString()));

            case BsonType.Decimal128: return(new CaseInsensitiveString(Reader.ReadDecimal128().ToString()));

            case BsonType.Double: return(new CaseInsensitiveString(Reader.ReadDouble().ToString()));

            case BsonType.Int32: return(new CaseInsensitiveString(Reader.ReadInt32().ToString()));

            case BsonType.Int64: return(new CaseInsensitiveString(Reader.ReadInt64().ToString()));

            case BsonType.JavaScript: return(new CaseInsensitiveString(Reader.ReadJavaScript()));

            case BsonType.JavaScriptWithScope: return(new CaseInsensitiveString(Reader.ReadJavaScriptWithScope()));

            case BsonType.Null: Reader.ReadNull(); return(null);

            case BsonType.ObjectId: return(new CaseInsensitiveString(Reader.ReadObjectId().ToString()));

            case BsonType.String: return(new CaseInsensitiveString(Reader.ReadString()));

            case BsonType.Symbol: return(new CaseInsensitiveString(Reader.ReadSymbol()));

            default: throw new Exception("Expected a case-insensitive string value.");
            }
        }
 public void TestNull()
 {
     var json = "null";
     using (_bsonReader = new JsonReader(json))
     {
         Assert.Equal(BsonType.Null, _bsonReader.ReadBsonType());
         _bsonReader.ReadNull();
         Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
     }
     Assert.Equal(json, BsonSerializer.Deserialize<BsonNull>(json).ToJson());
 }