@@ -0,0 +1,61 | |||||
|
1 | using System; | |||
|
2 | using System.Collections.Generic; | |||
|
3 | using System.Linq; | |||
|
4 | using System.Text; | |||
|
5 | using System.Xml; | |||
|
6 | ||||
|
7 | namespace Implab.JSON { | |||
|
8 | /// <summary> | |||
|
9 | /// Набор необязательных параметров для <see cref="JSONXmlReader"/>, позволяющий управлять процессом | |||
|
10 | /// интерпретации <c>JSON</c> документа. | |||
|
11 | /// </summary> | |||
|
12 | public class JSONXmlReaderOptions { | |||
|
13 | /// <summary> | |||
|
14 | /// Пространство имен в котором будут располагаться читаемые элементы документа | |||
|
15 | /// </summary> | |||
|
16 | public string NamespaceURI { | |||
|
17 | get; | |||
|
18 | set; | |||
|
19 | } | |||
|
20 | ||||
|
21 | /// <summary> | |||
|
22 | /// Интерпретировать массивы как множественные элементы (убирает один уровень вложенности) | |||
|
23 | /// </summary> | |||
|
24 | public bool FlattenArrays { | |||
|
25 | get; | |||
|
26 | set; | |||
|
27 | } | |||
|
28 | ||||
|
29 | /// <summary> | |||
|
30 | /// Префикс, для узлов документа | |||
|
31 | /// </summary> | |||
|
32 | public string NodesPrefix { | |||
|
33 | get; | |||
|
34 | set; | |||
|
35 | } | |||
|
36 | ||||
|
37 | /// <summary> | |||
|
38 | /// Имя корневого элемента в xml документе | |||
|
39 | /// </summary> | |||
|
40 | public string RootName { | |||
|
41 | get; | |||
|
42 | set; | |||
|
43 | } | |||
|
44 | ||||
|
45 | /// <summary> | |||
|
46 | /// Имя элемента для массивов, если не включена опция <see cref="FlattenArrays"/>. | |||
|
47 | /// </summary> | |||
|
48 | public string ArrayItemName { | |||
|
49 | get; | |||
|
50 | set; | |||
|
51 | } | |||
|
52 | ||||
|
53 | /// <summary> | |||
|
54 | /// Таблица атомизированных строк для построения документа. | |||
|
55 | /// </summary> | |||
|
56 | public XmlNameTable NameTable { | |||
|
57 | get; | |||
|
58 | set; | |||
|
59 | } | |||
|
60 | } | |||
|
61 | } |
@@ -63,6 +63,7 | |||||
63 | <Compile Include="JSON\JsonTokenType.cs" /> |
|
63 | <Compile Include="JSON\JsonTokenType.cs" /> | |
64 | <Compile Include="JSON\JSONWriter.cs" /> |
|
64 | <Compile Include="JSON\JSONWriter.cs" /> | |
65 | <Compile Include="JSON\JSONXmlReader.cs" /> |
|
65 | <Compile Include="JSON\JSONXmlReader.cs" /> | |
|
66 | <Compile Include="JSON\JSONXmlReaderOptions.cs" /> | |||
66 | <Compile Include="JSON\StringTranslator.cs" /> |
|
67 | <Compile Include="JSON\StringTranslator.cs" /> | |
67 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
68 | <Compile Include="Parallels\DispatchPool.cs" /> | |
68 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
69 | <Compile Include="Parallels\ArrayTraits.cs" /> |
@@ -8,13 +8,16 using System.Threading.Tasks; | |||||
8 |
|
8 | |||
9 | namespace Implab.JSON { |
|
9 | namespace Implab.JSON { | |
10 | /// <summary> |
|
10 | /// <summary> | |
11 | /// Сканнер, разбивающий поток символов на токены JSON. |
|
11 | /// Сканнер (лексер), разбивающий поток символов на токены JSON. | |
12 | /// </summary> |
|
12 | /// </summary> | |
13 | public class JSONScanner : Scanner { |
|
13 | public class JSONScanner : Scanner { | |
14 | char[] m_stringBuffer; |
|
14 | char[] m_stringBuffer; | |
15 | DFAStateDescriptior[] m_stringDFA; |
|
15 | DFAStateDescriptior[] m_stringDFA; | |
16 | int[] m_stringAlphabet; |
|
16 | int[] m_stringAlphabet; | |
17 |
|
17 | |||
|
18 | /// <summary> | |||
|
19 | /// Создает новый экземпляр сканнера | |||
|
20 | /// </summary> | |||
18 | public JSONScanner() |
|
21 | public JSONScanner() | |
19 | : base(JSONGrammar.Instance.JsonDFA) { |
|
22 | : base(JSONGrammar.Instance.JsonDFA) { | |
20 | m_stringBuffer = new char[1024]; |
|
23 | m_stringBuffer = new char[1024]; | |
@@ -23,6 +26,14 namespace Implab.JSON { | |||||
23 | m_stringDFA = dfa.States; |
|
26 | m_stringDFA = dfa.States; | |
24 | } |
|
27 | } | |
25 |
|
28 | |||
|
29 | /// <summary> | |||
|
30 | /// Читает следующий лексический элемент из входных данных. | |||
|
31 | /// </summary> | |||
|
32 | /// <param name="tokenValue">Возвращает значение прочитанного токена.</param> | |||
|
33 | /// <param name="tokenType">Возвращает тип прочитанного токена.</param> | |||
|
34 | /// <returns><c>true</c> - чтение произведено успешно. <c>false</c> - достигнут конец входных данных</returns> | |||
|
35 | /// <remarks>В случе если токен не распознается, возникает исключение. Значения токенов обрабатываются, т.е. | |||
|
36 | /// в строках обрабатываются экранированные символы, числа становтся типа double.</remarks> | |||
26 | public bool ReadToken(out object tokenValue, out JsonTokenType tokenType) { |
|
37 | public bool ReadToken(out object tokenValue, out JsonTokenType tokenType) { | |
27 | if (ReadTokenInternal()) { |
|
38 | if (ReadTokenInternal()) { | |
28 | switch ((JSONGrammar.TokenType)m_currentState.tag[0]) { |
|
39 | switch ((JSONGrammar.TokenType)m_currentState.tag[0]) { |
@@ -1,14 +1,14 | |||||
1 | using Implab; |
|
1 | using Implab; | |
2 | using Implab.JSON; |
|
|||
3 | using Implab.Parsing; |
|
2 | using Implab.Parsing; | |
4 | using System; |
|
3 | using System; | |
5 | using System.Collections.Generic; |
|
4 | using System.Collections.Generic; | |
|
5 | using System.IO; | |||
6 | using System.Linq; |
|
6 | using System.Linq; | |
7 | using System.Text; |
|
7 | using System.Text; | |
8 | using System.Threading.Tasks; |
|
8 | using System.Threading.Tasks; | |
9 | using System.Xml; |
|
9 | using System.Xml; | |
10 |
|
10 | |||
11 |
namespace |
|
11 | namespace Implab.JSON { | |
12 | public class JSONXmlReader : XmlReader { |
|
12 | public class JSONXmlReader : XmlReader { | |
13 |
|
13 | |||
14 | enum ValueContext { |
|
14 | enum ValueContext { | |
@@ -29,16 +29,34 namespace ConsPlay { | |||||
29 | ReadState m_state = ReadState.Initial; |
|
29 | ReadState m_state = ReadState.Initial; | |
30 | Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>(); |
|
30 | Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>(); | |
31 | LocalNameContext m_localName; |
|
31 | LocalNameContext m_localName; | |
32 | string m_rootName = "json"; |
|
|||
33 | string m_prefix = String.Empty; |
|
|||
34 | string m_namespaceUri = String.Empty; |
|
|||
35 | bool m_flattenArrays = false; |
|
|||
36 | NameTable m_nameTable = new NameTable(); |
|
|||
37 | int m_depthCorrection = 0; |
|
32 | int m_depthCorrection = 0; | |
38 |
|
33 | |||
39 | public JSONXmlReader(JSONParser parser) { |
|
34 | readonly string m_rootName; | |
|
35 | readonly string m_prefix; | |||
|
36 | readonly string m_namespaceUri; | |||
|
37 | readonly bool m_flattenArrays; | |||
|
38 | readonly string m_arrayItemName; | |||
|
39 | readonly XmlNameTable m_nameTable; | |||
|
40 | ||||
|
41 | public JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) { | |||
40 | Safe.ArgumentNotNull(parser, "parser"); |
|
42 | Safe.ArgumentNotNull(parser, "parser"); | |
41 | m_parser = parser; |
|
43 | m_parser = parser; | |
|
44 | ||||
|
45 | if (options != null) { | |||
|
46 | m_prefix = options.NodesPrefix ?? String.Empty; | |||
|
47 | m_namespaceUri = options.NamespaceURI ?? String.Empty; | |||
|
48 | m_rootName = options.RootName ?? "json"; | |||
|
49 | m_flattenArrays = options.FlattenArrays; | |||
|
50 | m_arrayItemName = options.ArrayItemName ?? "item"; | |||
|
51 | m_nameTable = options.NameTable ?? new NameTable(); | |||
|
52 | } else { | |||
|
53 | m_prefix = String.Empty; | |||
|
54 | m_namespaceUri = String.Empty; | |||
|
55 | m_rootName = "json"; | |||
|
56 | m_flattenArrays = false; | |||
|
57 | m_arrayItemName = "item"; | |||
|
58 | m_nameTable = new NameTable(); | |||
|
59 | } | |||
42 | } |
|
60 | } | |
43 |
|
61 | |||
44 | /// <summary> |
|
62 | /// <summary> | |
@@ -194,7 +212,7 namespace ConsPlay { | |||||
194 | RestoreLocalName(); |
|
212 | RestoreLocalName(); | |
195 | break; |
|
213 | break; | |
196 | } |
|
214 | } | |
197 |
string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : |
|
215 | string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : m_arrayItemName; | |
198 | while (m_parser.Read()) { |
|
216 | while (m_parser.Read()) { | |
199 | if (!String.IsNullOrEmpty(m_parser.ElementName)) |
|
217 | if (!String.IsNullOrEmpty(m_parser.ElementName)) | |
200 | itemName = m_parser.ElementName; |
|
218 | itemName = m_parser.ElementName; | |
@@ -282,5 +300,11 namespace ConsPlay { | |||||
282 | base.Dispose(disposing); |
|
300 | base.Dispose(disposing); | |
283 | } |
|
301 | } | |
284 |
|
302 | |||
|
303 | ||||
|
304 | public static JSONXmlReader OpenFile(string file, JSONXmlReaderOptions options) { | |||
|
305 | var stream = File.OpenText(file); | |||
|
306 | var parser = new JSONParser(stream, true); | |||
|
307 | return new JSONXmlReader(parser, options); | |||
|
308 | } | |||
285 | } |
|
309 | } | |
286 | } |
|
310 | } |
@@ -167,7 +167,8 namespace Implab.Parsing { | |||||
167 | m_pointer++; |
|
167 | m_pointer++; | |
168 |
|
168 | |||
169 | if (m_pointer >= m_bufferSize) { |
|
169 | if (m_pointer >= m_bufferSize) { | |
170 |
|
|
170 | if (!ReadNextChunk()) | |
|
171 | return false; | |||
171 | } |
|
172 | } | |
172 |
|
173 | |||
173 | m_previewCode = m_alphabetMap[m_buffer[m_pointer]]; |
|
174 | m_previewCode = m_alphabetMap[m_buffer[m_pointer]]; |
General Comments 0
You need to be logged in to leave comments.
Login now