diff Implab/JSON/JSONWriter.cs @ 141:0fa293bb1351 v2

fixed JSON writer
author cin
date Tue, 24 Feb 2015 00:54:22 +0300
parents 2573b562e328
children 2100965eb97f
line wrap: on
line diff
--- 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("]");
         }