示例#1
0
        public override Voucher Deserialize(IBsonReader bsonReader)
        {
            string read = null;

            bsonReader.ReadStartDocument();

            var voucher = new Voucher
            {
                ID   = bsonReader.ReadObjectId("_id", ref read),
                Date = bsonReader.ReadDateTime("date", ref read),
                Type = VoucherType.Ordinary,
            };

            voucher.Type = bsonReader.ReadString("special", ref read) switch
            {
                "amorz" => VoucherType.Amortization,
                "acarry" => VoucherType.AnnualCarry,
                "carry" => VoucherType.Carry,
                "dep" => VoucherType.Depreciation,
                "dev" => VoucherType.Devalue,
                "unc" => VoucherType.Uncertain,
                _ => VoucherType.Ordinary,
            };

            voucher.Details = bsonReader.ReadArray("detail", ref read, new VoucherDetailSerializer().Deserialize);
            voucher.Remark  = bsonReader.ReadString("remark", ref read);
            bsonReader.ReadEndDocument();

            return(voucher);
        }
        /// <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));
            }
        }
示例#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);
        }
示例#4
0
    /// <inheritdoc/>
    public Type GetActualType(IBsonReader bsonReader, Type nominalType)
    {
        ThrowIfNominalTypeIsIncorrect(nominalType);
        var bookmark = bsonReader.GetBookmark();

        bsonReader.ReadStartDocument();
        ObjectId id = default;

        while (bsonReader.ReadBsonType() != BsonType.EndOfDocument)
        {
            var fieldName = bsonReader.ReadName();
            if (fieldName == ElementName)
            {
                var partitioned = bsonReader.ReadBoolean();
                bsonReader.ReturnToBookmark(bookmark);
                return(partitioned ? typeof(Partitioned.PartitionedStreamProcessorState) : typeof(StreamProcessorState));
            }
            else if (fieldName == "_id")
            {
                id = bsonReader.ReadObjectId();
            }
            else
            {
                bsonReader.SkipValue();
            }
        }

        bsonReader.ReturnToBookmark(bookmark);
        throw new StreamProcessorStateDocumentIsMissingPartitionedField(id);
    }
示例#5
0
文件: Serializer.cs 项目: tchilz/Mdbc
        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));
            }
        }
        public void TestObjectIdShell()
        {
            var json = "ObjectId(\"4d0ce088e447ad08b4721a37\")";

            using (_bsonReader = new JsonReader(json))
            {
                Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType());
                var objectId = _bsonReader.ReadObjectId();
                Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString());
                Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
            }
            Assert.Equal(json, BsonSerializer.Deserialize <ObjectId>(json).ToJson());
        }
        public override Voucher Deserialize(IBsonReader bsonReader)
        {
            string read = null;

            bsonReader.ReadStartDocument();

            var voucher = new Voucher
            {
                ID   = bsonReader.ReadObjectId("_id", ref read),
                Date = bsonReader.ReadDateTime("date", ref read),
                Type = VoucherType.Ordinary
            };

            switch (bsonReader.ReadString("special", ref read))
            {
            case "amorz":
                voucher.Type = VoucherType.Amortization;
                break;

            case "acarry":
                voucher.Type = VoucherType.AnnualCarry;
                break;

            case "carry":
                voucher.Type = VoucherType.Carry;
                break;

            case "dep":
                voucher.Type = VoucherType.Depreciation;
                break;

            case "dev":
                voucher.Type = VoucherType.Devalue;
                break;

            case "unc":
                voucher.Type = VoucherType.Uncertain;
                break;

            default:
                voucher.Type = VoucherType.Ordinary;
                break;
            }

            voucher.Details = bsonReader.ReadArray("detail", ref read, new VoucherDetailSerializer().Deserialize);
            voucher.Remark  = bsonReader.ReadString("remark", ref read);
            bsonReader.ReadEndDocument();

            return(voucher);
        }
        public override AmortItem Deserialize(IBsonReader bsonReader)
        {
            string read = null;

            bsonReader.ReadStartDocument();
            var item = new AmortItem
            {
                VoucherID = bsonReader.ReadObjectId("voucher", ref read),
                Date      = bsonReader.ReadDateTime("date", ref read),
                Amount    = bsonReader.ReadDouble("amount", ref read) ?? 0D,
                Remark    = bsonReader.ReadString("remark", ref read)
            };

            bsonReader.ReadEndDocument();
            return(item);
        }
示例#9
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);
    }
示例#10
0
        public void TestObjectIdStrict()
        {
            var json = "{ \"$oid\" : \"4d0ce088e447ad08b4721a37\" }";

            using (_bsonReader = new JsonReader(json))
            {
                Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType());
                var objectId = _bsonReader.ReadObjectId();
                Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString());
                Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
            }
            var jsonSettings = new JsonWriterSettings {
                OutputMode = JsonOutputMode.Strict
            };

            Assert.Equal(json, BsonSerializer.Deserialize <ObjectId>(json).ToJson(jsonSettings));
        }
示例#11
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));
            }
        }
        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.");
            }
        }
示例#13
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.");
            }
        }
示例#14
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)
        {
            BsonReaderBookmark Bookmark    = Reader.GetBookmark();
            BsonType?          DataTypeBak = DataType;

            if (!DataType.HasValue)
            {
                DataType = Reader.ReadBsonType();
            }

            switch (DataType.Value)
            {
            case BsonType.Document:
                break;

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

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

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

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

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

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

            case BsonType.String:
            case BsonType.Symbol:
            case BsonType.JavaScript:
            case BsonType.JavaScriptWithScope:
                return(Reader.ReadString());

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

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

            default:
                throw new Exception("Object or value expected.");
            }

            LinkedList <KeyValuePair <string, object> > Properties = new LinkedList <KeyValuePair <string, object> >();
            LinkedList <KeyValuePair <string, object> > LowerCase  = null;
            string   TypeName       = string.Empty;
            Guid     ObjectId       = Guid.Empty;
            string   CollectionName = string.Empty;
            string   FieldName;
            BsonType ValueType;
            object   Value;

            Reader.ReadStartDocument();

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

                FieldName = Reader.ReadName();

                switch (ValueType)
                {
                case BsonType.Array:
                    Value = GeneratedObjectSerializerBase.ReadArray(null, this.Provider, Reader, ValueType);
                    break;

                case BsonType.Binary:
                    Value = Reader.ReadBytes();
                    break;

                case BsonType.Boolean:
                    Value = Reader.ReadBoolean();
                    break;

                case BsonType.DateTime:
                    Value = ObjectSerializer.UnixEpoch.AddMilliseconds(Reader.ReadDateTime());
                    break;

                case BsonType.Decimal128:
                    Value = (decimal)Reader.ReadDecimal128();
                    break;

                case BsonType.Document:
                    Value = this.Deserialize(Reader, ValueType, true);
                    break;

                case BsonType.Double:
                    Value = Reader.ReadDouble();
                    break;

                case BsonType.Int32:
                    Value = Reader.ReadInt32();
                    break;

                case BsonType.Int64:
                    Value = Reader.ReadInt64();
                    break;

                case BsonType.JavaScript:
                    Value = Reader.ReadJavaScript();
                    break;

                case BsonType.JavaScriptWithScope:
                    Value = Reader.ReadJavaScriptWithScope();
                    break;

                case BsonType.Null:
                    Value = null;
                    Reader.ReadNull();
                    break;

                case BsonType.ObjectId:
                    Value = Reader.ReadObjectId();
                    break;

                case BsonType.String:
                    Value = Reader.ReadString();
                    break;

                case BsonType.Symbol:
                    Value = Reader.ReadSymbol();
                    break;

                default:
                    throw new Exception("Unrecognized data type: " + ValueType.ToString());
                }

                switch (FieldName)
                {
                case "_id":
                    if (Value is Guid Guid)
                    {
                        ObjectId = Guid;
                    }
                    else if (Value is string s)
                    {
                        ObjectId = new Guid(s);
                    }
                    else if (Value is byte[] A)
                    {
                        ObjectId = new Guid(A);
                    }
                    else if (Value is ObjectId ObjId)
                    {
                        ObjectId = GeneratedObjectSerializerBase.ObjectIdToGuid(ObjId);
                    }
                    else
                    {
                        throw new Exception("Unrecognized Object ID type: " + Value.GetType().FullName);
                    }
                    break;

                case "_type":
                    TypeName = Value?.ToString();

                    if (this.returnTypedObjects && !string.IsNullOrEmpty(TypeName))
                    {
                        Type DesiredType = Types.GetType(TypeName);
                        if (DesiredType is null)
                        {
                            DesiredType = typeof(GenericObject);
                        }

                        if (DesiredType != typeof(GenericObject))
                        {
                            IObjectSerializer Serializer2 = this.provider.GetObjectSerializer(DesiredType);
                            Reader.ReturnToBookmark(Bookmark);
                            return(Serializer2.Deserialize(Reader, DataTypeBak, Embedded));
                        }
                    }
                    break;

                case "_collection":
                    CollectionName = Value?.ToString();
                    break;

                default:
                    if (FieldName.EndsWith("_L"))
                    {
                        string s      = FieldName.Substring(0, FieldName.Length - 2);
                        bool   Ignore = false;

                        foreach (KeyValuePair <string, object> P in Properties)
                        {
                            if (P.Key == s)
                            {
                                Ignore = true;
                                break;
                            }
                        }

                        if (!Ignore)
                        {
                            if (LowerCase is null)
                            {
                                LowerCase = new LinkedList <KeyValuePair <string, object> >();
                            }

                            LowerCase.AddLast(new KeyValuePair <string, object>(s, Value));
                        }
                    }
                    else
                    {
                        Properties.AddLast(new KeyValuePair <string, object>(FieldName, Value));
                    }
                    break;
                }
            }

            if (!(LowerCase is null))
            {
                foreach (KeyValuePair <string, object> P in LowerCase)
                {
                    bool Ignore = false;

                    foreach (KeyValuePair <string, object> P2 in Properties)
                    {
                        if (P2.Key == P.Key)
                        {
                            Ignore = true;
                            break;
                        }
                    }

                    if (!Ignore)
                    {
                        Properties.AddLast(new KeyValuePair <string, object>(P.Key + "_L", P.Value));
                    }
                }
            }

            Reader.ReadEndDocument();

            return(new GenericObject(CollectionName, TypeName, ObjectId, Properties));
        }
示例#15
0
 /// <summary>
 /// Reads a BSON ObjectId element from the reader.
 /// </summary>
 /// <param name="reader">The reader.</param>
 /// <param name="name">The name of the element.</param>
 /// <returns>An ObjectId.</returns>
 public static ObjectId ReadObjectId(this IBsonReader reader, string name)
 {
     VerifyName(reader, name);
     return(reader.ReadObjectId());
 }
        // private methods
        private void ReadValue()
        {
            object jsonDotNetValue;

            switch (_wrappedReader.GetCurrentBsonType())
            {
            case BsonType.Array:
                _wrappedReader.ReadStartArray();
                SetCurrentToken(Newtonsoft.Json.JsonToken.StartArray);
                return;

            case BsonType.Binary:
                var bsonBinaryData = _wrappedReader.ReadBinaryData();
                switch (bsonBinaryData.SubType)
                {
                case BsonBinarySubType.UuidLegacy:
                    var guidRepresentation = GuidRepresentation.Unspecified;
                    var bsonReader         = _wrappedReader as BsonReader;
                    if (bsonReader != null)
                    {
                        guidRepresentation = bsonReader.Settings.GuidRepresentation;
                    }
                    jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, guidRepresentation);
                    break;

                case BsonBinarySubType.UuidStandard:
                    jsonDotNetValue = GuidConverter.FromBytes(bsonBinaryData.Bytes, GuidRepresentation.Standard);
                    break;

                default:
                    jsonDotNetValue = bsonBinaryData.Bytes;
                    break;
                }
                SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, jsonDotNetValue, bsonBinaryData);
                return;

            case BsonType.Boolean:
                var booleanValue = _wrappedReader.ReadBoolean();
                SetCurrentToken(Newtonsoft.Json.JsonToken.Boolean, booleanValue, (BsonBoolean)booleanValue);
                return;

            case BsonType.DateTime:
                var bsonDateTime = new BsonDateTime(_wrappedReader.ReadDateTime());
                if (bsonDateTime.IsValidDateTime)
                {
                    jsonDotNetValue = bsonDateTime.ToUniversalTime();
                }
                else
                {
                    jsonDotNetValue = bsonDateTime.MillisecondsSinceEpoch;
                }
                SetCurrentToken(Newtonsoft.Json.JsonToken.Date, jsonDotNetValue, bsonDateTime);
                return;

            case BsonType.Document:
                _wrappedReader.ReadStartDocument();
                SetCurrentToken(Newtonsoft.Json.JsonToken.StartObject);
                return;

            case BsonType.Double:
                var bsonDouble = new BsonDouble(_wrappedReader.ReadDouble());
                switch (FloatParseHandling)
                {
                case Newtonsoft.Json.FloatParseHandling.Decimal:
                    jsonDotNetValue = Convert.ToDecimal(bsonDouble);
                    break;

                case Newtonsoft.Json.FloatParseHandling.Double:
                    jsonDotNetValue = bsonDouble.Value;
                    break;

                default:
                    throw new NotSupportedException(string.Format("Unexpected FloatParseHandling value: {0}.", FloatParseHandling));
                }
                SetCurrentToken(Newtonsoft.Json.JsonToken.Float, jsonDotNetValue, bsonDouble);
                return;

            case BsonType.Int32:
                var bsonInt32 = (BsonInt32)_wrappedReader.ReadInt32();
                SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, (long)bsonInt32.Value, bsonInt32);
                return;

            case BsonType.Int64:
                var bsonInt64 = (BsonInt64)_wrappedReader.ReadInt64();
                SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonInt64.Value, bsonInt64);
                return;

            case BsonType.JavaScript:
            {
                var code           = _wrappedReader.ReadJavaScript();
                var bsonJavaScript = new BsonJavaScript(code);
                SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScript);
            }
                return;

            case BsonType.JavaScriptWithScope:
            {
                var code    = _wrappedReader.ReadJavaScriptWithScope();
                var context = BsonDeserializationContext.CreateRoot(_wrappedReader);
                var scope   = BsonDocumentSerializer.Instance.Deserialize <BsonDocument>(context);
                var bsonJavaScriptWithScope = new BsonJavaScriptWithScope(code, scope);
                SetCurrentToken(Newtonsoft.Json.JsonToken.String, code, bsonJavaScriptWithScope);
            }
                return;

            case BsonType.MaxKey:
                _wrappedReader.ReadMaxKey();
                SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMaxKey.Value);
                return;

            case BsonType.MinKey:
                _wrappedReader.ReadMinKey();
                SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonMinKey.Value);
                return;

            case BsonType.Null:
                _wrappedReader.ReadNull();
                SetCurrentToken(Newtonsoft.Json.JsonToken.Null, null, BsonNull.Value);
                return;

            case BsonType.ObjectId:
                var bsonObjectId = new BsonObjectId(_wrappedReader.ReadObjectId());
                SetCurrentToken(Newtonsoft.Json.JsonToken.Bytes, bsonObjectId.Value.ToByteArray(), bsonObjectId);
                return;

            case BsonType.RegularExpression:
                var bsonRegularExpression = _wrappedReader.ReadRegularExpression();
                var pattern = bsonRegularExpression.Pattern;
                var options = bsonRegularExpression.Options;
                jsonDotNetValue = "/" + pattern.Replace("/", "\\/") + "/" + options;
                SetCurrentToken(Newtonsoft.Json.JsonToken.String, jsonDotNetValue, bsonRegularExpression);
                return;

            case BsonType.String:
                var stringValue = _wrappedReader.ReadString();
                SetCurrentToken(Newtonsoft.Json.JsonToken.String, stringValue, (BsonString)stringValue);
                return;

            case BsonType.Symbol:
                var bsonSymbol = BsonSymbolTable.Lookup(_wrappedReader.ReadSymbol());
                SetCurrentToken(Newtonsoft.Json.JsonToken.String, bsonSymbol.Name, bsonSymbol);
                return;

            case BsonType.Timestamp:
                var bsonTimestamp = new BsonTimestamp(_wrappedReader.ReadTimestamp());
                SetCurrentToken(Newtonsoft.Json.JsonToken.Integer, bsonTimestamp.Value, bsonTimestamp);
                return;

            case BsonType.Undefined:
                _wrappedReader.ReadUndefined();
                SetCurrentToken(Newtonsoft.Json.JsonToken.Undefined, null, BsonUndefined.Value);
                return;

            default:
                var message = string.Format("Unexpected BsonType: {0}.", _wrappedReader.GetCurrentBsonType());
                throw new Newtonsoft.Json.JsonReaderException(message);
            }
        }
 /// <summary>
 ///     安全地读入<c>ObjectId</c>类型的字段
 /// </summary>
 /// <param name="bsonReader">Bson读取器</param>
 /// <param name="expected">字段名</param>
 /// <param name="read">字段名缓存</param>
 /// <returns>读取结果</returns>
 public static string ReadObjectId(this IBsonReader bsonReader, string expected, ref string read)
 => ReadClass(bsonReader, expected, ref read, () => bsonReader.ReadObjectId().ToString());
 public void TestObjectIdShell()
 {
     var json = "ObjectId(\"4d0ce088e447ad08b4721a37\")";
     using (_bsonReader = new JsonReader(json))
     {
         Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType());
         var objectId = _bsonReader.ReadObjectId();
         Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString());
         Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
     }
     Assert.Equal(json, BsonSerializer.Deserialize<ObjectId>(json).ToJson());
 }
 public void TestObjectIdStrict()
 {
     var json = "{ \"$oid\" : \"4d0ce088e447ad08b4721a37\" }";
     using (_bsonReader = new JsonReader(json))
     {
         Assert.Equal(BsonType.ObjectId, _bsonReader.ReadBsonType());
         var objectId = _bsonReader.ReadObjectId();
         Assert.Equal("4d0ce088e447ad08b4721a37", objectId.ToString());
         Assert.Equal(BsonReaderState.Initial, _bsonReader.State);
     }
     var jsonSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict };
     Assert.Equal(json, BsonSerializer.Deserialize<ObjectId>(json).ToJson(jsonSettings));
 }