annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System.IO;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 namespace Implab.JSON {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6 public class JSONWriter {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 struct Context {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8 public bool needComma;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 public JSONElementContext element;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 Stack<Context> m_contextStack = new Stack<Context>();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 Context m_context;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 TextWriter m_writer;
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
15 readonly bool m_indent = true;
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
16 readonly int m_indentSize = 4;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 static readonly char [] _escapeBKS,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19 _escapeFWD,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 _escapeCR,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 _escapeNL,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 _escapeTAB,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23 _escapeBSLASH,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
24 _escapeQ;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 static JSONWriter() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 _escapeBKS = "\\b".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28 _escapeFWD = "\\f".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 _escapeCR = "\\r".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 _escapeNL = "\\n".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 _escapeTAB = "\\t".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32 _escapeBSLASH = "\\\\".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 _escapeQ = "\\\"".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36 public JSONWriter(TextWriter writer) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 Safe.ArgumentNotNull(writer, "writer");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 m_writer = writer;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
42 public JSONWriter(TextWriter writer, bool indent) {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
43 Safe.ArgumentNotNull(writer, "writer");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
44
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
45 m_writer = writer;
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
46 m_indent = indent;
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
47 }
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
48
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
49 void WriteIndent() {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
50 if (m_indent) {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
51 var indent = new char[m_contextStack.Count * m_indentSize + 1];
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
52 indent[0] = '\n';
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
53 for (int i = 1; i < indent.Length; i++)
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
54 indent[i] = ' ';
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
55 m_writer.Write(new String(indent));
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
56 } else {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
57 m_writer.Write(' ');
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
58 }
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
59 }
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
60
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 void WriteMemberName(string name) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62 Safe.ArgumentNotEmpty(name, "name");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63 if (m_context.element != JSONElementContext.Object)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64 OperationNotApplicable("WriteMember");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65 if (m_context.needComma)
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
66 m_writer.Write(",");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
67
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
68 WriteIndent();
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
69 m_context.needComma = true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
70 Write(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
71 m_writer.Write(" : ");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
72 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
73
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
74 public void WriteValue(string name, string value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
75 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
76 Write(value);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
77 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
78
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
79 public void WriteValue(string name, bool value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
80 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
81 Write(value);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
82 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
83
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
84 public void WriteValue(string name, double value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
85 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
86 Write(value);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
87 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
88
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
89 public void WriteValue(string value) {
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
90 if (m_context.element == JSONElementContext.Array) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
91
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
92 if (m_context.needComma)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
93 m_writer.Write(",");
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
94 WriteIndent();
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
95 m_context.needComma = true;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
96
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
97 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
98 } else if (m_context.element == JSONElementContext.None) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
99 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
100 m_context.element = JSONElementContext.Closed;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
101 } else {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
102 OperationNotApplicable("WriteValue");
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
103 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
104 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
105
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
106 public void WriteValue(bool value) {
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
107 if (m_context.element == JSONElementContext.Array) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
108
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
109 if (m_context.needComma)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
110 m_writer.Write(",");
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
111 WriteIndent();
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
112 m_context.needComma = true;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
113
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
114 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
115 } else if (m_context.element == JSONElementContext.None) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
116 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
117 m_context.element = JSONElementContext.Closed;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
118 } else {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
119 OperationNotApplicable("WriteValue");
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
120 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
121 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
122
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
123 public void WriteValue(double value) {
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
124 if (m_context.element == JSONElementContext.Array) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
125
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
126 if (m_context.needComma)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
127 m_writer.Write(",");
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
128 WriteIndent();
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
129 m_context.needComma = true;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
130
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
131 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
132 } else if (m_context.element == JSONElementContext.None) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
133 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
134 m_context.element = JSONElementContext.Closed;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
135 } else {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
136 OperationNotApplicable("WriteValue");
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
137 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
138 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
139
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
140 public void BeginObject() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
141 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
142 OperationNotApplicable("BeginObject");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
143 if (m_context.needComma)
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
144 m_writer.Write(",");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
145
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
146 WriteIndent();
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
147
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
148 m_context.needComma = true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
149
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
150 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
151
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
152 m_context = new Context { element = JSONElementContext.Object, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
153 m_writer.Write("{");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
154 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
155
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
156 public void BeginObject(string name) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
157 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
158
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
159 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
160
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
161 m_context = new Context { element = JSONElementContext.Object, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
162 m_writer.Write("{");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
163 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
164
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
165 public void EndObject() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
166 if (m_context.element != JSONElementContext.Object)
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
167 OperationNotApplicable("EndObject");
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
168
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
169 m_context = m_contextStack.Pop();
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
170 if (m_contextStack.Count == 0)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
171 m_context.element = JSONElementContext.Closed;
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
172 WriteIndent();
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
173 m_writer.Write("}");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
174 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
175
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
176 public void BeginArray() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
177 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
178 throw new InvalidOperationException();
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
179 if (m_context.needComma) {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
180 m_writer.Write(",");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
181
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
182 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
183 m_context.needComma = true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
184
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
185 WriteIndent();
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
186 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
187 m_context = new Context { element = JSONElementContext.Array, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
188 m_writer.Write("[");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
189 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
190
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
191 public void BeginArray(string name) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
192 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
193
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
194 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
195
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
196 m_context = new Context { element = JSONElementContext.Array, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
197 m_writer.Write("[");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
198 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
199
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
200 public void EndArray() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
201 if (m_context.element != JSONElementContext.Array)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
202 OperationNotApplicable("EndArray");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
203
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
204 m_context = m_contextStack.Pop();
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
205 if (m_contextStack.Count == 0)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
206 m_context.element = JSONElementContext.Closed;
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
207 WriteIndent();
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
208 m_writer.Write("]");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
209 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
210
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
211 void Write(bool value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
212 m_writer.Write(value ? "true" : "false");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
213 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
214
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
215
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
216 void Write(string value) {
115
ffd3702968c7 fixed JSON writer
cin
parents: 72
diff changeset
217 if (value == null) {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
218 m_writer.Write("null");
115
ffd3702968c7 fixed JSON writer
cin
parents: 72
diff changeset
219 return;
ffd3702968c7 fixed JSON writer
cin
parents: 72
diff changeset
220 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
221
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
222 var chars = value.ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
223 m_writer.Write('"');
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
224
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 118
diff changeset
225 // Analysis disable once ForCanBeConvertedToForeach
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
226 for (int i = 0; i < chars.Length; i++) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
227 var ch = chars[i];
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
228
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
229 switch (ch) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
230 case '\b':
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
231 m_writer.Write(_escapeBKS);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
232 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
233 case '\f':
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
234 m_writer.Write(_escapeFWD);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
235 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
236 case '\r':
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
237 m_writer.Write(_escapeCR);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
238 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
239 case '\n':
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
240 m_writer.Write(_escapeNL);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
241 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
242 case '\t':
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
243 m_writer.Write(_escapeTAB);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
244 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
245 case '\\':
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
246 m_writer.Write(_escapeBSLASH);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
247 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
248 case '"':
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
249 m_writer.Write(_escapeQ);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
250 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
251 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
252 if (ch < 0x20) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
253 m_writer.Write("\\u00{0:x2}",(int)ch);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
254 } else {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
255 m_writer.Write(ch);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
256 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
257 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
258 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
259 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
260
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
261 m_writer.Write('"');
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
262 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
263
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
264 void Write(double value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
265 m_writer.Write(value);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
266 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
267
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
268 void OperationNotApplicable(string opName) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
269 throw new InvalidOperationException(String.Format("The operation '{0}' isn't applicable in the context of '{1}'", opName, m_context.element ));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
270 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
271
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
272 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
273 }