comparison Implab/Xml/JsonXmlReader.cs @ 236:302ca905c19e v2

JsonReader optimizations
author cin
date Tue, 21 Nov 2017 14:57:58 +0300
parents 5f7a3e1d32b9
children 7c7e9ad6fe4a
comparison
equal deleted inserted replaced
235:b49969a7043c 236:302ca905c19e
37 XmlQualifiedName m_qName; 37 XmlQualifiedName m_qName;
38 string m_prefix; 38 string m_prefix;
39 int m_xmlDepth; 39 int m_xmlDepth;
40 40
41 XmlSimpleAttribute[] m_attributes; 41 XmlSimpleAttribute[] m_attributes;
42 object m_value; 42 string m_value;
43 bool m_isEmpty; 43 bool m_isEmpty;
44 44
45 XmlNodeType m_nodeType = XmlNodeType.None; 45 XmlNodeType m_nodeType = XmlNodeType.None;
46 46
47 bool m_isAttribute; // indicates that we are reading attribute nodes 47 bool m_isAttribute; // indicates that we are reading attribute nodes
156 } 156 }
157 } 157 }
158 158
159 public override string Value { 159 public override string Value {
160 get { 160 get {
161 return ConvertValueToString(m_value); 161 return m_value;
162 } 162 }
163 } 163 }
164 164
165 static string ConvertValueToString(object value) {
166 if (value == null)
167 return string.Empty;
168
169 switch (Convert.GetTypeCode(value)) {
170 case TypeCode.Double:
171 return ((double)value).ToString(CultureInfo.InvariantCulture);
172 case TypeCode.String:
173 return (string)value;
174 case TypeCode.Boolean:
175 return (bool)value ? "true" : "false";
176 default:
177 return value.ToString();
178 }
179 }
180
181 public override string GetAttribute(int i) { 165 public override string GetAttribute(int i) {
182 Safe.ArgumentInRange(i, 0, AttributeCount - 1, nameof(i)); 166 Safe.ArgumentInRange(i, 0, AttributeCount - 1, nameof(i));
183 return ConvertValueToString(m_attributes[i].Value); 167 return m_attributes[i].Value;
184 } 168 }
185 169
186 public override string GetAttribute(string name) { 170 public override string GetAttribute(string name) {
187 if (m_attributes == null) 171 if (m_attributes == null)
188 return null; 172 return null;
189 var qName = m_context.Resolve(name); 173 var qName = m_context.Resolve(name);
190 var attr = Array.Find(m_attributes, x => x.QName == qName); 174 var attr = Array.Find(m_attributes, x => x.QName == qName);
191 var value = ConvertValueToString(attr?.Value); 175 var value = attr?.Value;
192 return value == string.Empty ? null : value; 176 return value == string.Empty ? null : value;
193 } 177 }
194 178
195 public override string GetAttribute(string name, string namespaceURI) { 179 public override string GetAttribute(string name, string namespaceURI) {
196 if (m_attributes == null) 180 if (m_attributes == null)
197 return null; 181 return null;
198 var qName = new XmlQualifiedName(name, namespaceURI); 182 var qName = new XmlQualifiedName(name, namespaceURI);
199 var attr = Array.Find(m_attributes, x => x.QName == qName); 183 var attr = Array.Find(m_attributes, x => x.QName == qName);
200 var value = ConvertValueToString(attr?.Value); 184 var value = attr?.Value;
201 return value == string.Empty ? null : value; 185 return value == string.Empty ? null : value;
202 } 186 }
203 187
204 public override string LookupNamespace(string prefix) { 188 public override string LookupNamespace(string prefix) {
205 return m_context.ResolvePrefix(prefix); 189 return m_context.ResolvePrefix(prefix);
317 default: 301 default:
318 return true; 302 return true;
319 } 303 }
320 } 304 }
321 305
322 void ValueNode(object value) { 306 void ValueNode(string value) {
323 if (!IsSibling()) // the node is nested 307 if (!IsSibling()) // the node is nested
324 m_xmlDepth++; 308 m_xmlDepth++;
325 309
326 m_qName = XmlQualifiedName.Empty; 310 m_qName = XmlQualifiedName.Empty;
327 m_nodeType = XmlNodeType.Text; 311 m_nodeType = XmlNodeType.Text;
342 if (attrs != null) { 326 if (attrs != null) {
343 foreach (var attr in attrs) { 327 foreach (var attr in attrs) {
344 if (attr.QName.Name == "xmlns") { 328 if (attr.QName.Name == "xmlns") {
345 if (context == m_context) 329 if (context == m_context)
346 context = new XmlNameContext(m_context, m_xmlDepth); 330 context = new XmlNameContext(m_context, m_xmlDepth);
347 context.DefinePrefix(ConvertValueToString(attr.Value), string.Empty); 331 context.DefinePrefix(attr.Value, string.Empty);
348 } else if (attr.Prefix == m_xmlnsPrefix) { 332 } else if (attr.Prefix == m_xmlnsPrefix) {
349 if (context == m_context) 333 if (context == m_context)
350 context = new XmlNameContext(m_context, m_xmlDepth); 334 context = new XmlNameContext(m_context, m_xmlDepth);
351 context.DefinePrefix(ConvertValueToString(attr.Value), attr.QName.Name); 335 context.DefinePrefix(attr.Value, attr.QName.Name);
352 } else { 336 } else {
353 string attrPrefix; 337 string attrPrefix;
354 if (string.IsNullOrEmpty(attr.QName.Namespace)) 338 if (string.IsNullOrEmpty(attr.QName.Namespace))
355 continue; 339 continue;
356 340
514 // generate empty element with xsi:nil="true" attribute 498 // generate empty element with xsi:nil="true" attribute
515 ElementNode( 499 ElementNode(
516 m_jsonValueName, 500 m_jsonValueName,
517 m_jsonNamespace, 501 m_jsonNamespace,
518 new[] { 502 new[] {
519 new XmlSimpleAttribute("nil", m_xsiNamespace, m_xsiPrefix, true) 503 new XmlSimpleAttribute("nil", m_xsiNamespace, m_xsiPrefix, "true")
520 }, 504 },
521 true 505 true
522 ); 506 );
523 else 507 else
524 ElementNode(m_jsonValueName, m_jsonNamespace, elementAttrs, m_parser.ElementValue.Equals(string.Empty)); 508 ElementNode(m_jsonValueName, m_jsonNamespace, elementAttrs, m_parser.ElementValue.Equals(string.Empty));
605 } 589 }
606 590
607 public override string ToString() { 591 public override string ToString() {
608 switch (NodeType) { 592 switch (NodeType) {
609 case XmlNodeType.Element: 593 case XmlNodeType.Element:
610 return $"<{Name} {string.Join(" ", (m_attributes ?? new XmlSimpleAttribute[0]).Select(x => $"{x.Prefix}{(string.IsNullOrEmpty(x.Prefix) ? "" : ":")}{x.QName.Name}='{ConvertValueToString(x.Value)}'"))} {(IsEmptyElement ? "/" : "")}>"; 594 return $"<{Name} {string.Join(" ", (m_attributes ?? new XmlSimpleAttribute[0]).Select(x => $"{x.Prefix}{(string.IsNullOrEmpty(x.Prefix) ? "" : ":")}{x.QName.Name}='{x.Value}'"))} {(IsEmptyElement ? "/" : "")}>";
611 case XmlNodeType.Attribute: 595 case XmlNodeType.Attribute:
612 return $"@{Name}"; 596 return $"@{Name}";
613 case XmlNodeType.Text: 597 case XmlNodeType.Text:
614 return $"{Value}"; 598 return $"{Value}";
615 case XmlNodeType.CDATA: 599 case XmlNodeType.CDATA: