annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
1 using Implab;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
2 using Implab.Parsing;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
3 using System;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
4 using System.Collections.Generic;
60
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
5 using System.IO;
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
6 using System.Linq;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
7 using System.Text;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
8 using System.Threading.Tasks;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
9 using System.Xml;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
10
60
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
11 namespace Implab.JSON {
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
12 public class JSONXmlReader : XmlReader {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
13
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
14 enum ValueContext {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
15 Undefined,
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
16 ElementStart,
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
17 ElementValue,
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
18 ElementEnd,
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
19 ElementEmpty
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
20 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
21
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
22 struct LocalNameContext {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
23 public string localName;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
24 public bool isArray;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
25 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
26
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
27 JSONParser m_parser;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
28 ValueContext m_valueContext;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
29 ReadState m_state = ReadState.Initial;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
30 Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
31 LocalNameContext m_localName;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
32 int m_depthCorrection = 0;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
33
60
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
34 readonly string m_rootName;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
35 readonly string m_prefix;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
36 readonly string m_namespaceUri;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
37 readonly bool m_flattenArrays;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
38 readonly string m_arrayItemName;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
39 readonly XmlNameTable m_nameTable;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
40
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
41 public JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) {
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
42 Safe.ArgumentNotNull(parser, "parser");
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
43 m_parser = parser;
60
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
44
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
45 if (options != null) {
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
46 m_prefix = options.NodesPrefix ?? String.Empty;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
47 m_namespaceUri = options.NamespaceURI ?? String.Empty;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
48 m_rootName = options.RootName ?? "json";
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
49 m_flattenArrays = options.FlattenArrays;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
50 m_arrayItemName = options.ArrayItemName ?? "item";
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
51 m_nameTable = options.NameTable ?? new NameTable();
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
52 } else {
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
53 m_prefix = String.Empty;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
54 m_namespaceUri = String.Empty;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
55 m_rootName = "json";
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
56 m_flattenArrays = false;
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
57 m_arrayItemName = "item";
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
58 m_nameTable = new NameTable();
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
59 }
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
60 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
61
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
62 /// <summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
63 /// Always 0, JSON doesn't support attributes
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
64 /// </summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
65 public override int AttributeCount {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
66 get { return 0; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
67 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
68
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
69 public override string BaseURI {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
70 get { return String.Empty; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
71 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
72
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
73 public override int Depth {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
74 get {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
75 return m_localNameStack.Count+m_depthCorrection;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
76 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
77 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
78
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
79 public override bool EOF {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
80 get { return m_parser.EOF; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
81 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
82
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
83 /// <summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
84 /// Always throws an exception
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
85 /// </summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
86 /// <param name="i"></param>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
87 /// <returns></returns>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
88 public override string GetAttribute(int i) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
89 throw new ArgumentOutOfRangeException();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
90 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
91
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
92 /// <summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
93 /// Always returns empty string
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
94 /// </summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
95 /// <param name="name"></param>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
96 /// <param name="namespaceURI"></param>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
97 /// <returns></returns>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
98 public override string GetAttribute(string name, string namespaceURI) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
99 return String.Empty;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
100 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
101
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
102 /// <summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
103 /// Always returns empty string
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
104 /// </summary>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
105 /// <param name="name"></param>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
106 /// <returns></returns>
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
107 public override string GetAttribute(string name) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
108 return String.Empty;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
109 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
110
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
111 public override bool IsEmptyElement {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
112 get { return m_parser.ElementType == JSONElementType.Value && m_valueContext == ValueContext.ElementEmpty; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
113 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
114
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
115 public override string LocalName {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
116 get { return m_localName.localName; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
117 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
118
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
119 public override string LookupNamespace(string prefix) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
120 if (String.IsNullOrEmpty(prefix) || prefix == m_prefix)
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
121 return m_namespaceUri;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
122 else
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
123 return String.Empty;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
124 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
125
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
126 public override bool MoveToAttribute(string name, string ns) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
127 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
128 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
129
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
130 public override bool MoveToAttribute(string name) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
131 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
132 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
133
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
134 public override bool MoveToElement() {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
135 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
136 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
137
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
138 public override bool MoveToFirstAttribute() {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
139 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
140 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
141
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
142 public override bool MoveToNextAttribute() {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
143 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
144 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
145
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
146 public override XmlNameTable NameTable {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
147 get { return m_nameTable; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
148 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
149
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
150 public override string NamespaceURI {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
151 get { return m_namespaceUri; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
152 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
153
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
154 public override XmlNodeType NodeType {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
155 get {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
156 switch (m_parser.ElementType) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
157 case JSONElementType.BeginObject:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
158 case JSONElementType.BeginArray:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
159 return XmlNodeType.Element;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
160 case JSONElementType.EndObject:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
161 case JSONElementType.EndArray:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
162 return XmlNodeType.EndElement;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
163 case JSONElementType.Value:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
164 switch (m_valueContext) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
165 case ValueContext.ElementStart:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
166 case ValueContext.ElementEmpty:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
167 return XmlNodeType.Element;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
168 case ValueContext.ElementValue:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
169 return XmlNodeType.Text;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
170 case ValueContext.ElementEnd:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
171 return XmlNodeType.EndElement;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
172 default:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
173 throw new InvalidOperationException();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
174 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
175 default:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
176 throw new InvalidOperationException();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
177 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
178 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
179 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
180
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
181 public override string Prefix {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
182 get { return m_prefix; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
183 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
184
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
185 public override bool Read() {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
186 if (m_state != System.Xml.ReadState.Interactive && m_state != System.Xml.ReadState.Initial)
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
187 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
188
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
189 if (m_state == ReadState.Initial)
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
190 m_state = System.Xml.ReadState.Interactive;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
191
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
192 try {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
193 switch (m_parser.ElementType) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
194 case JSONElementType.Value:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
195 switch (m_valueContext) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
196 case ValueContext.ElementStart:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
197 SetLocalName(String.Empty);
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
198 m_valueContext = ValueContext.ElementValue;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
199 return true;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
200 case ValueContext.ElementValue:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
201 RestoreLocalName();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
202 m_valueContext = ValueContext.ElementEnd;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
203 return true;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
204 case ValueContext.ElementEmpty:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
205 case ValueContext.ElementEnd:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
206 RestoreLocalName();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
207 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
208 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
209 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
210 case JSONElementType.EndArray:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
211 case JSONElementType.EndObject:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
212 RestoreLocalName();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
213 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
214 }
60
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
215 string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : m_arrayItemName;
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
216 while (m_parser.Read()) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
217 if (!String.IsNullOrEmpty(m_parser.ElementName))
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
218 itemName = m_parser.ElementName;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
219
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
220 switch (m_parser.ElementType) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
221 case JSONElementType.BeginArray:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
222 if (m_flattenArrays && !m_localName.isArray) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
223 m_depthCorrection--;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
224 SetLocalName(itemName, true);
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
225 continue;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
226 } else {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
227 SetLocalName(itemName, true);
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
228 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
229 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
230 case JSONElementType.BeginObject:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
231 SetLocalName(itemName);
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
232 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
233 case JSONElementType.EndArray:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
234 if (m_flattenArrays && !m_localNameStack.Peek().isArray) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
235 RestoreLocalName();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
236 m_depthCorrection++;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
237 continue;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
238 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
239 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
240 case JSONElementType.EndObject:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
241 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
242 case JSONElementType.Value:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
243 SetLocalName(itemName);
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
244 m_valueContext = m_parser.ElementValue == null ? ValueContext.ElementEmpty : ValueContext.ElementStart;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
245 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
246 default:
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
247 break;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
248 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
249 return true;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
250 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
251
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
252 m_state = System.Xml.ReadState.EndOfFile;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
253 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
254 } catch {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
255 m_state = System.Xml.ReadState.Error;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
256 throw;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
257 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
258 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
259
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
260 public override bool ReadAttributeValue() {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
261 return false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
262 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
263
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
264 public override ReadState ReadState {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
265 get { return m_state; }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
266 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
267
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
268 public override void ResolveEntity() {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
269 // do nothing
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
270 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
271
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
272 public override string Value {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
273 get { return m_parser.ElementValue == null ? String.Empty : m_parser.ElementValue.ToString(); }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
274 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
275
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
276 void SetLocalName(string name) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
277 m_localNameStack.Push(m_localName);
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
278 m_localName.localName = name;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
279 m_localName.isArray = false;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
280 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
281
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
282 void SetLocalName(string name, bool isArray) {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
283 m_localNameStack.Push(m_localName);
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
284 m_localName.localName = name;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
285 m_localName.isArray = isArray;
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
286 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
287
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
288 void RestoreLocalName() {
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
289 m_localName = m_localNameStack.Pop();
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
290 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
291
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
292 public override void Close() {
59
21611344d366 code cleanup
cin
parents: 58
diff changeset
293
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
294 }
59
21611344d366 code cleanup
cin
parents: 58
diff changeset
295
21611344d366 code cleanup
cin
parents: 58
diff changeset
296 protected override void Dispose(bool disposing) {
21611344d366 code cleanup
cin
parents: 58
diff changeset
297 if (disposing) {
21611344d366 code cleanup
cin
parents: 58
diff changeset
298 m_parser.Dispose();
21611344d366 code cleanup
cin
parents: 58
diff changeset
299 }
21611344d366 code cleanup
cin
parents: 58
diff changeset
300 base.Dispose(disposing);
21611344d366 code cleanup
cin
parents: 58
diff changeset
301 }
21611344d366 code cleanup
cin
parents: 58
diff changeset
302
60
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
303
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
304 public static JSONXmlReader OpenFile(string file, JSONXmlReaderOptions options) {
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
305 var stream = File.OpenText(file);
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
306 var parser = new JSONParser(stream, true);
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
307 return new JSONXmlReader(parser, options);
10c7337d29e7 +JSONXmlReaderOptions
cin
parents: 59
diff changeset
308 }
58
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
309 }
1710dcda34bb Added JSONXmlReader
cin
parents:
diff changeset
310 }