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