Пример #1
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the JsonWriter class.
 /// </summary>
 /// <param name="writer">A TextWriter.</param>
 /// <param name="settings">Optional JsonWriter settings.</param>
 public JsonWriter(TextWriter writer, JsonWriterSettings settings)
     : base(settings)
 {
     _textWriter = writer;
     _jsonWriterSettings = settings; // already frozen by base class
     _context = new JsonWriterContext(null, ContextType.TopLevel, "");
     State = BsonWriterState.Initial;
 }
Пример #2
0
 // constructors
 /// <summary>
 /// Initializes a new instance of the JsonWriter class.
 /// </summary>
 /// <param name="writer">A TextWriter.</param>
 /// <param name="settings">Optional JsonWriter settings.</param>
 public JsonWriter(TextWriter writer, JsonWriterSettings settings)
     : base(settings)
 {
     _textWriter         = writer;
     _jsonWriterSettings = settings; // already frozen by base class
     _context            = new JsonWriterContext(null, ContextType.TopLevel, "");
     State = BsonWriterState.Initial;
 }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the JsonWriter class.
 /// </summary>
 /// <param name="writer">A TextWriter.</param>
 /// <param name="settings">Optional JsonWriter settings.</param>
 public JsonWriter(
     TextWriter writer,
     JsonWriterSettings settings
 ) {
     this.textWriter = writer;
     this.settings = settings.Freeze();
     context = new JsonWriterContext(null, ContextType.TopLevel, "");
     state = BsonWriterState.Initial;
 }
Пример #4
0
 /// <summary>
 /// Initializes a new instance of the JsonWriter class.
 /// </summary>
 /// <param name="writer">A TextWriter.</param>
 /// <param name="settings">Optional JsonWriter settings.</param>
 public JsonWriter(
     TextWriter writer,
     JsonWriterSettings settings
     )
 {
     this.textWriter = writer;
     this.settings   = settings.Freeze();
     context         = new JsonWriterContext(null, ContextType.TopLevel, "");
     state           = BsonWriterState.Initial;
 }
 internal JsonWriterContext(
     JsonWriterContext parentContext,
     ContextType contextType,
     string indentChars
     )
 {
     this.parentContext = parentContext;
     this.contextType   = contextType;
     this.indentation   = (parentContext == null) ? indentChars : parentContext.Indentation + indentChars;
 }
Пример #6
0
 // public methods
 /// <summary>
 /// Closes the writer.
 /// </summary>
 public override void Close()
 {
     // Close can be called on Disposed objects
     if (State != BsonWriterState.Closed)
     {
         Flush();
         _context = null;
         State    = BsonWriterState.Closed;
     }
 }
 // public methods
 /// <summary>
 /// Closes the writer.
 /// </summary>
 public override void Close()
 {
     // Close can be called on Disposed objects
     if (State != BsonWriterState.Closed)
     {
         Flush();
         _context = null;
         State = BsonWriterState.Closed;
     }
 }
Пример #8
0
 /// <summary>
 /// Closes the writer.
 /// </summary>
 public override void Close() {
     // Close can be called on Disposed objects
     if (state != BsonWriterState.Closed) {
         Flush();
         if (settings.CloseOutput) {
             textWriter.Close();
         }
         context = null;
         state = BsonWriterState.Closed;
     }
 }
Пример #9
0
        /// <summary>
        /// Initializes a new instance of the JsonWriter class.
        /// </summary>
        /// <param name="writer">A TextWriter.</param>
        /// <param name="settings">Optional JsonWriter settings.</param>
        public JsonWriter(TextWriter writer, JsonWriterSettings settings)
            : base(settings)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            _textWriter = writer;
            _context    = new JsonWriterContext(null, ContextType.TopLevel, "");
            State       = BsonWriterState.Initial;
        }
Пример #10
0
        /// <summary>
        /// Initializes a new instance of the JsonWriter class.
        /// </summary>
        /// <param name="writer">A TextWriter.</param>
        /// <param name="settings">Optional JsonWriter settings.</param>
        public JsonWriter(TextWriter writer, JsonWriterSettings settings)
            : base(settings)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            _textWriter = writer;
            _jsonWriterSettings = settings; // already frozen by base class
            _context = new JsonWriterContext(null, ContextType.TopLevel, "");
            State = BsonWriterState.Initial;
        }
Пример #11
0
 /// <summary>
 /// Closes the writer.
 /// </summary>
 public override void Close()
 {
     // Close can be called on Disposed objects
     if (state != BsonWriterState.Closed)
     {
         Flush();
         if (settings.CloseOutput)
         {
             textWriter.Close();
         }
         context = null;
         state   = BsonWriterState.Closed;
     }
 }
Пример #12
0
        /// <summary>
        /// Writes the end of a BSON array to the writer.
        /// </summary>
        public override void WriteEndArray()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (state != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteEndArray", BsonWriterState.Value);
            }

            textWriter.Write("]");

            context = context.ParentContext;
            state   = GetNextState();
        }
Пример #13
0
        /// <summary>
        /// Writes the start of a BSON array to the writer.
        /// </summary>
        public override void WriteStartArray()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial)
            {
                ThrowInvalidState("WriteStartArray", BsonWriterState.Value, BsonWriterState.Initial);
            }

            WriteNameHelper(name);
            textWriter.Write("[");

            context = new JsonWriterContext(context, ContextType.Array, settings.IndentChars);
            state   = BsonWriterState.Value;
        }
Пример #14
0
        /// <summary>
        /// Writes the end of a BSON array to the writer.
        /// </summary>
        public override void WriteEndArray()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (state != BsonWriterState.Value)
            {
                var message = string.Format("WriteEndArray cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            textWriter.Write("]");

            context = context.ParentContext;
            state   = GetNextState();
        }
Пример #15
0
        /// <summary>
        /// Writes the end of a BSON document to the writer.
        /// </summary>
        public override void WriteEndDocument()
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (State != BsonWriterState.Name)
            {
                ThrowInvalidState("WriteEndDocument", BsonWriterState.Name);
            }

            base.WriteEndDocument();
            if (_jsonWriterSettings.Indent && _context.HasElements)
            {
                _textWriter.Write(_jsonWriterSettings.NewLineChars);
                if (_context.ParentContext != null)
                {
                    _textWriter.Write(_context.ParentContext.Indentation);
                }
                _textWriter.Write("}");
            }
            else
            {
                _textWriter.Write(" }");
            }

            if (_context.ContextType == ContextType.ScopeDocument)
            {
                _context = _context.ParentContext;
                WriteEndDocument();
            }
            else
            {
                _context = _context.ParentContext;
            }

            if (_context == null)
            {
                State = BsonWriterState.Done;
            }
            else
            {
                State = GetNextState();
            }
        }
Пример #16
0
        /// <summary>
        /// Writes the end of a BSON document to the writer.
        /// </summary>
        public override void WriteEndDocument()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (state != BsonWriterState.Name)
            {
                var message = string.Format("WriteEndDocument cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            if (settings.Indent && context.HasElements)
            {
                textWriter.Write(settings.NewLineChars);
                if (context.ParentContext != null)
                {
                    textWriter.Write(context.ParentContext.Indentation);
                }
                textWriter.Write("}");
            }
            else
            {
                textWriter.Write(" }");
            }

            if (context.ContextType == ContextType.ScopeDocument)
            {
                context = context.ParentContext;
                WriteEndDocument();
            }
            else
            {
                context = context.ParentContext;
            }

            if (context == null)
            {
                state = BsonWriterState.Done;
            }
            else
            {
                state = GetNextState();
            }
        }
Пример #17
0
        /// <summary>
        /// Writes the start of a BSON array to the writer.
        /// </summary>
        public override void WriteStartArray()
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
            {
                ThrowInvalidState("WriteStartArray", BsonWriterState.Value, BsonWriterState.Initial);
            }

            base.WriteStartArray();
            WriteNameHelper(Name);
            _textWriter.Write("[");

            _context = new JsonWriterContext(_context, ContextType.Array, _jsonWriterSettings.IndentChars);
            State    = BsonWriterState.Value;
        }
Пример #18
0
        /// <summary>
        /// Writes the start of a BSON array to the writer.
        /// </summary>
        public override void WriteStartArray()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial)
            {
                var message = string.Format("WriteStartArray cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            WriteNameHelper(name);
            textWriter.Write("[");

            context = new JsonWriterContext(context, ContextType.Array, settings.IndentChars);
            state   = BsonWriterState.Value;
        }
Пример #19
0
        /// <summary>
        /// Writes the start of a BSON document to the writer.
        /// </summary>
        public override void WriteStartDocument()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial && state != BsonWriterState.ScopeDocument)
            {
                ThrowInvalidState("WriteStartDocument", BsonWriterState.Value, BsonWriterState.Initial, BsonWriterState.ScopeDocument);
            }

            if (state == BsonWriterState.Value || state == BsonWriterState.ScopeDocument)
            {
                WriteNameHelper(name);
            }
            textWriter.Write("{");

            var contextType = (state == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;

            context = new JsonWriterContext(context, contextType, settings.IndentChars);
            state   = BsonWriterState.Name;
        }
Пример #20
0
        /// <summary>
        /// Writes the start of a BSON document to the writer.
        /// </summary>
        public override void WriteStartDocument()
        {
            if (disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial && state != BsonWriterState.ScopeDocument)
            {
                var message = string.Format("WriteStartDocument cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            if (state == BsonWriterState.Value || state == BsonWriterState.ScopeDocument)
            {
                WriteNameHelper(name);
            }
            textWriter.Write("{");

            var contextType = (state == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;

            context = new JsonWriterContext(context, contextType, settings.IndentChars);
            state   = BsonWriterState.Name;
        }
Пример #21
0
        /// <summary>
        /// Writes the start of a BSON document to the writer.
        /// </summary>
        public override void WriteStartDocument()
        {
            if (Disposed)
            {
                throw new ObjectDisposedException("JsonWriter");
            }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial && State != BsonWriterState.ScopeDocument)
            {
                ThrowInvalidState("WriteStartDocument", BsonWriterState.Value, BsonWriterState.Initial, BsonWriterState.ScopeDocument);
            }

            base.WriteStartDocument();
            if (State == BsonWriterState.Value || State == BsonWriterState.ScopeDocument)
            {
                WriteNameHelper(Name);
            }
            _textWriter.Write("{");

            var contextType = (State == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;

            _context = new JsonWriterContext(_context, contextType, _jsonWriterSettings.IndentChars);
            State    = BsonWriterState.Name;
        }
Пример #22
0
        /// <summary>
        /// Writes the end of a BSON array to the writer.
        /// </summary>
        public override void WriteEndArray()
        {
            if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (State != BsonWriterState.Value)
            {
                ThrowInvalidState("WriteEndArray", BsonWriterState.Value);
            }

            base.WriteEndArray();
            _textWriter.Write("]");

            _context = _context.ParentContext;
            State = GetNextState();
        }
Пример #23
0
        /// <summary>
        /// Writes the start of a BSON array to the writer.
        /// </summary>
        public override void WriteStartArray() {
            if (disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial) {
                ThrowInvalidState("WriteStartArray", BsonWriterState.Value, BsonWriterState.Initial);
            }

            WriteNameHelper(name);
            textWriter.Write("[");

            context = new JsonWriterContext(context, ContextType.Array, settings.IndentChars);
            state = BsonWriterState.Value;
        }
Пример #24
0
        /// <summary>
        /// Writes the start of a BSON document to the writer.
        /// </summary>
        public override void WriteStartDocument() {
            if (disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial && state != BsonWriterState.ScopeDocument) {
                ThrowInvalidState("WriteStartDocument", BsonWriterState.Value, BsonWriterState.Initial, BsonWriterState.ScopeDocument);
            }

            if (state == BsonWriterState.Value || state == BsonWriterState.ScopeDocument) {
                WriteNameHelper(name);
            }
            textWriter.Write("{");

            var contextType = (state == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
            context = new JsonWriterContext(context, contextType, settings.IndentChars);
            state = BsonWriterState.Name;
        }
 // constructors
 internal JsonWriterContext(JsonWriterContext parentContext, ContextType contextType, string indentChars)
 {
     _parentContext = parentContext;
     _contextType = contextType;
     _indentation = (parentContext == null) ? indentChars : parentContext.Indentation + indentChars;
 }
Пример #26
0
        public override void WriteEndArray()
        {
            if (disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (state != BsonWriterState.Value) {
                var message = string.Format("WriteEndArray cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            textWriter.Write("]");

            context = context.ParentContext;
            state = GetNextState();
        }
Пример #27
0
        public override void WriteEndDocument()
        {
            if (disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (state != BsonWriterState.Name) {
                var message = string.Format("WriteEndDocument cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            if (settings.Indent && context.HasElements) {
                textWriter.Write(settings.NewLineChars);
                if (context.ParentContext != null) {
                    textWriter.Write(context.ParentContext.Indentation);
                }
                textWriter.Write("}");
            } else {
                textWriter.Write(" }");
            }

            if (context.ContextType == ContextType.ScopeDocument) {
                context = context.ParentContext;
                WriteEndDocument();
            } else {
                context = context.ParentContext;
            }

            if (context == null) {
                state = BsonWriterState.Done;
            } else {
                state = GetNextState();
            }
        }
Пример #28
0
        public override void WriteStartArray()
        {
            if (disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial) {
                var message = string.Format("WriteStartArray cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            WriteNameHelper(name);
            textWriter.Write("[");

            context = new JsonWriterContext(context, ContextType.Array, settings.IndentChars);
            state = BsonWriterState.Value;
        }
Пример #29
0
        public override void WriteStartDocument()
        {
            if (disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (state != BsonWriterState.Value && state != BsonWriterState.Initial && state != BsonWriterState.ScopeDocument) {
                var message = string.Format("WriteStartDocument cannot be called when State is: {0}", state);
                throw new InvalidOperationException(message);
            }

            if (state == BsonWriterState.Value || state == BsonWriterState.ScopeDocument) {
                WriteNameHelper(name);
            }
            textWriter.Write("{");

            var contextType = (state == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
            context = new JsonWriterContext(context, contextType, settings.IndentChars);
            state = BsonWriterState.Name;
        }
Пример #30
0
        /// <summary>
        /// Writes the end of a BSON document to the writer.
        /// </summary>
        public override void WriteEndDocument()
        {
            if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (State != BsonWriterState.Name)
            {
                ThrowInvalidState("WriteEndDocument", BsonWriterState.Name);
            }

            base.WriteEndDocument();
            if (_jsonWriterSettings.Indent && _context.HasElements)
            {
                _textWriter.Write(_jsonWriterSettings.NewLineChars);
                if (_context.ParentContext != null)
                {
                    _textWriter.Write(_context.ParentContext.Indentation);
                }
                _textWriter.Write("}");
            }
            else
            {
                _textWriter.Write(" }");
            }

            if (_context.ContextType == ContextType.ScopeDocument)
            {
                _context = _context.ParentContext;
                WriteEndDocument();
            }
            else
            {
                _context = _context.ParentContext;
            }

            if (_context == null)
            {
                State = BsonWriterState.Done;
            }
            else
            {
                State = GetNextState();
            }
        }
Пример #31
0
        /// <summary>
        /// Writes the start of a BSON array to the writer.
        /// </summary>
        public override void WriteStartArray()
        {
            if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial)
            {
                ThrowInvalidState("WriteStartArray", BsonWriterState.Value, BsonWriterState.Initial);
            }

            base.WriteStartArray();
            WriteNameHelper(Name);
            _textWriter.Write("[");

            _context = new JsonWriterContext(_context, ContextType.Array, _jsonWriterSettings.IndentChars);
            State = BsonWriterState.Value;
        }
Пример #32
0
        /// <summary>
        /// Writes the start of a BSON document to the writer.
        /// </summary>
        public override void WriteStartDocument()
        {
            if (Disposed) { throw new ObjectDisposedException("JsonWriter"); }
            if (State != BsonWriterState.Value && State != BsonWriterState.Initial && State != BsonWriterState.ScopeDocument)
            {
                ThrowInvalidState("WriteStartDocument", BsonWriterState.Value, BsonWriterState.Initial, BsonWriterState.ScopeDocument);
            }

            base.WriteStartDocument();
            if (State == BsonWriterState.Value || State == BsonWriterState.ScopeDocument)
            {
                WriteNameHelper(Name);
            }
            _textWriter.Write("{");

            var contextType = (State == BsonWriterState.ScopeDocument) ? ContextType.ScopeDocument : ContextType.Document;
            _context = new JsonWriterContext(_context, contextType, _jsonWriterSettings.IndentChars);
            State = BsonWriterState.Name;
        }