# HG changeset patch # User cin # Date 1523896998 -10800 # Node ID 74e048cbaac8cdd5a0825b1fd8b47dd932a05ae8 # Parent 3a6e18c432beb8930d2b86a1b0a0596f4f6a2898 Restored lost JsonXmlCaseTransform from version 2.1 diff -r 3a6e18c432be -r 74e048cbaac8 Implab.Test/JsonTests.cs --- a/Implab.Test/JsonTests.cs Mon Apr 16 18:43:49 2018 +0300 +++ b/Implab.Test/JsonTests.cs Mon Apr 16 19:43:18 2018 +0300 @@ -167,7 +167,7 @@ CloseOutput = false, ConformanceLevel = ConformanceLevel.Document })) - using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { + using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "Data", NodesPrefix = "json", CaseTransform = JsonXmlCaseTransform.UcFirst, ArrayItemName = "Item" })) { xmlWriter.WriteNode(xmlReader, false); } Console.WriteLine(); diff -r 3a6e18c432be -r 74e048cbaac8 Implab/Implab.csproj --- a/Implab/Implab.csproj Mon Apr 16 18:43:49 2018 +0300 +++ b/Implab/Implab.csproj Mon Apr 16 19:43:18 2018 +0300 @@ -8,7 +8,7 @@ and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc. 2012-2018 Sergey Smirnov - 3.0.8 + 3.0.10 https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt https://implab.org https://hg.implab.org/pub/ImplabNet/ diff -r 3a6e18c432be -r 74e048cbaac8 Implab/Xml/JsonXmlCaseTransform.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Implab/Xml/JsonXmlCaseTransform.cs Mon Apr 16 19:43:18 2018 +0300 @@ -0,0 +1,11 @@ +using System; + +namespace Implab.Xml +{ + public enum JsonXmlCaseTransform + { + None, + UcFirst, + LcFirst + } +} \ No newline at end of file diff -r 3a6e18c432be -r 74e048cbaac8 Implab/Xml/JsonXmlReader.cs --- 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); diff -r 3a6e18c432be -r 74e048cbaac8 Implab/Xml/JsonXmlReaderOptions.cs --- a/Implab/Xml/JsonXmlReaderOptions.cs Mon Apr 16 18:43:49 2018 +0300 +++ b/Implab/Xml/JsonXmlReaderOptions.cs Mon Apr 16 19:43:18 2018 +0300 @@ -59,6 +59,8 @@ set; } + public JsonXmlCaseTransform CaseTransform { get; set; } + public object Clone() { return MemberwiseClone(); }