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