Пример #1
0
 public JsonReader(
     JsonBuffer buffer
 )
 {
     this.buffer = buffer;
     this.context = new JsonReaderContext(null, ContextType.TopLevel);
 }
 internal JsonReaderContext(
     JsonReaderContext parentContext,
     ContextType contextType
 ) {
     this.parentContext = parentContext;
     this.contextType = contextType;
 }
Пример #3
0
 public JsonReaderContext Clone()
 {
     var clone = new JsonReaderContext();
     clone.parentContext = this.parentContext;
     clone.contextType = this.contextType;
     return clone;
 }
Пример #4
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the JsonReader class.
 /// </summary>
 /// <param name="buffer">The buffer.</param>
 /// <param name="settings">The reader settings.</param>
 public JsonReader(JsonBuffer buffer, JsonReaderSettings settings)
     : base(settings)
 {
     _buffer = buffer;
     _jsonReaderSettings = settings; // already frozen by base class
     _context = new JsonReaderContext(null, ContextType.TopLevel);
 }
Пример #5
0
 /// <summary>
 /// Initializes a new instance of the JsonReader class.
 /// </summary>
 /// <param name="buffer">The buffer.</param>
 public JsonReader(
     JsonBuffer buffer
     )
 {
     this.buffer  = buffer;
     this.context = new JsonReaderContext(null, ContextType.TopLevel);
 }
 internal JsonReaderContext(
     JsonReaderContext parentContext,
     ContextType contextType
     )
 {
     this.parentContext = parentContext;
     this.contextType   = contextType;
 }
Пример #7
0
 /// <summary>
 /// Initializes a new instance of the JsonReader class.
 /// </summary>
 /// <param name="buffer">The buffer.</param>
 /// <param name="settings">The reader settings.</param>
 public JsonReader(
     JsonBuffer buffer,
     JsonReaderSettings settings
 ) {
     this.buffer = buffer;
     this.settings = settings.Freeze();
     this.context = new JsonReaderContext(null, ContextType.TopLevel);
 }
 // constructors
 internal JsonReaderBookmark(BsonReaderState state, BsonType currentBsonType, string currentName, JsonReaderContext context, JsonToken currentToken, BsonValue currentValue, JsonToken pushedToken, int position)
     : base(state, currentBsonType, currentName)
 {
     this.context      = context.Clone();
     this.currentToken = currentToken;
     this.currentValue = currentValue;
     this.pushedToken  = pushedToken;
     this.position     = position;
 }
Пример #9
0
 /// <summary>
 /// Initializes a new instance of the JsonReader class.
 /// </summary>
 /// <param name="buffer">The buffer.</param>
 /// <param name="settings">The reader settings.</param>
 public JsonReader(
     JsonBuffer buffer,
     JsonReaderSettings settings
     )
 {
     this.buffer   = buffer;
     this.settings = settings.Freeze();
     this.context  = new JsonReaderContext(null, ContextType.TopLevel);
 }
 // constructors
 internal JsonReaderBookmark(BsonReaderState state, BsonType currentBsonType, string currentName, JsonReaderContext context, JsonToken currentToken, BsonValue currentValue, JsonToken pushedToken, int position)
     : base(state, currentBsonType, currentName)
 {
     _context = context.Clone();
     _currentToken = currentToken;
     _currentValue = currentValue;
     _pushedToken = pushedToken;
     _position = position;
 }
Пример #11
0
        /// <summary>
        /// Reads the start of a BSON document.
        /// </summary>
        public override void ReadStartDocument()
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            VerifyBsonType("ReadStartDocument", BsonType.Document);

            context = new JsonReaderContext(context, ContextType.Document);
            state   = BsonReaderState.Type;
        }
Пример #12
0
 /// <summary>
 /// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
 /// </summary>
 /// <returns>A string.</returns>
 public override string ReadJavaScriptWithScope()
 {
     if (disposed)
     {
         ThrowObjectDisposedException();
     }
     VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);
     context = new JsonReaderContext(context, ContextType.JavaScriptWithScope);
     state   = BsonReaderState.ScopeDocument;
     return(currentValue.AsString);
 }
Пример #13
0
        /// <summary>
        /// Reads the end of a BSON document from the reader.
        /// </summary>
        public override void ReadEndDocument()
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            if (
                context.ContextType != ContextType.Document &&
                context.ContextType != ContextType.ScopeDocument
                )
            {
                var message = string.Format("ReadEndDocument cannot be called when ContextType is: {0}", context.ContextType);
                throw new InvalidOperationException(message);
            }
            if (state == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfDocument if at end of document
            }
            if (state != BsonReaderState.EndOfDocument)
            {
                var message = string.Format("ReadEndDocument cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            context = context.PopContext();
            if (context != null && context.ContextType == ContextType.JavaScriptWithScope)
            {
                context = context.PopContext(); // JavaScriptWithScope
                VerifyToken("}");               // outermost closing bracket for JavaScriptWithScope
            }
            switch (context.ContextType)
            {
            case ContextType.Array: state = BsonReaderState.Type; break;

            case ContextType.Document: state = BsonReaderState.Type; break;

            case ContextType.TopLevel: state = BsonReaderState.Done; break;

            default: throw new BsonInternalException("Unexpected ContextType");
            }

            if (context.ContextType == ContextType.Array || context.ContextType == ContextType.Document)
            {
                var commaToken = PopToken();
                if (commaToken.Type != JsonTokenType.Comma)
                {
                    PushToken(commaToken);
                }
            }
        }
Пример #14
0
        /// <summary>
        /// Returns the reader to previously bookmarked position and state.
        /// </summary>
        /// <param name="bookmark">The bookmark.</param>
        public override void ReturnToBookmark(
            BsonReaderBookmark bookmark
            )
        {
            var jsonReaderBookmark = (JsonReaderBookmark)bookmark;

            state           = jsonReaderBookmark.State;
            currentBsonType = jsonReaderBookmark.CurrentBsonType;
            currentName     = jsonReaderBookmark.CurrentName;
            context         = jsonReaderBookmark.CloneContext();
            currentToken    = jsonReaderBookmark.CurrentToken;
            currentValue    = jsonReaderBookmark.CurrentValue;
            pushedToken     = jsonReaderBookmark.PushedToken;
            buffer.Position = jsonReaderBookmark.Position;
        }
Пример #15
0
        /// <summary>
        /// Returns the reader to previously bookmarked position and state.
        /// </summary>
        /// <param name="bookmark">The bookmark.</param>
        public override void ReturnToBookmark(
            BsonReaderBookmark bookmark
            )
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            var jsonReaderBookmark = (JsonReaderBookmark)bookmark;

            state           = jsonReaderBookmark.State;
            currentBsonType = jsonReaderBookmark.CurrentBsonType;
            currentName     = jsonReaderBookmark.CurrentName;
            context         = jsonReaderBookmark.CloneContext();
            currentToken    = jsonReaderBookmark.CurrentToken;
            currentValue    = jsonReaderBookmark.CurrentValue;
            pushedToken     = jsonReaderBookmark.PushedToken;
            buffer.Position = jsonReaderBookmark.Position;
        }
Пример #16
0
        /// <summary>
        /// Reads the end of a BSON array from the reader.
        /// </summary>
        public override void ReadEndArray()
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            if (context.ContextType != ContextType.Array)
            {
                var message = string.Format("ReadEndArray cannot be called when ContextType is: {0}", context.ContextType);
                throw new InvalidOperationException(message);
            }
            if (state == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfArray if at end of array
            }
            if (state != BsonReaderState.EndOfArray)
            {
                var message = string.Format("ReadEndArray cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            context = context.PopContext();
            switch (context.ContextType)
            {
            case ContextType.Array: state = BsonReaderState.Type; break;

            case ContextType.Document: state = BsonReaderState.Type; break;

            case ContextType.TopLevel: state = BsonReaderState.Done; break;

            default: throw new BsonInternalException("Unexpected ContextType");
            }

            if (context.ContextType == ContextType.Array || context.ContextType == ContextType.Document)
            {
                var commaToken = PopToken();
                if (commaToken.Type != JsonTokenType.Comma)
                {
                    PushToken(commaToken);
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Reads the end of a BSON array from the reader.
        /// </summary>
        public override void ReadEndArray()
        {
            if (disposed)
            {
                ThrowObjectDisposedException();
            }
            if (context.ContextType != ContextType.Array)
            {
                ThrowInvalidContextType("ReadEndArray", context.ContextType, ContextType.Array);
            }
            if (state == BsonReaderState.Type)
            {
                ReadBsonType(); // will set state to EndOfArray if at end of array
            }
            if (state != BsonReaderState.EndOfArray)
            {
                ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
            }

            context = context.PopContext();
            switch (context.ContextType)
            {
            case ContextType.Array: state = BsonReaderState.Type; break;

            case ContextType.Document: state = BsonReaderState.Type; break;

            case ContextType.TopLevel: state = BsonReaderState.Done; break;

            default: throw new BsonInternalException("Unexpected ContextType.");
            }

            if (context.ContextType == ContextType.Array || context.ContextType == ContextType.Document)
            {
                var commaToken = PopToken();
                if (commaToken.Type != JsonTokenType.Comma)
                {
                    PushToken(commaToken);
                }
            }
        }
Пример #18
0
 /// <summary>
 /// Returns the reader to previously bookmarked position and state.
 /// </summary>
 /// <param name="bookmark">The bookmark.</param>
 public override void ReturnToBookmark(
     BsonReaderBookmark bookmark
 ) {
     if (disposed) { ThrowObjectDisposedException(); }
     var jsonReaderBookmark = (JsonReaderBookmark) bookmark;
     state = jsonReaderBookmark.State;
     currentBsonType = jsonReaderBookmark.CurrentBsonType;
     currentName = jsonReaderBookmark.CurrentName;
     context = jsonReaderBookmark.CloneContext();
     currentToken = jsonReaderBookmark.CurrentToken;
     currentValue = jsonReaderBookmark.CurrentValue;
     pushedToken = jsonReaderBookmark.PushedToken;
     buffer.Position = jsonReaderBookmark.Position;
 }
Пример #19
0
        /// <summary>
        /// Reads the start of a BSON document.
        /// </summary>
        public override void ReadStartDocument() {
            if (disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadStartDocument", BsonType.Document);

            context = new JsonReaderContext(context, ContextType.Document);
            state = BsonReaderState.Type;
        }
Пример #20
0
 /// <summary>
 /// Reads a BSON JavaScript with scope from the reader (call ReadStartDocument next to read the scope).
 /// </summary>
 /// <returns>A string.</returns>
 public override string ReadJavaScriptWithScope() {
     if (disposed) { ThrowObjectDisposedException(); }
     VerifyBsonType("ReadJavaScriptWithScope", BsonType.JavaScriptWithScope);
     context = new JsonReaderContext(context, ContextType.JavaScriptWithScope);
     state = BsonReaderState.ScopeDocument;
     return currentValue.AsString;
 }
Пример #21
0
        /// <summary>
        /// Reads the end of a BSON document from the reader.
        /// </summary>
        public override void ReadEndDocument() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (
                context.ContextType != ContextType.Document &&
                context.ContextType != ContextType.ScopeDocument
            ) {
                ThrowInvalidContextType("ReadEndDocument", context.ContextType, ContextType.Document, ContextType.ScopeDocument);
            }
            if (state == BsonReaderState.Type) {
                ReadBsonType(); // will set state to EndOfDocument if at end of document
            }
            if (state != BsonReaderState.EndOfDocument) {
                ThrowInvalidState("ReadEndDocument", BsonReaderState.EndOfDocument);
            }

            context = context.PopContext();
            if (context != null && context.ContextType == ContextType.JavaScriptWithScope) {
                context = context.PopContext(); // JavaScriptWithScope
                VerifyToken("}"); // outermost closing bracket for JavaScriptWithScope
            }
            switch (context.ContextType) {
                case ContextType.Array: state = BsonReaderState.Type; break;
                case ContextType.Document: state = BsonReaderState.Type; break;
                case ContextType.TopLevel: state = BsonReaderState.Done; break;
                default: throw new BsonInternalException("Unexpected ContextType");
            }

            if (context.ContextType == ContextType.Array || context.ContextType == ContextType.Document) {
                var commaToken = PopToken();
                if (commaToken.Type != JsonTokenType.Comma) {
                    PushToken(commaToken);
                }
            }
        }
Пример #22
0
        /// <summary>
        /// Reads the end of a BSON array from the reader.
        /// </summary>
        public override void ReadEndArray() {
            if (disposed) { ThrowObjectDisposedException(); }
            if (context.ContextType != ContextType.Array) {
                ThrowInvalidContextType("ReadEndArray", context.ContextType, ContextType.Array);
            }
            if (state == BsonReaderState.Type) {
                ReadBsonType(); // will set state to EndOfArray if at end of array
            }
            if (state != BsonReaderState.EndOfArray) {
                ThrowInvalidState("ReadEndArray", BsonReaderState.EndOfArray);
            }

            context = context.PopContext();
            switch (context.ContextType) {
                case ContextType.Array: state = BsonReaderState.Type; break;
                case ContextType.Document: state = BsonReaderState.Type; break;
                case ContextType.TopLevel: state = BsonReaderState.Done; break;
                default: throw new BsonInternalException("Unexpected ContextType.");
            }

            if (context.ContextType == ContextType.Array || context.ContextType == ContextType.Document) {
                var commaToken = PopToken();
                if (commaToken.Type != JsonTokenType.Comma) {
                    PushToken(commaToken);
                }
            }
        }
Пример #23
0
        /// <summary>
        /// Reads the start of a BSON array.
        /// </summary>
        public override void ReadStartArray()
        {
            if (_disposed) { ThrowObjectDisposedException(); }
            VerifyBsonType("ReadStartArray", BsonType.Array);

            _context = new JsonReaderContext(_context, ContextType.Array);
            _state = BsonReaderState.Type;
        }
Пример #24
0
 internal JsonReaderContext(JsonReaderContext parentContext, ContextType contextType)
 {
     _parentContext = parentContext;
     _contextType   = contextType;
 }
 internal JsonReaderContext(JsonReaderContext parentContext, ContextType contextType)
 {
     _parentContext = parentContext;
     _contextType = contextType;
 }
Пример #26
0
 public override void ReturnToBookmark(
     BsonReaderBookmark bookmark
 )
 {
     var jsonReaderBookmark = (JsonReaderBookmark) bookmark;
     state = jsonReaderBookmark.State;
     currentBsonType = jsonReaderBookmark.CurrentBsonType;
     currentName = jsonReaderBookmark.CurrentName;
     context = jsonReaderBookmark.Context;
     currentToken = jsonReaderBookmark.CurrentToken;
     currentValue = jsonReaderBookmark.CurrentValue;
     pushedToken = jsonReaderBookmark.PushedToken;
     buffer.Position = jsonReaderBookmark.Position;
 }
Пример #27
0
        public override void ReadEndArray()
        {
            if (disposed) { ThrowObjectDisposedException(); }
            if (context.ContextType != ContextType.Array) {
                var message = string.Format("ReadEndArray cannot be called when ContextType is: {0}", context.ContextType);
                throw new InvalidOperationException(message);
            }
            if (state == BsonReaderState.Type) {
                ReadBsonType(); // will set state to EndOfArray if at end of array
            }
            if (state != BsonReaderState.EndOfArray) {
                var message = string.Format("ReadEndArray cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            context = context.PopContext();
            switch (context.ContextType) {
                case ContextType.Array: state = BsonReaderState.Type; break;
                case ContextType.Document: state = BsonReaderState.Type; break;
                case ContextType.TopLevel: state = BsonReaderState.Done; break;
                default: throw new BsonInternalException("Unexpected ContextType");
            }

            if (context.ContextType == ContextType.Array || context.ContextType == ContextType.Document) {
                var commaToken = PopToken();
                if (commaToken.Type != JsonTokenType.Comma) {
                    PushToken(commaToken);
                }
            }
        }