changeset 141:0fa293bb1351 v2

fixed JSON writer
author cin
date Tue, 24 Feb 2015 00:54:22 +0300
parents f973c5df9972
children 2100965eb97f
files Implab/ICancellable.cs Implab/JSON/JSONElementContext.cs Implab/JSON/JSONWriter.cs
diffstat 3 files changed, 46 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- 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 {
--- 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
     }
 }
--- 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("]");
         }