annotate Implab/Formats/JSON/JSONXmlReader.cs @ 204:cbb0bd8fc0d1 v2

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