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