annotate Implab/Formats/JSON/JSONXmlReader.cs @ 176:0c3c69fe225b ref20160224

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