##// END OF EJS Templates
fixed JSON writer
cin -
r141:0fa293bb1351 v2
parent child
Show More
@@ -1,7 +1,4
1 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 2
6 3 namespace Implab {
7 4 public interface ICancellable {
@@ -11,6 +11,7 namespace Implab.JSON {
11 11 public enum JSONElementContext {
12 12 None,
13 13 Object,
14 Array
14 Array,
15 Closed
15 16 }
16 17 }
@@ -87,36 +87,54 namespace Implab.JSON {
87 87 }
88 88
89 89 public void WriteValue(string value) {
90 if (m_context.element != JSONElementContext.Array)
90 if (m_context.element == JSONElementContext.Array) {
91
92 if (m_context.needComma)
93 m_writer.Write(",");
94 WriteIndent();
95 m_context.needComma = true;
96
97 Write(value);
98 } else if (m_context.element == JSONElementContext.None) {
99 Write(value);
100 m_context.element = JSONElementContext.Closed;
101 } else {
91 102 OperationNotApplicable("WriteValue");
92 if (m_context.needComma)
93 m_writer.Write(",");
94 WriteIndent();
95 m_context.needComma = true;
96
97 Write(value);
103 }
98 104 }
99 105
100 106 public void WriteValue(bool value) {
101 if (m_context.element != JSONElementContext.Array)
107 if (m_context.element == JSONElementContext.Array) {
108
109 if (m_context.needComma)
110 m_writer.Write(",");
111 WriteIndent();
112 m_context.needComma = true;
113
114 Write(value);
115 } else if (m_context.element == JSONElementContext.None) {
116 Write(value);
117 m_context.element = JSONElementContext.Closed;
118 } else {
102 119 OperationNotApplicable("WriteValue");
103 if (m_context.needComma)
104 m_writer.Write(",");
105 m_context.needComma = true;
106
107 WriteIndent();
108 Write(value);
120 }
109 121 }
110 122
111 123 public void WriteValue(double value) {
112 if (m_context.element != JSONElementContext.Array)
124 if (m_context.element == JSONElementContext.Array) {
125
126 if (m_context.needComma)
127 m_writer.Write(",");
128 WriteIndent();
129 m_context.needComma = true;
130
131 Write(value);
132 } else if (m_context.element == JSONElementContext.None) {
133 Write(value);
134 m_context.element = JSONElementContext.Closed;
135 } else {
113 136 OperationNotApplicable("WriteValue");
114 if (m_context.needComma)
115 m_writer.Write(",");
116 m_context.needComma = true;
117
118 WriteIndent();
119 Write(value);
137 }
120 138 }
121 139
122 140 public void BeginObject() {
@@ -146,9 +164,11 namespace Implab.JSON {
146 164
147 165 public void EndObject() {
148 166 if (m_context.element != JSONElementContext.Object)
149 OperationNotApplicable("EndArray");
167 OperationNotApplicable("EndObject");
150 168
151 169 m_context = m_contextStack.Pop();
170 if (m_contextStack.Count == 0)
171 m_context.element = JSONElementContext.Closed;
152 172 WriteIndent();
153 173 m_writer.Write("}");
154 174 }
@@ -182,6 +202,8 namespace Implab.JSON {
182 202 OperationNotApplicable("EndArray");
183 203
184 204 m_context = m_contextStack.Pop();
205 if (m_contextStack.Count == 0)
206 m_context.element = JSONElementContext.Closed;
185 207 WriteIndent();
186 208 m_writer.Write("]");
187 209 }
General Comments 0
You need to be logged in to leave comments. Login now