comparison Implab/JSON/JSONXmlReader.cs @ 60:10c7337d29e7

+JSONXmlReaderOptions *JSONScanner: fixed reading from TextReader *code cleanup
author cin
date Thu, 19 Jun 2014 03:41:28 +0400
parents 21611344d366
children 908b4f340c69
comparison
equal deleted inserted replaced
59:21611344d366 60:10c7337d29e7
1 using Implab; 1 using Implab;
2 using Implab.JSON;
3 using Implab.Parsing; 2 using Implab.Parsing;
4 using System; 3 using System;
5 using System.Collections.Generic; 4 using System.Collections.Generic;
5 using System.IO;
6 using System.Linq; 6 using System.Linq;
7 using System.Text; 7 using System.Text;
8 using System.Threading.Tasks; 8 using System.Threading.Tasks;
9 using System.Xml; 9 using System.Xml;
10 10
11 namespace ConsPlay { 11 namespace Implab.JSON {
12 public class JSONXmlReader : XmlReader { 12 public class JSONXmlReader : XmlReader {
13 13
14 enum ValueContext { 14 enum ValueContext {
15 Undefined, 15 Undefined,
16 ElementStart, 16 ElementStart,
27 JSONParser m_parser; 27 JSONParser m_parser;
28 ValueContext m_valueContext; 28 ValueContext m_valueContext;
29 ReadState m_state = ReadState.Initial; 29 ReadState m_state = ReadState.Initial;
30 Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>(); 30 Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>();
31 LocalNameContext m_localName; 31 LocalNameContext m_localName;
32 string m_rootName = "json";
33 string m_prefix = String.Empty;
34 string m_namespaceUri = String.Empty;
35 bool m_flattenArrays = false;
36 NameTable m_nameTable = new NameTable();
37 int m_depthCorrection = 0; 32 int m_depthCorrection = 0;
38 33
39 public JSONXmlReader(JSONParser parser) { 34 readonly string m_rootName;
35 readonly string m_prefix;
36 readonly string m_namespaceUri;
37 readonly bool m_flattenArrays;
38 readonly string m_arrayItemName;
39 readonly XmlNameTable m_nameTable;
40
41 public JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) {
40 Safe.ArgumentNotNull(parser, "parser"); 42 Safe.ArgumentNotNull(parser, "parser");
41 m_parser = parser; 43 m_parser = parser;
44
45 if (options != null) {
46 m_prefix = options.NodesPrefix ?? String.Empty;
47 m_namespaceUri = options.NamespaceURI ?? String.Empty;
48 m_rootName = options.RootName ?? "json";
49 m_flattenArrays = options.FlattenArrays;
50 m_arrayItemName = options.ArrayItemName ?? "item";
51 m_nameTable = options.NameTable ?? new NameTable();
52 } else {
53 m_prefix = String.Empty;
54 m_namespaceUri = String.Empty;
55 m_rootName = "json";
56 m_flattenArrays = false;
57 m_arrayItemName = "item";
58 m_nameTable = new NameTable();
59 }
42 } 60 }
43 61
44 /// <summary> 62 /// <summary>
45 /// Always 0, JSON doesn't support attributes 63 /// Always 0, JSON doesn't support attributes
46 /// </summary> 64 /// </summary>
192 case JSONElementType.EndArray: 210 case JSONElementType.EndArray:
193 case JSONElementType.EndObject: 211 case JSONElementType.EndObject:
194 RestoreLocalName(); 212 RestoreLocalName();
195 break; 213 break;
196 } 214 }
197 string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : "item"; 215 string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : m_arrayItemName;
198 while (m_parser.Read()) { 216 while (m_parser.Read()) {
199 if (!String.IsNullOrEmpty(m_parser.ElementName)) 217 if (!String.IsNullOrEmpty(m_parser.ElementName))
200 itemName = m_parser.ElementName; 218 itemName = m_parser.ElementName;
201 219
202 switch (m_parser.ElementType) { 220 switch (m_parser.ElementType) {
280 m_parser.Dispose(); 298 m_parser.Dispose();
281 } 299 }
282 base.Dispose(disposing); 300 base.Dispose(disposing);
283 } 301 }
284 302
303
304 public static JSONXmlReader OpenFile(string file, JSONXmlReaderOptions options) {
305 var stream = File.OpenText(file);
306 var parser = new JSONParser(stream, true);
307 return new JSONXmlReader(parser, options);
308 }
285 } 309 }
286 } 310 }