Mercurial > pub > ImplabNet
changeset 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 | 254d1f255d87 |
files | Implab.Test/JsonTests.cs Implab/Implab.csproj Implab/Xml/JsonXmlCaseTransform.cs Implab/Xml/JsonXmlReader.cs Implab/Xml/JsonXmlReaderOptions.cs |
diffstat | 5 files changed, 47 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- 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();
--- 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. </Description> <Copyright>2012-2018 Sergey Smirnov</Copyright> - <Version>3.0.8</Version> + <Version>3.0.10</Version> <PackageLicenseUrl>https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt</PackageLicenseUrl> <PackageProjectUrl>https://implab.org</PackageProjectUrl> <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl>
--- /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
--- 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);