# HG changeset patch # User cin # Date 1424728462 -10800 # Node ID 0fa293bb1351e448bde0470e9175c483ecc68993 # Parent f973c5df997241dfd2ef297191df1589f07ff091 fixed JSON writer diff -r f973c5df9972 -r 0fa293bb1351 Implab/ICancellable.cs --- a/Implab/ICancellable.cs Fri Feb 20 15:58:34 2015 +0300 +++ b/Implab/ICancellable.cs Tue Feb 24 00:54:22 2015 +0300 @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Implab { public interface ICancellable { diff -r f973c5df9972 -r 0fa293bb1351 Implab/JSON/JSONElementContext.cs --- a/Implab/JSON/JSONElementContext.cs Fri Feb 20 15:58:34 2015 +0300 +++ b/Implab/JSON/JSONElementContext.cs Tue Feb 24 00:54:22 2015 +0300 @@ -11,6 +11,7 @@ public enum JSONElementContext { None, Object, - Array + Array, + Closed } } diff -r f973c5df9972 -r 0fa293bb1351 Implab/JSON/JSONWriter.cs --- a/Implab/JSON/JSONWriter.cs Fri Feb 20 15:58:34 2015 +0300 +++ b/Implab/JSON/JSONWriter.cs Tue Feb 24 00:54:22 2015 +0300 @@ -87,36 +87,54 @@ } public void WriteValue(string value) { - if (m_context.element != JSONElementContext.Array) + if (m_context.element == JSONElementContext.Array) { + + if (m_context.needComma) + m_writer.Write(","); + WriteIndent(); + m_context.needComma = true; + + Write(value); + } else if (m_context.element == JSONElementContext.None) { + Write(value); + m_context.element = JSONElementContext.Closed; + } else { OperationNotApplicable("WriteValue"); - if (m_context.needComma) - m_writer.Write(","); - WriteIndent(); - m_context.needComma = true; - - Write(value); + } } public void WriteValue(bool value) { - if (m_context.element != JSONElementContext.Array) + if (m_context.element == JSONElementContext.Array) { + + if (m_context.needComma) + m_writer.Write(","); + WriteIndent(); + m_context.needComma = true; + + Write(value); + } else if (m_context.element == JSONElementContext.None) { + Write(value); + m_context.element = JSONElementContext.Closed; + } else { OperationNotApplicable("WriteValue"); - if (m_context.needComma) - m_writer.Write(","); - m_context.needComma = true; - - WriteIndent(); - Write(value); + } } public void WriteValue(double value) { - if (m_context.element != JSONElementContext.Array) + if (m_context.element == JSONElementContext.Array) { + + if (m_context.needComma) + m_writer.Write(","); + WriteIndent(); + m_context.needComma = true; + + Write(value); + } else if (m_context.element == JSONElementContext.None) { + Write(value); + m_context.element = JSONElementContext.Closed; + } else { OperationNotApplicable("WriteValue"); - if (m_context.needComma) - m_writer.Write(","); - m_context.needComma = true; - - WriteIndent(); - Write(value); + } } public void BeginObject() { @@ -146,9 +164,11 @@ public void EndObject() { if (m_context.element != JSONElementContext.Object) - OperationNotApplicable("EndArray"); + OperationNotApplicable("EndObject"); m_context = m_contextStack.Pop(); + if (m_contextStack.Count == 0) + m_context.element = JSONElementContext.Closed; WriteIndent(); m_writer.Write("}"); } @@ -182,6 +202,8 @@ OperationNotApplicable("EndArray"); m_context = m_contextStack.Pop(); + if (m_contextStack.Count == 0) + m_context.element = JSONElementContext.Closed; WriteIndent(); m_writer.Write("]"); }