comparison 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
comparison
equal deleted inserted replaced
62:62b440d46313 63:908b4f340c69
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;
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) {
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
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; }
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;
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