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("]"); }