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