diff --git a/Implab.Test/JsonTests.cs b/Implab.Test/JsonTests.cs --- a/Implab.Test/JsonTests.cs +++ b/Implab.Test/JsonTests.cs @@ -167,7 +167,7 @@ namespace Implab.Test { 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 --git a/Implab/Implab.csproj b/Implab/Implab.csproj --- a/Implab/Implab.csproj +++ b/Implab/Implab.csproj @@ -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 --git a/Implab/Xml/JsonXmlCaseTransform.cs b/Implab/Xml/JsonXmlCaseTransform.cs new file mode 100644 --- /dev/null +++ b/Implab/Xml/JsonXmlCaseTransform.cs @@ -0,0 +1,11 @@ +using System; + +namespace Implab.Xml +{ + public enum JsonXmlCaseTransform + { + None, + UcFirst, + LcFirst + } +} \ No newline at end of file diff --git a/Implab/Xml/JsonXmlReader.cs b/Implab/Xml/JsonXmlReader.cs --- a/Implab/Xml/JsonXmlReader.cs +++ b/Implab/Xml/JsonXmlReader.cs @@ -56,6 +56,7 @@ namespace Implab.Xml { 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 @@ namespace Implab.Xml { 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 @@ namespace Implab.Xml { 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 @@ namespace Implab.Xml { m_elementIsEmpty = m_isEmpty; m_isAttribute = true; } - + var attr = m_attributes[i]; @@ -328,7 +331,7 @@ namespace Implab.Xml { 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 @@ namespace Implab.Xml { } 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 @@ namespace Implab.Xml { 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 @@ namespace Implab.Xml { #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 --git a/Implab/Xml/JsonXmlReaderOptions.cs b/Implab/Xml/JsonXmlReaderOptions.cs --- a/Implab/Xml/JsonXmlReaderOptions.cs +++ b/Implab/Xml/JsonXmlReaderOptions.cs @@ -59,6 +59,8 @@ namespace Implab.Xml { set; } + public JsonXmlCaseTransform CaseTransform { get; set; } + public object Clone() { return MemberwiseClone(); }