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 | CloseOutput = false, |
|
167 | CloseOutput = false, | |
168 | ConformanceLevel = ConformanceLevel.Document |
|
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 | xmlWriter.WriteNode(xmlReader, false); |
|
171 | xmlWriter.WriteNode(xmlReader, false); | |
172 | } |
|
172 | } | |
173 | Console.WriteLine(); |
|
173 | Console.WriteLine(); |
@@ -8,7 +8,7 | |||||
8 | and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc. |
|
8 | and SharedLock, Trace helpers on top of System.Diagnostics, ObjectPool etc. | |
9 | </Description> |
|
9 | </Description> | |
10 | <Copyright>2012-2018 Sergey Smirnov</Copyright> |
|
10 | <Copyright>2012-2018 Sergey Smirnov</Copyright> | |
11 |
<Version>3.0. |
|
11 | <Version>3.0.10</Version> | |
12 | <PackageLicenseUrl>https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt</PackageLicenseUrl> |
|
12 | <PackageLicenseUrl>https://hg.implab.org/pub/ImplabNet/file/tip/Implab/license.txt</PackageLicenseUrl> | |
13 | <PackageProjectUrl>https://implab.org</PackageProjectUrl> |
|
13 | <PackageProjectUrl>https://implab.org</PackageProjectUrl> | |
14 | <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl> |
|
14 | <RepositoryUrl>https://hg.implab.org/pub/ImplabNet/</RepositoryUrl> |
@@ -56,6 +56,7 namespace Implab.Xml { | |||||
56 | readonly string m_xmlnsNamespace; |
|
56 | readonly string m_xmlnsNamespace; | |
57 | readonly string m_xsiPrefix; |
|
57 | readonly string m_xsiPrefix; | |
58 | readonly string m_xsiNamespace; |
|
58 | readonly string m_xsiNamespace; | |
|
59 | readonly JsonXmlCaseTransform m_caseTransform; | |||
59 |
|
60 | |||
60 |
|
61 | |||
61 | public JsonXmlReader(JsonReader parser, JsonXmlReaderOptions options) { |
|
62 | public JsonXmlReader(JsonReader parser, JsonXmlReaderOptions options) { | |
@@ -76,6 +77,8 namespace Implab.Xml { | |||||
76 | m_xsiPrefix = m_nameTable.Add(XmlNameContext.XsiPrefix); |
|
77 | m_xsiPrefix = m_nameTable.Add(XmlNameContext.XsiPrefix); | |
77 | m_xsiNamespace = m_nameTable.Add(XmlNameContext.XsiNamespace); |
|
78 | m_xsiNamespace = m_nameTable.Add(XmlNameContext.XsiNamespace); | |
78 |
|
79 | |||
|
80 | m_caseTransform = m_options.CaseTransform; | |||
|
81 | ||||
79 | // TODO validate m_jsonRootName, m_jsonArrayItemName |
|
82 | // TODO validate m_jsonRootName, m_jsonArrayItemName | |
80 |
|
83 | |||
81 | m_context = new XmlNameContext(null, 0); |
|
84 | m_context = new XmlNameContext(null, 0); | |
@@ -459,7 +462,7 namespace Implab.Xml { | |||||
459 | } |
|
462 | } | |
460 |
|
463 | |||
461 | while (m_parser.Read()) { |
|
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 | switch (m_parser.ElementType) { |
|
467 | switch (m_parser.ElementType) { | |
465 | case JsonElementType.BeginObject: |
|
468 | case JsonElementType.BeginObject: | |
@@ -589,6 +592,16 namespace Implab.Xml { | |||||
589 | return !skip; |
|
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 | protected override void Dispose(bool disposing) { |
|
605 | protected override void Dispose(bool disposing) { | |
593 | if (disposing) |
|
606 | if (disposing) | |
594 | Safe.Dispose(m_parser); |
|
607 | Safe.Dispose(m_parser); | |
@@ -616,6 +629,21 namespace Implab.Xml { | |||||
616 |
|
629 | |||
617 | #region static methods |
|
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 | public static JsonXmlReader CreateJsonXmlReader(TextReader textReader, JsonXmlReaderOptions options = null) { |
|
647 | public static JsonXmlReader CreateJsonXmlReader(TextReader textReader, JsonXmlReaderOptions options = null) { | |
620 | var jsonReader = JsonReader.Create(textReader); |
|
648 | var jsonReader = JsonReader.Create(textReader); | |
621 | return new JsonXmlReader(jsonReader, options); |
|
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