annotate Implab/Formats/JSON/JSONWriter.cs @ 206:86b61d53b7db v2

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