diff Implab/Xml/JsonXmlReader.cs @ 265:74e048cbaac8 v3 v3.0.10

Restored lost JsonXmlCaseTransform from version 2.1
author cin
date Mon, 16 Apr 2018 19:43:18 +0300
parents 3a6e18c432be
children
line wrap: on
line diff
--- a/Implab/Xml/JsonXmlReader.cs	Mon Apr 16 18:43:49 2018 +0300
+++ b/Implab/Xml/JsonXmlReader.cs	Mon Apr 16 19:43:18 2018 +0300
@@ -56,6 +56,7 @@
         readonly string m_xmlnsNamespace;
         readonly string m_xsiPrefix;
         readonly string m_xsiNamespace;
+        readonly JsonXmlCaseTransform m_caseTransform;
 
 
         public JsonXmlReader(JsonReader parser, JsonXmlReaderOptions options) {
@@ -76,6 +77,8 @@
             m_xsiPrefix = m_nameTable.Add(XmlNameContext.XsiPrefix);
             m_xsiNamespace = m_nameTable.Add(XmlNameContext.XsiNamespace);
 
+            m_caseTransform = m_options.CaseTransform;
+
             // TODO validate m_jsonRootName, m_jsonArrayItemName
 
             m_context = new XmlNameContext(null, 0);
@@ -162,7 +165,7 @@
                 return m_value;
             }
         }
-        
+
         public override string GetAttribute(int i) {
             Safe.ArgumentInRange(i >= 0 && i < AttributeCount, nameof(i));
             return m_attributes[i].Value;
@@ -224,7 +227,7 @@
                 m_elementIsEmpty = m_isEmpty;
                 m_isAttribute = true;
             }
-            
+
             var attr = m_attributes[i];
 
 
@@ -328,7 +331,7 @@
                 foreach (var attr in attrs) {
                     if (attr.QName.Name == "xmlns") {
                         if (context == m_context)
-                             context = new XmlNameContext(m_context, m_xmlDepth);
+                            context = new XmlNameContext(m_context, m_xmlDepth);
                         context.DefinePrefix(attr.Value, string.Empty);
                     } else if (attr.Prefix == m_xmlnsPrefix) {
                         if (context == m_context)
@@ -459,7 +462,7 @@
                 }
 
                 while (m_parser.Read()) {
-                    var jsonName = m_nameTable.Add(m_parser.ElementName);
+                    var jsonName = m_nameTable.Add(TransformJsonName(m_parser.ElementName));
 
                     switch (m_parser.ElementType) {
                         case JsonElementType.BeginObject:
@@ -589,6 +592,16 @@
             return !skip;
         }
 
+        private string TransformJsonName(string name) {
+            if (m_caseTransform == JsonXmlCaseTransform.None || string.IsNullOrEmpty(name)) {
+                return name;
+            } else if (m_caseTransform == JsonXmlCaseTransform.UcFirst) {
+                return JsonXmlReader.UppercaseFirst(name);
+            } else {
+                return JsonXmlReader.LowercaseFirst(name);
+            }
+        }
+
         protected override void Dispose(bool disposing) {
             if (disposing)
                 Safe.Dispose(m_parser);
@@ -616,6 +629,21 @@
 
         #region static methods
 
+        //
+        // Static Methods
+        //
+        private static string LowercaseFirst(string s) {
+            char[] array = s.ToCharArray();
+            array[0] = char.ToLower(array[0]);
+            return new string(array);
+        }
+
+        private static string UppercaseFirst(string s) {
+            char[] array = s.ToCharArray();
+            array[0] = char.ToUpper(array[0]);
+            return new string(array);
+        }
+
         public static JsonXmlReader CreateJsonXmlReader(TextReader textReader, JsonXmlReaderOptions options = null) {
             var jsonReader = JsonReader.Create(textReader);
             return new JsonXmlReader(jsonReader, options);