comparison Implab/Formats/JSON/JSONXmlReader.cs @ 180:c32688129f14 ref20160224

refactoring complete, JSONParser rewritten
author cin
date Thu, 24 Mar 2016 02:30:46 +0300
parents 419aa51b04fd
children 8222a2ab3ab7
comparison
equal deleted inserted replaced
179:478ef706906a 180:c32688129f14
1 using Implab; 1 using Implab;
2 using Implab.Parsing;
3 using System; 2 using System;
4 using System.Collections.Generic; 3 using System.Collections.Generic;
5 using System.Globalization; 4 using System.Globalization;
6 using System.IO; 5 using System.IO;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Xml; 6 using System.Xml;
11 7
12 namespace Implab.JSON { 8 namespace Implab.Formats.JSON {
13 public class JSONXmlReader : XmlReader { 9 public class JSONXmlReader : XmlReader {
14 10
15 enum ValueContext { 11 enum ValueContext {
16 Undefined, 12 Undefined,
17 ElementStart, 13 ElementStart,
28 JSONParser m_parser; 24 JSONParser m_parser;
29 ValueContext m_valueContext; 25 ValueContext m_valueContext;
30 ReadState m_state = ReadState.Initial; 26 ReadState m_state = ReadState.Initial;
31 Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>(); 27 Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>();
32 LocalNameContext m_localName; 28 LocalNameContext m_localName;
33 int m_depthCorrection = 0; 29 int m_depthCorrection;
34 30
35 readonly string m_rootName; 31 readonly string m_rootName;
36 readonly string m_prefix; 32 readonly string m_prefix;
37 readonly string m_namespaceUri; 33 readonly string m_namespaceUri;
38 readonly bool m_flattenArrays; 34 readonly bool m_flattenArrays;
117 } 113 }
118 114
119 public override string LookupNamespace(string prefix) { 115 public override string LookupNamespace(string prefix) {
120 if (String.IsNullOrEmpty(prefix) || prefix == m_prefix) 116 if (String.IsNullOrEmpty(prefix) || prefix == m_prefix)
121 return m_namespaceUri; 117 return m_namespaceUri;
122 else 118
123 return String.Empty; 119 return String.Empty;
124 } 120 }
125 121
126 public override bool MoveToAttribute(string name, string ns) { 122 public override bool MoveToAttribute(string name, string ns) {
127 return false; 123 return false;
128 } 124 }
181 public override string Prefix { 177 public override string Prefix {
182 get { return m_prefix; } 178 get { return m_prefix; }
183 } 179 }
184 180
185 public override bool Read() { 181 public override bool Read() {
186 if (m_state != System.Xml.ReadState.Interactive && m_state != System.Xml.ReadState.Initial) 182 if (m_state != ReadState.Interactive && m_state != ReadState.Initial)
187 return false; 183 return false;
188 184
189 if (m_state == ReadState.Initial) 185 if (m_state == ReadState.Initial)
190 m_state = System.Xml.ReadState.Interactive; 186 m_state = ReadState.Interactive;
191 187
192 try { 188 try {
193 switch (m_parser.ElementType) { 189 switch (m_parser.ElementType) {
194 case JSONElementType.Value: 190 case JSONElementType.Value:
195 switch (m_valueContext) { 191 switch (m_valueContext) {
221 case JSONElementType.BeginArray: 217 case JSONElementType.BeginArray:
222 if (m_flattenArrays && !m_localName.isArray) { 218 if (m_flattenArrays && !m_localName.isArray) {
223 m_depthCorrection--; 219 m_depthCorrection--;
224 SetLocalName(itemName, true); 220 SetLocalName(itemName, true);
225 continue; 221 continue;
226 } else {
227 SetLocalName(itemName, true);
228 } 222 }
223 SetLocalName(itemName, true);
229 break; 224 break;
230 case JSONElementType.BeginObject: 225 case JSONElementType.BeginObject:
231 SetLocalName(itemName); 226 SetLocalName(itemName);
232 break; 227 break;
233 case JSONElementType.EndArray: 228 case JSONElementType.EndArray:
241 break; 236 break;
242 case JSONElementType.Value: 237 case JSONElementType.Value:
243 SetLocalName(itemName); 238 SetLocalName(itemName);
244 m_valueContext = m_parser.ElementValue == null ? ValueContext.ElementEmpty : ValueContext.ElementStart; 239 m_valueContext = m_parser.ElementValue == null ? ValueContext.ElementEmpty : ValueContext.ElementStart;
245 break; 240 break;
246 default:
247 break;
248 } 241 }
249 return true; 242 return true;
250 } 243 }
251 244
252 m_state = System.Xml.ReadState.EndOfFile; 245 m_state = ReadState.EndOfFile;
253 return false; 246 return false;
254 } catch { 247 } catch {
255 m_state = System.Xml.ReadState.Error; 248 m_state = ReadState.Error;
256 throw; 249 throw;
257 } 250 }
258 } 251 }
259 252
260 public override bool ReadAttributeValue() { 253 public override bool ReadAttributeValue() {
273 get { 266 get {
274 if (m_parser.ElementValue == null) 267 if (m_parser.ElementValue == null)
275 return String.Empty; 268 return String.Empty;
276 if (Convert.GetTypeCode(m_parser.ElementValue) == TypeCode.Double) 269 if (Convert.GetTypeCode(m_parser.ElementValue) == TypeCode.Double)
277 return ((double)m_parser.ElementValue).ToString(CultureInfo.InvariantCulture); 270 return ((double)m_parser.ElementValue).ToString(CultureInfo.InvariantCulture);
278 else 271 return m_parser.ElementValue.ToString();
279 return m_parser.ElementValue.ToString();
280 } 272 }
281 } 273 }
282 274
283 void SetLocalName(string name) { 275 void SetLocalName(string name) {
284 m_localNameStack.Push(m_localName); 276 m_localNameStack.Push(m_localName);
321 /// <param name="options">Options.</param> 313 /// <param name="options">Options.</param>
322 /// <remarks> 314 /// <remarks>
323 /// The reader will be disposed when the XmlReader is disposed. 315 /// The reader will be disposed when the XmlReader is disposed.
324 /// </remarks> 316 /// </remarks>
325 public static JSONXmlReader Create(TextReader reader, JSONXmlReaderOptions options) { 317 public static JSONXmlReader Create(TextReader reader, JSONXmlReaderOptions options) {
326 return new JSONXmlReader(new JSONParser(reader, true), options); 318 return new JSONXmlReader(new JSONParser(reader), options);
327 } 319 }
328 320
329 /// <summary> 321 /// <summary>
330 /// Creates the XmlReader for the specified stream with JSON data. 322 /// Creates the XmlReader for the specified stream with JSON data.
331 /// </summary> 323 /// </summary>