# HG changeset patch # User cin # Date 2015-02-23 21:54:22 # Node ID 0fa293bb1351e448bde0470e9175c483ecc68993 # Parent f973c5df997241dfd2ef297191df1589f07ff091 fixed JSON writer diff --git a/Implab/ICancellable.cs b/Implab/ICancellable.cs --- a/Implab/ICancellable.cs +++ b/Implab/ICancellable.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace Implab { public interface ICancellable { diff --git a/Implab/JSON/JSONElementContext.cs b/Implab/JSON/JSONElementContext.cs --- a/Implab/JSON/JSONElementContext.cs +++ b/Implab/JSON/JSONElementContext.cs @@ -11,6 +11,7 @@ namespace Implab.JSON { public enum JSONElementContext { None, Object, - Array + Array, + Closed } } diff --git a/Implab/JSON/JSONWriter.cs b/Implab/JSON/JSONWriter.cs --- a/Implab/JSON/JSONWriter.cs +++ b/Implab/JSON/JSONWriter.cs @@ -87,36 +87,54 @@ namespace Implab.JSON { } 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 @@ namespace Implab.JSON { 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 @@ namespace Implab.JSON { OperationNotApplicable("EndArray"); m_context = m_contextStack.Pop(); + if (m_contextStack.Count == 0) + m_context.element = JSONElementContext.Closed; WriteIndent(); m_writer.Write("]"); }