Auto status change to "Under Review"
@@ -0,0 +1,11 | |||
|
1 | using System; | |
|
2 | ||
|
3 | namespace Implab.Xml | |
|
4 | { | |
|
5 | public enum JsonXmlCaseTransform | |
|
6 | { | |
|
7 | None, | |
|
8 | UcFirst, | |
|
9 | LcFirst | |
|
10 | } | |
|
11 | } No newline at end of file |
@@ -167,7 +167,7 namespace Implab.Test { | |||
|
167 | 167 | CloseOutput = false, |
|
168 | 168 | ConformanceLevel = ConformanceLevel.Document |
|
169 | 169 | })) |
|
170 | using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", NodesPrefix = "json" })) { | |
|
170 | using (var xmlReader = new JsonXmlReader(JsonReader.ParseString(json), new JsonXmlReaderOptions { NamespaceUri = "JsonXmlReaderSimpleTest", RootName = "Data", NodesPrefix = "json", CaseTransform = JsonXmlCaseTransform.UcFirst, ArrayItemName = "Item" })) { | |
|
171 | 171 | xmlWriter.WriteNode(xmlReader, false); |
|
172 | 172 | } |
|
173 | 173 | Console.WriteLine(); |
@@ -8,7 +8,7 | |||
|
8 | 8 | and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc. |
|
9 | 9 | </Description> |
|
10 | 10 | <Copyright>2012-2018 Sergey Smirnov</Copyright> |
|
11 |
<Version>3.0. |
|
|
11 | <Version>3.0.10</Version> | |
|
12 | 12 | <PackageLicenseUrl>https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt</PackageLicenseUrl> |
|
13 | 13 | <PackageProjectUrl>https://implab.org</PackageProjectUrl> |
|
14 | 14 | <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl> |
@@ -56,6 +56,7 namespace Implab.Xml { | |||
|
56 | 56 | readonly string m_xmlnsNamespace; |
|
57 | 57 | readonly string m_xsiPrefix; |
|
58 | 58 | readonly string m_xsiNamespace; |
|
59 | readonly JsonXmlCaseTransform m_caseTransform; | |
|
59 | 60 | |
|
60 | 61 | |
|
61 | 62 | public JsonXmlReader(JsonReader parser, JsonXmlReaderOptions options) { |
@@ -76,6 +77,8 namespace Implab.Xml { | |||
|
76 | 77 | m_xsiPrefix = m_nameTable.Add(XmlNameContext.XsiPrefix); |
|
77 | 78 | m_xsiNamespace = m_nameTable.Add(XmlNameContext.XsiNamespace); |
|
78 | 79 | |
|
80 | m_caseTransform = m_options.CaseTransform; | |
|
81 | ||
|
79 | 82 | // TODO validate m_jsonRootName, m_jsonArrayItemName |
|
80 | 83 | |
|
81 | 84 | m_context = new XmlNameContext(null, 0); |
@@ -162,7 +165,7 namespace Implab.Xml { | |||
|
162 | 165 | return m_value; |
|
163 | 166 | } |
|
164 | 167 | } |
|
165 | ||
|
168 | ||
|
166 | 169 | public override string GetAttribute(int i) { |
|
167 | 170 | Safe.ArgumentInRange(i >= 0 && i < AttributeCount, nameof(i)); |
|
168 | 171 | return m_attributes[i].Value; |
@@ -224,7 +227,7 namespace Implab.Xml { | |||
|
224 | 227 | m_elementIsEmpty = m_isEmpty; |
|
225 | 228 | m_isAttribute = true; |
|
226 | 229 | } |
|
227 | ||
|
230 | ||
|
228 | 231 | var attr = m_attributes[i]; |
|
229 | 232 | |
|
230 | 233 | |
@@ -328,7 +331,7 namespace Implab.Xml { | |||
|
328 | 331 | foreach (var attr in attrs) { |
|
329 | 332 | if (attr.QName.Name == "xmlns") { |
|
330 | 333 | if (context == m_context) |
|
331 |
|
|
|
334 | context = new XmlNameContext(m_context, m_xmlDepth); | |
|
332 | 335 | context.DefinePrefix(attr.Value, string.Empty); |
|
333 | 336 | } else if (attr.Prefix == m_xmlnsPrefix) { |
|
334 | 337 | if (context == m_context) |
@@ -459,7 +462,7 namespace Implab.Xml { | |||
|
459 | 462 | } |
|
460 | 463 | |
|
461 | 464 | while (m_parser.Read()) { |
|
462 | var jsonName = m_nameTable.Add(m_parser.ElementName); | |
|
465 | var jsonName = m_nameTable.Add(TransformJsonName(m_parser.ElementName)); | |
|
463 | 466 | |
|
464 | 467 | switch (m_parser.ElementType) { |
|
465 | 468 | case JsonElementType.BeginObject: |
@@ -589,6 +592,16 namespace Implab.Xml { | |||
|
589 | 592 | return !skip; |
|
590 | 593 | } |
|
591 | 594 | |
|
595 | private string TransformJsonName(string name) { | |
|
596 | if (m_caseTransform == JsonXmlCaseTransform.None || string.IsNullOrEmpty(name)) { | |
|
597 | return name; | |
|
598 | } else if (m_caseTransform == JsonXmlCaseTransform.UcFirst) { | |
|
599 | return JsonXmlReader.UppercaseFirst(name); | |
|
600 | } else { | |
|
601 | return JsonXmlReader.LowercaseFirst(name); | |
|
602 | } | |
|
603 | } | |
|
604 | ||
|
592 | 605 | protected override void Dispose(bool disposing) { |
|
593 | 606 | if (disposing) |
|
594 | 607 | Safe.Dispose(m_parser); |
@@ -616,6 +629,21 namespace Implab.Xml { | |||
|
616 | 629 | |
|
617 | 630 | #region static methods |
|
618 | 631 | |
|
632 | // | |
|
633 | // Static Methods | |
|
634 | // | |
|
635 | private static string LowercaseFirst(string s) { | |
|
636 | char[] array = s.ToCharArray(); | |
|
637 | array[0] = char.ToLower(array[0]); | |
|
638 | return new string(array); | |
|
639 | } | |
|
640 | ||
|
641 | private static string UppercaseFirst(string s) { | |
|
642 | char[] array = s.ToCharArray(); | |
|
643 | array[0] = char.ToUpper(array[0]); | |
|
644 | return new string(array); | |
|
645 | } | |
|
646 | ||
|
619 | 647 | public static JsonXmlReader CreateJsonXmlReader(TextReader textReader, JsonXmlReaderOptions options = null) { |
|
620 | 648 | var jsonReader = JsonReader.Create(textReader); |
|
621 | 649 | return new JsonXmlReader(jsonReader, options); |
General Comments 3
ok, latest stable version should be in default
You need to be logged in to leave comments.
Login now