Mercurial > pub > ImplabNet
diff Implab/JSON/JSONWriter.cs @ 72:d67b95eddaf4 v2
promises refactoring
author | cin |
---|---|
date | Thu, 04 Sep 2014 18:47:12 +0400 |
parents | c0bf853aa04f |
children | ffd3702968c7 |
line wrap: on
line diff
--- a/Implab/JSON/JSONWriter.cs Wed Sep 03 18:34:02 2014 +0400 +++ b/Implab/JSON/JSONWriter.cs Thu Sep 04 18:47:12 2014 +0400 @@ -15,7 +15,8 @@ Context m_context; TextWriter m_writer; - bool m_indent; + readonly bool m_indent = true; + readonly int m_indentSize = 4; static readonly char [] _escapeBKS, _escapeFWD, @@ -43,13 +44,33 @@ m_writer = writer; } + public JSONWriter(TextWriter writer, bool indent) { + Safe.ArgumentNotNull(writer, "writer"); + + m_writer = writer; + m_indent = indent; + } + + void WriteIndent() { + if (m_indent) { + var indent = new char[m_contextStack.Count * m_indentSize + 1]; + indent[0] = '\n'; + for (int i = 1; i < indent.Length; i++) + indent[i] = ' '; + m_writer.Write(new String(indent)); + } else { + m_writer.Write(' '); + } + } + void WriteMemberName(string name) { Safe.ArgumentNotEmpty(name, "name"); if (m_context.element != JSONElementContext.Object) OperationNotApplicable("WriteMember"); if (m_context.needComma) - m_writer.Write(", "); - // TODO indent + m_writer.Write(","); + + WriteIndent(); m_context.needComma = true; Write(name); m_writer.Write(" : "); @@ -70,13 +91,12 @@ Write(value); } - - public void WriteValue(string value) { if (m_context.element != JSONElementContext.Array) OperationNotApplicable("WriteValue"); if (m_context.needComma) - m_writer.Write(", "); + m_writer.Write(","); + WriteIndent(); m_context.needComma = true; Write(value); @@ -86,9 +106,10 @@ if (m_context.element != JSONElementContext.Array) OperationNotApplicable("WriteValue"); if (m_context.needComma) - m_writer.Write(", "); + m_writer.Write(","); m_context.needComma = true; + WriteIndent(); Write(value); } @@ -96,9 +117,10 @@ if (m_context.element != JSONElementContext.Array) OperationNotApplicable("WriteValue"); if (m_context.needComma) - m_writer.Write(", "); + m_writer.Write(","); m_context.needComma = true; + WriteIndent(); Write(value); } @@ -106,13 +128,16 @@ if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array) OperationNotApplicable("BeginObject"); if (m_context.needComma) - m_writer.Write(", "); + m_writer.Write(","); + + WriteIndent(); + m_context.needComma = true; m_contextStack.Push(m_context); m_context = new Context { element = JSONElementContext.Object, needComma = false }; - m_writer.Write("{ "); + m_writer.Write("{"); } public void BeginObject(string name) { @@ -121,28 +146,31 @@ m_contextStack.Push(m_context); m_context = new Context { element = JSONElementContext.Object, needComma = false }; - m_writer.Write("{ "); + m_writer.Write("{"); } public void EndObject() { if (m_context.element != JSONElementContext.Object) OperationNotApplicable("EndArray"); - - m_writer.Write(" }"); + m_context = m_contextStack.Pop(); + WriteIndent(); + m_writer.Write("}"); } public void BeginArray() { if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array) throw new InvalidOperationException(); - if (m_context.needComma) - m_writer.Write(", "); + if (m_context.needComma) { + m_writer.Write(","); + + } m_context.needComma = true; + WriteIndent(); m_contextStack.Push(m_context); - m_context = new Context { element = JSONElementContext.Array, needComma = false }; - m_writer.Write("[ "); + m_writer.Write("["); } public void BeginArray(string name) { @@ -151,15 +179,16 @@ m_contextStack.Push(m_context); m_context = new Context { element = JSONElementContext.Array, needComma = false }; - m_writer.Write("[ "); + m_writer.Write("["); } public void EndArray() { if (m_context.element != JSONElementContext.Array) OperationNotApplicable("EndArray"); - m_writer.Write(" ]"); m_context = m_contextStack.Pop(); + WriteIndent(); + m_writer.Write("]"); } void Write(bool value) {