public BsonJsonWriter(
     TextWriter writer,
     BsonJsonWriterSettings settings
 )
 {
     this.textWriter = writer;
     this.settings = settings;
     context = new BsonJsonWriterContext(null, BsonWriteState.Initial, "");
 }
 internal BsonJsonWriterContext(
     BsonJsonWriterContext parentContext,
     BsonWriteState writeState,
     string indentation
 )
 {
     this.parentContext = parentContext;
     this.writeState = writeState;
     this.indentation = indentation;
 }
 public override void Close()
 {
     // Close can be called on Disposed objects
     if (context.WriteState != BsonWriteState.Closed) {
         Flush();
         if (settings.CloseOutput) {
             textWriter.Close();
         }
         context = new BsonJsonWriterContext(null, BsonWriteState.Closed, "");
     }
 }
 private void WriteDocumentName(
     string name,
     BsonWriteState documentType
 )
 {
     WriteName(name);
     context = new BsonJsonWriterContext(context, documentType, context.Indentation + settings.IndentChars);
     context = new BsonJsonWriterContext(context, BsonWriteState.StartDocument, null);
 }
 public override void WriteStartDocument()
 {
     if (disposed) { throw new ObjectDisposedException("BsonJsonWriter"); }
     if (context.WriteState == BsonWriteState.StartDocument) {
         context = context.ParentContext;
     } else if (context.WriteState == BsonWriteState.Initial || context.WriteState == BsonWriteState.Done) {
         context = new BsonJsonWriterContext(context, BsonWriteState.Document, context.Indentation + settings.IndentChars);
     } else {
         throw new InvalidOperationException("WriteStartDocument can only be called when WriteState is Initial, StartDocument or Done");
     }
     if (context.WriteState == BsonWriteState.Array) {
         textWriter.Write("[");
     } else {
         textWriter.Write("{");
     }
 }
        public override void WriteEndDocument()
        {
            if (disposed) { throw new ObjectDisposedException("BsonJsonWriter"); }
            if ((context.WriteState & BsonWriteState.Document) == 0) {
                throw new InvalidOperationException("WriteEndDocument can only be called when WriteState is one of the document states");
            }
            if (context.WriteState == BsonWriteState.Array) {
                textWriter.Write("]");
            } else {
                if (settings.Indent && context.HasElements) {
                    textWriter.Write(settings.NewLineChars);
                    textWriter.Write(context.ParentContext.Indentation);
                    textWriter.Write("}");
                } else {
                    textWriter.Write(" }");
                }
            }
            context = context.ParentContext;

            if (context.WriteState == BsonWriteState.JavaScriptWithScope) {
                WriteEndDocument();
            }

            if (context.WriteState == BsonWriteState.Initial) {
                context = new BsonJsonWriterContext(null, BsonWriteState.Done, "");
            }
        }