annotate Implab/JSON/JSONXmlReader.cs @ 63:908b4f340c69

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