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

JsonReader optimizations
author cin
date Tue, 21 Nov 2017 14:57:58 +0300
parents 5f7a3e1d32b9
children 7c7e9ad6fe4a
line wrap: on
line diff
--- a/Implab/Xml/JsonXmlReader.cs	Thu Oct 05 09:24:49 2017 +0300
+++ b/Implab/Xml/JsonXmlReader.cs	Tue Nov 21 14:57:58 2017 +0300
@@ -39,7 +39,7 @@
         int m_xmlDepth;
 
         XmlSimpleAttribute[] m_attributes;
-        object m_value;
+        string m_value;
         bool m_isEmpty;
 
         XmlNodeType m_nodeType = XmlNodeType.None;
@@ -158,29 +158,13 @@
 
         public override string Value {
             get {
-                return ConvertValueToString(m_value);
+                return m_value;
             }
         }
-
-        static string ConvertValueToString(object value) {
-            if (value == null)
-                return string.Empty;
-
-            switch (Convert.GetTypeCode(value)) {
-                case TypeCode.Double:
-                    return ((double)value).ToString(CultureInfo.InvariantCulture);
-                case TypeCode.String:
-                    return (string)value;
-                case TypeCode.Boolean:
-                    return (bool)value ? "true" : "false";
-                default:
-                    return value.ToString();
-            }
-        }
-
+        
         public override string GetAttribute(int i) {
             Safe.ArgumentInRange(i, 0, AttributeCount - 1, nameof(i));
-            return ConvertValueToString(m_attributes[i].Value);
+            return m_attributes[i].Value;
         }
 
         public override string GetAttribute(string name) {
@@ -188,7 +172,7 @@
                 return null;
             var qName = m_context.Resolve(name);
             var attr = Array.Find(m_attributes, x => x.QName == qName);
-            var value = ConvertValueToString(attr?.Value);
+            var value = attr?.Value;
             return value == string.Empty ? null : value;
         }
 
@@ -197,7 +181,7 @@
                 return null;
             var qName = new XmlQualifiedName(name, namespaceURI);
             var attr = Array.Find(m_attributes, x => x.QName == qName);
-            var value = ConvertValueToString(attr?.Value);
+            var value = attr?.Value;
             return value == string.Empty ? null : value;
         }
 
@@ -319,7 +303,7 @@
             }
         }
 
-        void ValueNode(object value) {
+        void ValueNode(string value) {
             if (!IsSibling()) // the node is nested
                 m_xmlDepth++;
 
@@ -344,11 +328,11 @@
                     if (attr.QName.Name == "xmlns") {
                         if (context == m_context)
                              context = new XmlNameContext(m_context, m_xmlDepth);
-                        context.DefinePrefix(ConvertValueToString(attr.Value), string.Empty);
+                        context.DefinePrefix(attr.Value, string.Empty);
                     } else if (attr.Prefix == m_xmlnsPrefix) {
                         if (context == m_context)
                             context = new XmlNameContext(m_context, m_xmlDepth);
-                        context.DefinePrefix(ConvertValueToString(attr.Value), attr.QName.Name);
+                        context.DefinePrefix(attr.Value, attr.QName.Name);
                     } else {
                         string attrPrefix;
                         if (string.IsNullOrEmpty(attr.QName.Namespace))
@@ -516,7 +500,7 @@
                                     m_jsonValueName,
                                     m_jsonNamespace,
                                     new[] {
-                                        new XmlSimpleAttribute("nil", m_xsiNamespace, m_xsiPrefix, true)
+                                        new XmlSimpleAttribute("nil", m_xsiNamespace, m_xsiPrefix, "true")
                                     },
                                     true
                                 );
@@ -607,7 +591,7 @@
         public override string ToString() {
             switch (NodeType) {
                 case XmlNodeType.Element:
-                    return $"<{Name} {string.Join(" ", (m_attributes ?? new XmlSimpleAttribute[0]).Select(x => $"{x.Prefix}{(string.IsNullOrEmpty(x.Prefix) ? "" : ":")}{x.QName.Name}='{ConvertValueToString(x.Value)}'"))} {(IsEmptyElement ? "/" : "")}>";
+                    return $"<{Name} {string.Join(" ", (m_attributes ?? new XmlSimpleAttribute[0]).Select(x => $"{x.Prefix}{(string.IsNullOrEmpty(x.Prefix) ? "" : ":")}{x.QName.Name}='{x.Value}'"))} {(IsEmptyElement ? "/" : "")}>";
                 case XmlNodeType.Attribute:
                     return $"@{Name}";
                 case XmlNodeType.Text: