##// 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)
91 OperationNotApplicable("WriteValue");
90 if (m_context.element == JSONElementContext.Array) {
91
92 92 if (m_context.needComma)
93 93 m_writer.Write(",");
94 94 WriteIndent();
95 95 m_context.needComma = true;
96 96
97 97 Write(value);
98 } else if (m_context.element == JSONElementContext.None) {
99 Write(value);
100 m_context.element = JSONElementContext.Closed;
101 } else {
102 OperationNotApplicable("WriteValue");
103 }
98 104 }
99 105
100 106 public void WriteValue(bool value) {
101 if (m_context.element != JSONElementContext.Array)
102 OperationNotApplicable("WriteValue");
107 if (m_context.element == JSONElementContext.Array) {
108
103 109 if (m_context.needComma)
104 110 m_writer.Write(",");
111 WriteIndent();
105 112 m_context.needComma = true;
106 113
107 WriteIndent();
114 Write(value);
115 } else if (m_context.element == JSONElementContext.None) {
108 116 Write(value);
117 m_context.element = JSONElementContext.Closed;
118 } else {
119 OperationNotApplicable("WriteValue");
120 }
109 121 }
110 122
111 123 public void WriteValue(double value) {
112 if (m_context.element != JSONElementContext.Array)
113 OperationNotApplicable("WriteValue");
124 if (m_context.element == JSONElementContext.Array) {
125
114 126 if (m_context.needComma)
115 127 m_writer.Write(",");
128 WriteIndent();
116 129 m_context.needComma = true;
117 130
118 WriteIndent();
131 Write(value);
132 } else if (m_context.element == JSONElementContext.None) {
119 133 Write(value);
134 m_context.element = JSONElementContext.Closed;
135 } else {
136 OperationNotApplicable("WriteValue");
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