annotate Implab/JSON/JSONXmlReader.cs @ 145:706fccb85524 v2

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