comparison Implab/Xml/JsonXmlReader.cs @ 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
comparison
equal deleted inserted replaced
264:3a6e18c432be 265:74e048cbaac8
54 54
55 readonly string m_xmlnsPrefix; 55 readonly string m_xmlnsPrefix;
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) {
62 Safe.ArgumentNotNull(parser, nameof(parser)); 63 Safe.ArgumentNotNull(parser, nameof(parser));
63 m_parser = parser; 64 m_parser = parser;
73 m_jsonPrefix = m_nameTable.Add(m_options.NodesPrefix ?? string.Empty); 74 m_jsonPrefix = m_nameTable.Add(m_options.NodesPrefix ?? string.Empty);
74 m_xmlnsPrefix = m_nameTable.Add(XmlNameContext.XmlnsPrefix); 75 m_xmlnsPrefix = m_nameTable.Add(XmlNameContext.XmlnsPrefix);
75 m_xmlnsNamespace = m_nameTable.Add(XmlNameContext.XmlnsNamespace); 76 m_xmlnsNamespace = m_nameTable.Add(XmlNameContext.XmlnsNamespace);
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);
79
80 m_caseTransform = m_options.CaseTransform;
78 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);
82 } 85 }
160 public override string Value { 163 public override string Value {
161 get { 164 get {
162 return m_value; 165 return m_value;
163 } 166 }
164 } 167 }
165 168
166 public override string GetAttribute(int i) { 169 public override string GetAttribute(int i) {
167 Safe.ArgumentInRange(i >= 0 && i < AttributeCount, nameof(i)); 170 Safe.ArgumentInRange(i >= 0 && i < AttributeCount, nameof(i));
168 return m_attributes[i].Value; 171 return m_attributes[i].Value;
169 } 172 }
170 173
222 m_elementDepth = m_xmlDepth; 225 m_elementDepth = m_xmlDepth;
223 m_elementPrefix = m_prefix; 226 m_elementPrefix = m_prefix;
224 m_elementIsEmpty = m_isEmpty; 227 m_elementIsEmpty = m_isEmpty;
225 m_isAttribute = true; 228 m_isAttribute = true;
226 } 229 }
227 230
228 var attr = m_attributes[i]; 231 var attr = m_attributes[i];
229 232
230 233
231 m_currentAttribute = i; 234 m_currentAttribute = i;
232 m_currentAttributeRead = false; 235 m_currentAttributeRead = false;
326 // define new namespaces 329 // define new namespaces
327 if (attrs != null) { 330 if (attrs != null) {
328 foreach (var attr in attrs) { 331 foreach (var attr in attrs) {
329 if (attr.QName.Name == "xmlns") { 332 if (attr.QName.Name == "xmlns") {
330 if (context == m_context) 333 if (context == m_context)
331 context = new XmlNameContext(m_context, m_xmlDepth); 334 context = new XmlNameContext(m_context, m_xmlDepth);
332 context.DefinePrefix(attr.Value, string.Empty); 335 context.DefinePrefix(attr.Value, string.Empty);
333 } else if (attr.Prefix == m_xmlnsPrefix) { 336 } else if (attr.Prefix == m_xmlnsPrefix) {
334 if (context == m_context) 337 if (context == m_context)
335 context = new XmlNameContext(m_context, m_xmlDepth); 338 context = new XmlNameContext(m_context, m_xmlDepth);
336 context.DefinePrefix(attr.Value, attr.QName.Name); 339 context.DefinePrefix(attr.Value, attr.QName.Name);
457 case JsonXmlReaderPosition.Error: 460 case JsonXmlReaderPosition.Error:
458 return false; 461 return false;
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:
466 if (!EnterJsonObject(jsonName, out elementName)) 469 if (!EnterJsonObject(jsonName, out elementName))
467 continue; 470 continue;
587 m_jsonSkip = prev.skip; 590 m_jsonSkip = prev.skip;
588 591
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);
595 base.Dispose(true); 608 base.Dispose(true);
596 } 609 }
614 } 627 }
615 } 628 }
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);
622 } 650 }
623 651