comparison Implab/Xml/JsonXmlReader.cs @ 228:6fa235c5a760 v2

Rewritten JsonScanner, JsonParser, fixed naming style
author cin
date Tue, 12 Sep 2017 01:19:12 +0300
parents 8d5de4eb9c2c
children 5f7a3e1d32b9
comparison
equal deleted inserted replaced
227:8d5de4eb9c2c 228:6fa235c5a760
1 using Implab.Formats.JSON; 1 using Implab.Formats.Json;
2 using System; 2 using System;
3 using System.Collections.Generic; 3 using System.Collections.Generic;
4 using System.Globalization; 4 using System.Globalization;
5 using System.Linq; 5 using System.Linq;
6 using System.Text;
7 using System.Threading.Tasks;
8 using System.Xml; 6 using System.Xml;
9 7
10 namespace Implab.Xml { 8 namespace Implab.Xml {
11 public class JsonXmlReader : XmlReader { 9 public class JsonXmlReader : XmlReader {
12 struct JsonContext { 10 struct JsonContext {
13 public string localName; 11 public string localName;
14 public bool skip; 12 public bool skip;
15 } 13 }
16 14
17 JSONParser m_parser; 15 JsonParser m_parser;
18 JsonXmlReaderOptions m_options; 16 JsonXmlReaderOptions m_options;
19 JsonXmlReaderPosition m_position = JsonXmlReaderPosition.Initial; 17 JsonXmlReaderPosition m_position = JsonXmlReaderPosition.Initial;
20 XmlNameTable m_nameTable; 18 XmlNameTable m_nameTable;
21 19
22 readonly string m_jsonRootName; 20 readonly string m_jsonRootName;
50 int m_currentAttribute; 48 int m_currentAttribute;
51 bool m_currentAttributeRead; 49 bool m_currentAttributeRead;
52 50
53 51
54 XmlNameContext m_context; 52 XmlNameContext m_context;
55 int m_nextPrefix = 1;
56 53
57 readonly string m_xmlnsPrefix; 54 readonly string m_xmlnsPrefix;
58 readonly string m_xmlnsNamespace; 55 readonly string m_xmlnsNamespace;
59 readonly string m_xsiPrefix; 56 readonly string m_xsiPrefix;
60 readonly string m_xsiNamespace; 57 readonly string m_xsiNamespace;
61 58
62 59
63 public JsonXmlReader(JSONParser parser, JsonXmlReaderOptions options) { 60 public JsonXmlReader(JsonParser parser, JsonXmlReaderOptions options) {
64 Safe.ArgumentNotNull(parser, nameof(parser)); 61 Safe.ArgumentNotNull(parser, nameof(parser));
65 m_parser = parser; 62 m_parser = parser;
66 63
67 m_options = options ?? new JsonXmlReaderOptions(); 64 m_options = options ?? new JsonXmlReaderOptions();
68 65
478 475
479 while (m_parser.Read()) { 476 while (m_parser.Read()) {
480 var jsonName = m_nameTable.Add(m_parser.ElementName); 477 var jsonName = m_nameTable.Add(m_parser.ElementName);
481 478
482 switch (m_parser.ElementType) { 479 switch (m_parser.ElementType) {
483 case JSONElementType.BeginObject: 480 case JsonElementType.BeginObject:
484 if (!EnterJsonObject(jsonName, out elementName)) 481 if (!EnterJsonObject(jsonName, out elementName))
485 continue; 482 continue;
486 483
487 m_position = JsonXmlReaderPosition.BeginObject; 484 m_position = JsonXmlReaderPosition.BeginObject;
488 ElementNode(elementName, m_jsonNamespace, elementAttrs, false); 485 ElementNode(elementName, m_jsonNamespace, elementAttrs, false);
489 break; 486 break;
490 case JSONElementType.EndObject: 487 case JsonElementType.EndObject:
491 if (!LeaveJsonScope(out elementName)) 488 if (!LeaveJsonScope(out elementName))
492 continue; 489 continue;
493 490
494 m_position = JsonXmlReaderPosition.EndObject; 491 m_position = JsonXmlReaderPosition.EndObject;
495 EndElementNode(elementName, m_jsonNamespace); 492 EndElementNode(elementName, m_jsonNamespace);
496 break; 493 break;
497 case JSONElementType.BeginArray: 494 case JsonElementType.BeginArray:
498 if (!EnterJsonArray(jsonName, out elementName)) 495 if (!EnterJsonArray(jsonName, out elementName))
499 continue; 496 continue;
500 497
501 m_position = JsonXmlReaderPosition.BeginArray; 498 m_position = JsonXmlReaderPosition.BeginArray;
502 ElementNode(elementName, m_jsonNamespace, elementAttrs, false); 499 ElementNode(elementName, m_jsonNamespace, elementAttrs, false);
503 break; 500 break;
504 case JSONElementType.EndArray: 501 case JsonElementType.EndArray:
505 if (!LeaveJsonScope(out elementName)) 502 if (!LeaveJsonScope(out elementName))
506 continue; 503 continue;
507 504
508 m_position = JsonXmlReaderPosition.EndArray; 505 m_position = JsonXmlReaderPosition.EndArray;
509 EndElementNode(elementName, m_jsonNamespace); 506 EndElementNode(elementName, m_jsonNamespace);
510 break; 507 break;
511 case JSONElementType.Value: 508 case JsonElementType.Value:
512 if (!VisitJsonValue(jsonName, out m_jsonValueName)) 509 if (!VisitJsonValue(jsonName, out m_jsonValueName))
513 continue; 510 continue;
514 511
515 m_position = JsonXmlReaderPosition.ValueElement; 512 m_position = JsonXmlReaderPosition.ValueElement;
516 if (m_parser.ElementValue == null) 513 if (m_parser.ElementValue == null)