annotate Implab/Formats/Json/JsonReader.cs @ 239:eedf4d834e67 v2

fix
author cin
date Wed, 13 Dec 2017 19:54:45 +0300
parents 302ca905c19e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
1 using System;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
2 using System.Diagnostics;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
3 using System.IO;
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
4 using Implab.Automaton;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
5 using Implab.Automaton.RegularExpressions;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
6 using System.Linq;
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
7 using Implab.Components;
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
8 using System.Collections.Generic;
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
9 using System.Text;
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
10 using System.Globalization;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
11
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
12 namespace Implab.Formats.Json {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
13 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
14 /// Pull парсер JSON данных.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
15 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
16 /// <remarks>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
17 /// Следует отметить отдельную интерпретацию свойства <see cref="Level"/>,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
18 /// оно означает текущий уровень вложенности объектов, однако закрывающий
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
19 /// элемент объекта и массива имеет уровень меньше, чем сам объект.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
20 /// <code>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
21 /// { // Level = 1
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
22 /// "name" : "Peter", // Level = 1
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
23 /// "address" : { // Level = 2
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
24 /// city : "Stern" // Level = 2
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
25 /// } // Level = 1
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
26 /// } // Level = 0
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
27 /// </code>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
28 /// </remarks>
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
29 public class JsonReader : Disposable {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
30
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
31 enum MemberContext {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
32 MemberName,
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
33 MemberValue
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
34 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
35
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
36 #region Parser rules
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
37 struct ParserContext {
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
38 readonly int[,] m_dfa;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
39 int m_state;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
40
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
41 readonly JsonElementContext m_elementContext;
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
42
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
43 public ParserContext(int[,] dfa, int state, JsonElementContext context) {
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
44 m_dfa = dfa;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
45 m_state = state;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
46 m_elementContext = context;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
47 }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
48
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
49 public bool Move(JsonTokenType token) {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
50 var next = m_dfa[m_state, (int)token];
236
302ca905c19e JsonReader optimizations
cin
parents: 230
diff changeset
51 if (next == AutomatonConst.UnreachableState)
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
52 return false;
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
53 m_state = next;
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
54 return true;
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
55 }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
56
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
57 public JsonElementContext ElementContext {
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
58 get { return m_elementContext; }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
59 }
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
60 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
61
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
62 static readonly ParserContext _jsonContext;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
63 static readonly ParserContext _objectContext;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
64 static readonly ParserContext _arrayContext;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
65
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
66 static JsonReader() {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
67
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
68 var valueExpression = MakeToken(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String);
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
69 var memberExpression = MakeToken(JsonTokenType.String).Cat(MakeToken(JsonTokenType.NameSeparator)).Cat(valueExpression);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
70
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
71 var objectExpression = memberExpression
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
72 .Cat(
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
73 MakeToken(JsonTokenType.ValueSeparator)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
74 .Cat(memberExpression)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
75 .EClosure()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
76 )
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
77 .Optional()
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
78 .Cat(MakeToken(JsonTokenType.EndObject))
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
79 .End();
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
80
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
81 var arrayExpression = valueExpression
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
82 .Cat(
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
83 MakeToken(JsonTokenType.ValueSeparator)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
84 .Cat(valueExpression)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
85 .EClosure()
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
86 )
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
87 .Optional()
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
88 .Cat(MakeToken(JsonTokenType.EndArray))
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
89 .End();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
90
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
91 var jsonExpression = valueExpression.End();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
92
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
93 _jsonContext = CreateParserContext(jsonExpression, JsonElementContext.None);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
94 _objectContext = CreateParserContext(objectExpression, JsonElementContext.Object);
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
95 _arrayContext = CreateParserContext(arrayExpression, JsonElementContext.Array);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
96 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
97
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
98 static Token MakeToken(params JsonTokenType[] input) {
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
99 return Token.New( input.Select(t => (int)t).ToArray() );
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
100 }
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
101
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
102 static ParserContext CreateParserContext(Token expr, JsonElementContext context) {
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
103
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
104 var dfa = new DFATable();
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
105 var builder = new RegularExpressionVisitor(dfa);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
106 expr.Accept(builder);
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
107 builder.BuildDFA();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
108
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
109 return new ParserContext(dfa.CreateTransitionTable(), dfa.InitialState, context);
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
110 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
111
178
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
112 #endregion
d5c5db0335ee working on JSON parser
cin
parents: 172
diff changeset
113
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
114 readonly JsonScanner m_scanner;
227
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents: 208
diff changeset
115 // json starts from the value context and may content even a single literal
8d5de4eb9c2c Reimplemented JsonXmlReader, added support for null values: JSON null values are
cin
parents: 208
diff changeset
116 MemberContext m_memberContext = MemberContext.MemberValue;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
117
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
118 JsonElementType m_elementType;
236
302ca905c19e JsonReader optimizations
cin
parents: 230
diff changeset
119 string m_elementValue;
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
120 string m_memberName = String.Empty;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
121
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
122 Stack<ParserContext> m_stack = new Stack<ParserContext>();
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
123 ParserContext m_context = _jsonContext;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
124
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
125 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
126 /// Создает новый парсер на основе строки, содержащей JSON
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
127 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
128 /// <param name="text"></param>
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
129 JsonReader(JsonScanner scanner) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
130 m_scanner = scanner;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
131 }
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
132
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
133 public int Level {
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
134 get { return m_stack.Count; }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
135 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
136
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
137 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
138 /// Тип текущего элемента на котором стоит парсер.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
139 /// </summary>
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
140 public JsonElementType ElementType {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
141 get { return m_elementType; }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
142 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
143
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
144 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
145 /// Имя элемента - имя свойства родительского контейнера. Для элементов массивов и корневого всегда
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
146 /// пустая строка.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
147 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
148 public string ElementName {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
149 get { return m_memberName; }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
150 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
151
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
152 /// <summary>
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
153 /// Значение элемента. Только для элементов типа <see cref="JsonElementType.Value"/>, для остальных <c>null</c>
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
154 /// </summary>
236
302ca905c19e JsonReader optimizations
cin
parents: 230
diff changeset
155 public string ElementValue {
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
156 get { return m_elementValue; }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
157 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
158
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
159 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
160 /// Читает слеюудущий объект из потока
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
161 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
162 /// <returns><c>true</c> - операция чтения прошла успешно, <c>false</c> - конец данных</returns>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
163 public bool Read() {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
164 string tokenValue;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
165 JsonTokenType tokenType;
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
166
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
167 m_memberName = String.Empty;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
168
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
169 while (m_scanner.ReadToken(out tokenValue, out tokenType)) {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
170 if(!m_context.Move(tokenType))
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
171 UnexpectedToken(tokenValue, tokenType);
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
172
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
173 switch (tokenType) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
174 case JsonTokenType.BeginObject:
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
175 m_stack.Push(m_context);
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
176 m_context = _objectContext;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
177
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
178 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
179 m_memberContext = MemberContext.MemberName;
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
180 m_elementType = JsonElementType.BeginObject;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
181 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
182 case JsonTokenType.EndObject:
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
183 if (m_stack.Count == 0)
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
184 UnexpectedToken(tokenValue, tokenType);
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
185 m_context = m_stack.Pop();
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
186
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
187 m_elementValue = null;
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
188 m_elementType = JsonElementType.EndObject;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
189 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
190 case JsonTokenType.BeginArray:
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
191 m_stack.Push(m_context);
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
192 m_context = _arrayContext;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
193
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
194 m_elementValue = null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
195 m_memberContext = MemberContext.MemberValue;
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
196 m_elementType = JsonElementType.BeginArray;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
197 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
198 case JsonTokenType.EndArray:
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
199 if (m_stack.Count == 0)
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
200 UnexpectedToken(tokenValue, tokenType);
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
201 m_context = m_stack.Pop();
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
202
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
203 m_elementValue = null;
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
204 m_elementType = JsonElementType.EndArray;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
205 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
206 case JsonTokenType.String:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
207 if (m_memberContext == MemberContext.MemberName) {
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
208 m_memberName = tokenValue;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
209 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
210 }
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
211 m_elementType = JsonElementType.Value;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
212 m_elementValue = tokenValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
213 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
214 case JsonTokenType.Number:
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
215 m_elementType = JsonElementType.Value;
236
302ca905c19e JsonReader optimizations
cin
parents: 230
diff changeset
216 m_elementValue = tokenValue;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
217 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
218 case JsonTokenType.Literal:
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
219 m_elementType = JsonElementType.Value;
236
302ca905c19e JsonReader optimizations
cin
parents: 230
diff changeset
220 m_elementValue = tokenValue == "null" ? null : tokenValue;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
221 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
222 case JsonTokenType.NameSeparator:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
223 m_memberContext = MemberContext.MemberValue;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
224 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
225 case JsonTokenType.ValueSeparator:
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
226 m_memberContext = m_context.ElementContext == JsonElementContext.Object ? MemberContext.MemberName : MemberContext.MemberValue;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
227 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
228 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
229 UnexpectedToken(tokenValue, tokenType);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
230 break;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
231 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
232 }
228
6fa235c5a760 Rewritten JsonScanner, JsonParser, fixed naming style
cin
parents: 227
diff changeset
233 if (m_context.ElementContext != JsonElementContext.None)
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
234 throw new ParserException("Unexpedted end of data");
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
235
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
236 Eof = true;
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
237
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
238 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
239 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
240
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
241 object ParseLiteral(string literal) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
242 switch (literal) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
243 case "null":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
244 return null;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
245 case "false":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
246 return false;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
247 case "true":
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
248 return true;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
249 default:
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
250 UnexpectedToken(literal, JsonTokenType.Literal);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
251 return null; // avoid compliler error
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
252 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
253 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
254
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
255 void UnexpectedToken(object value, JsonTokenType tokenType) {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
256 throw new ParserException(String.Format("Unexpected token {0}: '{1}'", tokenType, value));
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
257 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
258
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
259
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
260 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
261 /// Признак конца потока
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
262 /// </summary>
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
263 public bool Eof {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
264 get;
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
265 private set;
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
266 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
267
165
e227e78d72e4 DFA refactoring
cin
parents: 163
diff changeset
268 protected override void Dispose(bool disposing) {
180
c32688129f14 refactoring complete, JSONParser rewritten
cin
parents: 178
diff changeset
269 if (disposing)
208
7d07503621fe RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)
cin
parents: 180
diff changeset
270 m_scanner.Dispose();
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
271 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
272
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
273 /// <summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
274 /// Переходит в конец текущего объекта.
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
275 /// </summary>
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
276 public void SeekElementEnd() {
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
277 var level = Level - 1;
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
278
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
279 Debug.Assert(level >= 0);
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
280
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
281 while (Level != level)
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
282 Read();
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
283 }
229
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
284
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
285 public static JsonReader Create(string file, Encoding encoding) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
286 return new JsonReader(JsonTextScanner.Create(file, encoding));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
287 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
288
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
289 public static JsonReader Create(string file) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
290 return new JsonReader(JsonTextScanner.Create(file));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
291 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
292
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
293 public static JsonReader Create(Stream stream, Encoding encoding) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
294 return new JsonReader(JsonTextScanner.Create(stream, encoding));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
295 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
296
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
297 public static JsonReader Create(Stream stream) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
298 return new JsonReader(JsonTextScanner.Create(stream));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
299 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
300
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
301 public static JsonReader Create(TextReader reader) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
302 return new JsonReader(JsonTextScanner.Create(reader));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
303 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
304
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
305 public static JsonReader ParseString(string data) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
306 return new JsonReader(JsonStringScanner.Create(data));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
307 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
308
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
309 public static JsonReader ParseString(string data, int offset, int length) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
310 return new JsonReader(JsonStringScanner.Create(data, offset, length));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
311 }
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
312
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
313 public static JsonReader ParseString(char[] data, int offset, int lenght) {
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
314 return new JsonReader(JsonStringScanner.Create(data, offset, lenght));
5f7a3e1d32b9 JsonXmlReader performance tuning
cin
parents: 228
diff changeset
315 }
163
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
316 }
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
317
419aa51b04fd JSON moved to Formats namespace
cin
parents:
diff changeset
318 }