annotate Implab/JSON/JSONWriter.cs @ 150:3258399cba83 v2

JSONWriter improvements
author cin
date Sat, 12 Dec 2015 22:12:44 +0300
parents 2100965eb97f
children
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;
142
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
4 using System.Globalization;
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
5 using System.Diagnostics;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
6
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 namespace Implab.JSON {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8 public class JSONWriter {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 struct Context {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10 public bool needComma;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 public JSONElementContext element;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 Stack<Context> m_contextStack = new Stack<Context>();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 Context m_context;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
16 const int BUFFER_SIZE = 64;
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
17
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 TextWriter m_writer;
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
19 readonly bool m_indent = true;
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
20 readonly int m_indentSize = 4;
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
21 readonly char[] m_buffer = new char[BUFFER_SIZE];
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
22 int m_bufferPos;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
23
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
24 static readonly char [] _hex = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
25 static readonly char [] _escapeBKS,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
26 _escapeFWD,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
27 _escapeCR,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
28 _escapeNL,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
29 _escapeTAB,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
30 _escapeBSLASH,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
31 _escapeQ;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
32
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
33 static JSONWriter() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
34 _escapeBKS = "\\b".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
35 _escapeFWD = "\\f".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
36 _escapeCR = "\\r".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37 _escapeNL = "\\n".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38 _escapeTAB = "\\t".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 _escapeBSLASH = "\\\\".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 _escapeQ = "\\\"".ToCharArray();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
41 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 public JSONWriter(TextWriter writer) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 Safe.ArgumentNotNull(writer, "writer");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 m_writer = writer;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
48 public JSONWriter(TextWriter writer, bool indent) {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
49 Safe.ArgumentNotNull(writer, "writer");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
50
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
51 m_writer = writer;
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
52 m_indent = indent;
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
53 }
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
54
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
55 void WriteIndent() {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
56 if (m_indent) {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
57 var indent = new char[m_contextStack.Count * m_indentSize + 1];
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
58 indent[0] = '\n';
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
59 for (int i = 1; i < indent.Length; i++)
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
60 indent[i] = ' ';
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
61 m_writer.Write(new String(indent));
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
62 } else {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
63 m_writer.Write(' ');
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
64 }
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
65 }
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
66
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67 void WriteMemberName(string name) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
68 Safe.ArgumentNotEmpty(name, "name");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
69 if (m_context.element != JSONElementContext.Object)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
70 OperationNotApplicable("WriteMember");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
71 if (m_context.needComma)
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
72 m_writer.Write(",");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
73
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
74 WriteIndent();
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
75 m_context.needComma = true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
76 Write(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
77 m_writer.Write(" : ");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
78 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
79
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
80 public void WriteValue(string name, string value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
81 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
82 Write(value);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
83 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
84
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
85 public void WriteValue(string name, bool value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
86 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
87 Write(value);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
88 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
89
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
90 public void WriteValue(string name, double value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
91 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
92 Write(value);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
93 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
94
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
95 public void WriteValue(string value) {
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
96 if (m_context.element == JSONElementContext.Array) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
97
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
98 if (m_context.needComma)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
99 m_writer.Write(",");
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
100 WriteIndent();
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
101 m_context.needComma = true;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
102
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
103 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
104 } else if (m_context.element == JSONElementContext.None) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
105 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
106 m_context.element = JSONElementContext.Closed;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
107 } else {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
108 OperationNotApplicable("WriteValue");
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
109 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
110 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
111
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
112 public void WriteValue(bool value) {
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
113 if (m_context.element == JSONElementContext.Array) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
114
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
115 if (m_context.needComma)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
116 m_writer.Write(",");
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
117 WriteIndent();
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
118 m_context.needComma = true;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
119
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
120 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
121 } else if (m_context.element == JSONElementContext.None) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
122 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
123 m_context.element = JSONElementContext.Closed;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
124 } else {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
125 OperationNotApplicable("WriteValue");
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
126 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
127 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
128
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
129 public void WriteValue(double value) {
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
130 if (m_context.element == JSONElementContext.Array) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
131
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
132 if (m_context.needComma)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
133 m_writer.Write(",");
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
134 WriteIndent();
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
135 m_context.needComma = true;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
136
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
137 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
138 } else if (m_context.element == JSONElementContext.None) {
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
139 Write(value);
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
140 m_context.element = JSONElementContext.Closed;
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
141 } else {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
142 OperationNotApplicable("WriteValue");
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
143 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
144 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
145
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
146 public void BeginObject() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
147 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
148 OperationNotApplicable("BeginObject");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
149 if (m_context.needComma)
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
150 m_writer.Write(",");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
151
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
152 WriteIndent();
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
153
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
154 m_context.needComma = true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
155
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
156 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
157
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
158 m_context = new Context { element = JSONElementContext.Object, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
159 m_writer.Write("{");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
160 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
161
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
162 public void BeginObject(string name) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
163 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
164
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
165 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
166
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
167 m_context = new Context { element = JSONElementContext.Object, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
168 m_writer.Write("{");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
169 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
170
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
171 public void EndObject() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
172 if (m_context.element != JSONElementContext.Object)
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
173 OperationNotApplicable("EndObject");
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
174
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
175 m_context = m_contextStack.Pop();
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
176 if (m_contextStack.Count == 0)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
177 m_context.element = JSONElementContext.Closed;
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
178 WriteIndent();
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
179 m_writer.Write("}");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
180 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
181
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
182 public void BeginArray() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
183 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
184 throw new InvalidOperationException();
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
185 if (m_context.needComma) {
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
186 m_writer.Write(",");
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
187
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
188 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
189 m_context.needComma = true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
190
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
191 WriteIndent();
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
192 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
193 m_context = new Context { element = JSONElementContext.Array, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
194 m_writer.Write("[");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
195 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
196
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
197 public void BeginArray(string name) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
198 WriteMemberName(name);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
199
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
200 m_contextStack.Push(m_context);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
201
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
202 m_context = new Context { element = JSONElementContext.Array, needComma = false };
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
203 m_writer.Write("[");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
204 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
205
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
206 public void EndArray() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
207 if (m_context.element != JSONElementContext.Array)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
208 OperationNotApplicable("EndArray");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
209
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
210 m_context = m_contextStack.Pop();
141
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
211 if (m_contextStack.Count == 0)
0fa293bb1351 fixed JSON writer
cin
parents: 119
diff changeset
212 m_context.element = JSONElementContext.Closed;
72
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
213 WriteIndent();
d67b95eddaf4 promises refactoring
cin
parents: 55
diff changeset
214 m_writer.Write("]");
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
215 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
216
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
217 void Write(bool value) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
218 m_writer.Write(value ? "true" : "false");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
219 }
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
220
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
221 void FlushBuffer() {
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
222 if (m_bufferPos > 0) {
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
223 m_writer.Write(m_buffer, 0, m_bufferPos);
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
224 m_bufferPos = 0;
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
225 }
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
226 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
227
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
228 void Write(string value) {
115
ffd3702968c7 fixed JSON writer
cin
parents: 72
diff changeset
229 if (value == null) {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
230 m_writer.Write("null");
115
ffd3702968c7 fixed JSON writer
cin
parents: 72
diff changeset
231 return;
ffd3702968c7 fixed JSON writer
cin
parents: 72
diff changeset
232 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
233
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
234 Debug.Assert(m_bufferPos == 0);
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
235
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
236 var chars = value.ToCharArray();
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
237 m_buffer[m_bufferPos++] = '"';
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
238
119
2573b562e328 Promises rewritten, added improved version of AsyncQueue
cin
parents: 118
diff changeset
239 // Analysis disable once ForCanBeConvertedToForeach
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
240 for (int i = 0; i < chars.Length; i++) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
241 var ch = chars[i];
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
242
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
243 char[] escapeSeq;
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
244
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
245 switch (ch) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
246 case '\b':
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
247 escapeSeq = _escapeBKS;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
248 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
249 case '\f':
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
250 escapeSeq = _escapeFWD;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
251 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
252 case '\r':
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
253 escapeSeq = _escapeCR;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
254 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
255 case '\n':
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
256 escapeSeq = _escapeNL;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
257 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
258 case '\t':
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
259 escapeSeq = _escapeTAB;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
260 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
261 case '\\':
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
262 escapeSeq = _escapeBSLASH;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
263 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
264 case '"':
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
265 escapeSeq = _escapeQ;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
266 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
267 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
268 if (ch < 0x20) {
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
269 if (m_bufferPos + 6 > BUFFER_SIZE)
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
270 FlushBuffer();
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
271
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
272 m_buffer[m_bufferPos++] = '\\';
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
273 m_buffer[m_bufferPos++] = 'u';
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
274 m_buffer[m_bufferPos++] = '0';
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
275 m_buffer[m_bufferPos++] = '0';
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
276 m_buffer[m_bufferPos++] = _hex[ch >> 4 & 0xf];
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
277 m_buffer[m_bufferPos++] = _hex[ch & 0xf];
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
278
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
279 } else {
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
280 if (m_bufferPos >= BUFFER_SIZE)
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
281 FlushBuffer();
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
282 m_buffer[m_bufferPos++] = ch;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
283 }
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
284 continue;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
285 }
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
286
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
287 if (m_bufferPos + escapeSeq.Length > BUFFER_SIZE)
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
288 FlushBuffer();
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
289
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
290 Array.Copy(escapeSeq, 0, m_buffer, m_bufferPos, escapeSeq.Length);
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
291 m_bufferPos += escapeSeq.Length;
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
292
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
293 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
294
150
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
295 if (m_bufferPos >= BUFFER_SIZE)
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
296 FlushBuffer();
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
297
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
298 m_buffer[m_bufferPos++] = '"';
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
299
3258399cba83 JSONWriter improvements
cin
parents: 142
diff changeset
300 FlushBuffer();
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
301 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
302
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
303 void Write(double value) {
142
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
304 if (double.IsNaN(value))
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
305 Write("NaN");
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
306 else if (double.IsNegativeInfinity(value))
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
307 Write("-Infinity");
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
308 else if (double.IsPositiveInfinity(value))
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
309 Write("Infinity");
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
310 else
2100965eb97f fixed JSONWriter handling Infinity, NaN and locale aware number formatting
cin
parents: 141
diff changeset
311 m_writer.Write(value.ToString(CultureInfo.InvariantCulture));
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
312 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
313
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
314 void OperationNotApplicable(string opName) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
315 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
316 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
317
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
318 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
319 }