comparison Implab/JSON/JSONWriter.cs @ 72:d67b95eddaf4 v2

promises refactoring
author cin
date Thu, 04 Sep 2014 18:47:12 +0400
parents c0bf853aa04f
children ffd3702968c7
comparison
equal deleted inserted replaced
71:1714fd8678ef 72:d67b95eddaf4
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 bool m_indent; 18 readonly bool m_indent = true;
19 readonly int m_indentSize = 4;
19 20
20 static readonly char [] _escapeBKS, 21 static readonly char [] _escapeBKS,
21 _escapeFWD, 22 _escapeFWD,
22 _escapeCR, 23 _escapeCR,
23 _escapeNL, 24 _escapeNL,
41 Safe.ArgumentNotNull(writer, "writer"); 42 Safe.ArgumentNotNull(writer, "writer");
42 43
43 m_writer = writer; 44 m_writer = writer;
44 } 45 }
45 46
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
46 void WriteMemberName(string name) { 66 void WriteMemberName(string name) {
47 Safe.ArgumentNotEmpty(name, "name"); 67 Safe.ArgumentNotEmpty(name, "name");
48 if (m_context.element != JSONElementContext.Object) 68 if (m_context.element != JSONElementContext.Object)
49 OperationNotApplicable("WriteMember"); 69 OperationNotApplicable("WriteMember");
50 if (m_context.needComma) 70 if (m_context.needComma)
51 m_writer.Write(", "); 71 m_writer.Write(",");
52 // TODO indent 72
73 WriteIndent();
53 m_context.needComma = true; 74 m_context.needComma = true;
54 Write(name); 75 Write(name);
55 m_writer.Write(" : "); 76 m_writer.Write(" : ");
56 } 77 }
57 78
68 public void WriteValue(string name, double value) { 89 public void WriteValue(string name, double value) {
69 WriteMemberName(name); 90 WriteMemberName(name);
70 Write(value); 91 Write(value);
71 } 92 }
72 93
73
74
75 public void WriteValue(string value) { 94 public void WriteValue(string value) {
76 if (m_context.element != JSONElementContext.Array) 95 if (m_context.element != JSONElementContext.Array)
77 OperationNotApplicable("WriteValue"); 96 OperationNotApplicable("WriteValue");
78 if (m_context.needComma) 97 if (m_context.needComma)
79 m_writer.Write(", "); 98 m_writer.Write(",");
99 WriteIndent();
80 m_context.needComma = true; 100 m_context.needComma = true;
81 101
82 Write(value); 102 Write(value);
83 } 103 }
84 104
85 public void WriteValue(bool value) { 105 public void WriteValue(bool value) {
86 if (m_context.element != JSONElementContext.Array) 106 if (m_context.element != JSONElementContext.Array)
87 OperationNotApplicable("WriteValue"); 107 OperationNotApplicable("WriteValue");
88 if (m_context.needComma) 108 if (m_context.needComma)
89 m_writer.Write(", "); 109 m_writer.Write(",");
90 m_context.needComma = true; 110 m_context.needComma = true;
91 111
112 WriteIndent();
92 Write(value); 113 Write(value);
93 } 114 }
94 115
95 public void WriteValue(double value) { 116 public void WriteValue(double value) {
96 if (m_context.element != JSONElementContext.Array) 117 if (m_context.element != JSONElementContext.Array)
97 OperationNotApplicable("WriteValue"); 118 OperationNotApplicable("WriteValue");
98 if (m_context.needComma) 119 if (m_context.needComma)
99 m_writer.Write(", "); 120 m_writer.Write(",");
100 m_context.needComma = true; 121 m_context.needComma = true;
101 122
123 WriteIndent();
102 Write(value); 124 Write(value);
103 } 125 }
104 126
105 public void BeginObject() { 127 public void BeginObject() {
106 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array) 128 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array)
107 OperationNotApplicable("BeginObject"); 129 OperationNotApplicable("BeginObject");
108 if (m_context.needComma) 130 if (m_context.needComma)
109 m_writer.Write(", "); 131 m_writer.Write(",");
132
133 WriteIndent();
134
110 m_context.needComma = true; 135 m_context.needComma = true;
111 136
112 m_contextStack.Push(m_context); 137 m_contextStack.Push(m_context);
113 138
114 m_context = new Context { element = JSONElementContext.Object, needComma = false }; 139 m_context = new Context { element = JSONElementContext.Object, needComma = false };
115 m_writer.Write("{ "); 140 m_writer.Write("{");
116 } 141 }
117 142
118 public void BeginObject(string name) { 143 public void BeginObject(string name) {
119 WriteMemberName(name); 144 WriteMemberName(name);
120 145
121 m_contextStack.Push(m_context); 146 m_contextStack.Push(m_context);
122 147
123 m_context = new Context { element = JSONElementContext.Object, needComma = false }; 148 m_context = new Context { element = JSONElementContext.Object, needComma = false };
124 m_writer.Write("{ "); 149 m_writer.Write("{");
125 } 150 }
126 151
127 public void EndObject() { 152 public void EndObject() {
128 if (m_context.element != JSONElementContext.Object) 153 if (m_context.element != JSONElementContext.Object)
129 OperationNotApplicable("EndArray"); 154 OperationNotApplicable("EndArray");
130 155
131 m_writer.Write(" }");
132 m_context = m_contextStack.Pop(); 156 m_context = m_contextStack.Pop();
157 WriteIndent();
158 m_writer.Write("}");
133 } 159 }
134 160
135 public void BeginArray() { 161 public void BeginArray() {
136 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array) 162 if (m_context.element != JSONElementContext.None && m_context.element != JSONElementContext.Array)
137 throw new InvalidOperationException(); 163 throw new InvalidOperationException();
138 if (m_context.needComma) 164 if (m_context.needComma) {
139 m_writer.Write(", "); 165 m_writer.Write(",");
140 m_context.needComma = true; 166
141 167 }
142 m_contextStack.Push(m_context); 168 m_context.needComma = true;
143 169
170 WriteIndent();
171 m_contextStack.Push(m_context);
144 m_context = new Context { element = JSONElementContext.Array, needComma = false }; 172 m_context = new Context { element = JSONElementContext.Array, needComma = false };
145 m_writer.Write("[ "); 173 m_writer.Write("[");
146 } 174 }
147 175
148 public void BeginArray(string name) { 176 public void BeginArray(string name) {
149 WriteMemberName(name); 177 WriteMemberName(name);
150 178
151 m_contextStack.Push(m_context); 179 m_contextStack.Push(m_context);
152 180
153 m_context = new Context { element = JSONElementContext.Array, needComma = false }; 181 m_context = new Context { element = JSONElementContext.Array, needComma = false };
154 m_writer.Write("[ "); 182 m_writer.Write("[");
155 } 183 }
156 184
157 public void EndArray() { 185 public void EndArray() {
158 if (m_context.element != JSONElementContext.Array) 186 if (m_context.element != JSONElementContext.Array)
159 OperationNotApplicable("EndArray"); 187 OperationNotApplicable("EndArray");
160 188
161 m_writer.Write(" ]");
162 m_context = m_contextStack.Pop(); 189 m_context = m_contextStack.Pop();
190 WriteIndent();
191 m_writer.Write("]");
163 } 192 }
164 193
165 void Write(bool value) { 194 void Write(bool value) {
166 m_writer.Write(value ? "true" : "false"); 195 m_writer.Write(value ? "true" : "false");
167 } 196 }