Example #1
0
 private void PopContext()
 {
     this._stack.RemoveAt(this._stack.Count - 1);
     if (this._stack.Count == 0)
     {
         this._currentContext = (BsonReader.ContainerContext)null;
     }
     else
     {
         this._currentContext = this._stack[this._stack.Count - 1];
     }
 }
Example #2
0
        private bool ReadCodeWScope()
        {
            switch (this._bsonReaderState)
            {
            case BsonReader.BsonReaderState.CodeWScopeStart:
                this.SetToken(JsonToken.PropertyName, (object)"$code");
                this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeCode;
                return(true);

            case BsonReader.BsonReaderState.CodeWScopeCode:
                this.ReadInt32();
                this.SetToken(JsonToken.String, (object)this.ReadLengthString());
                this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeScope;
                return(true);

            case BsonReader.BsonReaderState.CodeWScopeScope:
                if (this.CurrentState == JsonReader.State.PostValue)
                {
                    this.SetToken(JsonToken.PropertyName, (object)"$scope");
                    return(true);
                }

                this.SetToken(JsonToken.StartObject);
                this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeScopeObject;
                BsonReader.ContainerContext newContext = new BsonReader.ContainerContext(BsonType.Object);
                this.PushContext(newContext);
                newContext.Length = this.ReadInt32();
                return(true);

            case BsonReader.BsonReaderState.CodeWScopeScopeObject:
                int num = this.ReadNormal() ? 1 : 0;
                if (num == 0)
                {
                    return(num != 0);
                }
                if (this.TokenType != JsonToken.EndObject)
                {
                    return(num != 0);
                }
                this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeScopeEnd;
                return(num != 0);

            case BsonReader.BsonReaderState.CodeWScopeScopeEnd:
                this.SetToken(JsonToken.EndObject);
                this._bsonReaderState = BsonReader.BsonReaderState.Normal;
                return(true);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Example #3
0
        private void ReadType(BsonType type)
        {
            switch (type)
            {
            case BsonType.Number:
                double num = this.ReadDouble();
                if (this._floatParseHandling == FloatParseHandling.Decimal)
                {
                    this.SetToken(JsonToken.Float,
                                  (object)Convert.ToDecimal((object)num, (IFormatProvider)CultureInfo.InvariantCulture));
                    break;
                }

                this.SetToken(JsonToken.Float, (object)num);
                break;

            case BsonType.String:
            case BsonType.Symbol:
                this.SetToken(JsonToken.String, (object)this.ReadLengthString());
                break;

            case BsonType.Object:
                this.SetToken(JsonToken.StartObject);
                BsonReader.ContainerContext newContext1 = new BsonReader.ContainerContext(BsonType.Object);
                this.PushContext(newContext1);
                newContext1.Length = this.ReadInt32();
                break;

            case BsonType.Array:
                this.SetToken(JsonToken.StartArray);
                BsonReader.ContainerContext newContext2 = new BsonReader.ContainerContext(BsonType.Array);
                this.PushContext(newContext2);
                newContext2.Length = this.ReadInt32();
                break;

            case BsonType.Binary:
                BsonBinaryType binaryType;
                byte[]         b = this.ReadBinary(out binaryType);
                this.SetToken(JsonToken.Bytes, binaryType != BsonBinaryType.Uuid ? (object)b : (object)new Guid(b));
                break;

            case BsonType.Undefined:
                this.SetToken(JsonToken.Undefined);
                break;

            case BsonType.Oid:
                this.SetToken(JsonToken.Bytes, (object)this.ReadBytes(12));
                break;

            case BsonType.Boolean:
                this.SetToken(JsonToken.Boolean, (object)Convert.ToBoolean(this.ReadByte()));
                break;

            case BsonType.Date:
                DateTime dateTime1 = DateTimeUtils.ConvertJavaScriptTicksToDateTime(this.ReadInt64());
                DateTime dateTime2;
                switch (this.DateTimeKindHandling)
                {
                case DateTimeKind.Unspecified:
                    dateTime2 = DateTime.SpecifyKind(dateTime1, DateTimeKind.Unspecified);
                    break;

                case DateTimeKind.Local:
                    dateTime2 = dateTime1.ToLocalTime();
                    break;

                default:
                    dateTime2 = dateTime1;
                    break;
                }

                this.SetToken(JsonToken.Date, (object)dateTime2);
                break;

            case BsonType.Null:
                this.SetToken(JsonToken.Null);
                break;

            case BsonType.Regex:
                this.SetToken(JsonToken.String, (object)("/" + this.ReadString() + "/" + this.ReadString()));
                break;

            case BsonType.Reference:
                this.SetToken(JsonToken.StartObject);
                this._bsonReaderState = BsonReader.BsonReaderState.ReferenceStart;
                break;

            case BsonType.Code:
                this.SetToken(JsonToken.String, (object)this.ReadLengthString());
                break;

            case BsonType.CodeWScope:
                this.SetToken(JsonToken.StartObject);
                this._bsonReaderState = BsonReader.BsonReaderState.CodeWScopeStart;
                break;

            case BsonType.Integer:
                this.SetToken(JsonToken.Integer, (object)(long)this.ReadInt32());
                break;

            case BsonType.TimeStamp:
            case BsonType.Long:
                this.SetToken(JsonToken.Integer, (object)this.ReadInt64());
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(type), "Unexpected BsonType value: " + (object)type);
            }
        }
Example #4
0
 private void PushContext(BsonReader.ContainerContext newContext)
 {
     this._stack.Add(newContext);
     this._currentContext = newContext;
 }
Example #5
0
        private bool ReadNormal()
        {
            switch (this.CurrentState)
            {
            case JsonReader.State.Start:
                JsonToken newToken = !this._readRootValueAsArray ? JsonToken.StartObject : JsonToken.StartArray;
                int       num1     = !this._readRootValueAsArray ? 3 : 4;
                this.SetToken(newToken);
                BsonReader.ContainerContext newContext = new BsonReader.ContainerContext((BsonType)num1);
                this.PushContext(newContext);
                newContext.Length = this.ReadInt32();
                return(true);

            case JsonReader.State.Complete:
            case JsonReader.State.Closed:
                return(false);

            case JsonReader.State.Property:
                this.ReadType(this._currentElementType);
                return(true);

            case JsonReader.State.ObjectStart:
            case JsonReader.State.ArrayStart:
            case JsonReader.State.PostValue:
                BsonReader.ContainerContext currentContext = this._currentContext;
                if (currentContext == null)
                {
                    return(false);
                }
                int num2 = currentContext.Length - 1;
                if (currentContext.Position < num2)
                {
                    if (currentContext.Type == BsonType.Array)
                    {
                        this.ReadElement();
                        this.ReadType(this._currentElementType);
                        return(true);
                    }

                    this.SetToken(JsonToken.PropertyName, (object)this.ReadElement());
                    return(true);
                }

                if (currentContext.Position != num2)
                {
                    throw JsonReaderException.Create((JsonReader)this, "Read past end of current container context.");
                }
                if (this.ReadByte() != (byte)0)
                {
                    throw JsonReaderException.Create((JsonReader)this, "Unexpected end of object byte value.");
                }
                this.PopContext();
                if (this._currentContext != null)
                {
                    this.MovePosition(currentContext.Length);
                }
                this.SetToken(currentContext.Type == BsonType.Object ? JsonToken.EndObject : JsonToken.EndArray);
                return(true);

            case JsonReader.State.ConstructorStart:
            case JsonReader.State.Constructor:
            case JsonReader.State.Error:
            case JsonReader.State.Finished:
                return(false);

            default:
                throw new ArgumentOutOfRangeException();
            }
        }