annotate Implab/JSON/JSONParser.cs @ 154:2dcdee4c0810 v2

docs
author cin
date Fri, 12 Feb 2016 01:00:11 +0300
parents f973c5df9972
children 97fbbf816844
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
1 using Implab;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
2 using Implab.Parsing;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
3 using System;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
4 using System.Collections.Generic;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
5 using System.Diagnostics;
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
6 using System.IO;
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
7 using System.Linq;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
8 using System.Text;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
9 using System.Threading.Tasks;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
10
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
11 namespace Implab.JSON {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
12 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
13 /// internal
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
14 /// </summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
15 public struct JSONParserContext {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
16 public string memberName;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
17 public JSONElementContext elementContext;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
18 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
19
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
20 /// <summary>
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
21 /// Pull парсер JSON данных.
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
22 /// </summary>
70
0349189d2564 refactoring
cin
parents: 62
diff changeset
23 /// <remarks>
0349189d2564 refactoring
cin
parents: 62
diff changeset
24 /// Следует отметить отдельную интерпретацию свойства <see cref="Level"/>,
0349189d2564 refactoring
cin
parents: 62
diff changeset
25 /// оно означает текущий уровень вложенности объектов, однако закрывающий
0349189d2564 refactoring
cin
parents: 62
diff changeset
26 /// элемент объекта и массива имеет уровень меньше, чем сам объект.
0349189d2564 refactoring
cin
parents: 62
diff changeset
27 /// <code>
0349189d2564 refactoring
cin
parents: 62
diff changeset
28 /// { // Level = 1
0349189d2564 refactoring
cin
parents: 62
diff changeset
29 /// "name" : "Peter", // Level = 1
0349189d2564 refactoring
cin
parents: 62
diff changeset
30 /// "address" : { // Level = 2
0349189d2564 refactoring
cin
parents: 62
diff changeset
31 /// city : "Stern" // Level = 2
0349189d2564 refactoring
cin
parents: 62
diff changeset
32 /// } // Level = 1
0349189d2564 refactoring
cin
parents: 62
diff changeset
33 /// } // Level = 0
0349189d2564 refactoring
cin
parents: 62
diff changeset
34 /// </code>
0349189d2564 refactoring
cin
parents: 62
diff changeset
35 /// </remarks>
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
36 public class JSONParser : DFAutomaton<JSONParserContext>, IDisposable {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
37
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
38 enum MemberContext {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
39 MemberName,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
40 MemberValue
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
41 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
42
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
43 static readonly EnumAlphabet<JsonTokenType> _alphabet = EnumAlphabet<JsonTokenType>.FullAlphabet;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
44 static readonly DFAStateDescriptior[] _jsonDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
45 static readonly DFAStateDescriptior[] _objectDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
46 static readonly DFAStateDescriptior[] _arrayDFA;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
47
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
48 static JSONParser() {
140
cin
parents: 70
diff changeset
49
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
50
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
51 var valueExpression = Token.New(JsonTokenType.BeginArray, JsonTokenType.BeginObject, JsonTokenType.Literal, JsonTokenType.Number, JsonTokenType.String);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
52 var memberExpression = Token.New(JsonTokenType.String).Cat(Token.New(JsonTokenType.NameSeparator)).Cat(valueExpression);
140
cin
parents: 70
diff changeset
53
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
54 var objectExpression = memberExpression
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
55 .Cat(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
56 Token.New(JsonTokenType.ValueSeparator)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
57 .Cat(memberExpression)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
58 .EClosure()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
59 )
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
60 .Optional()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
61 .Cat(Token.New(JsonTokenType.EndObject))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
62 .Tag(0);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
63 var arrayExpression = valueExpression
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
64 .Cat(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
65 Token.New(JsonTokenType.ValueSeparator)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
66 .Cat(valueExpression)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
67 .EClosure()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
68 )
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
69 .Optional()
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
70 .Cat(Token.New(JsonTokenType.EndArray))
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
71 .Tag(0);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
72
140
cin
parents: 70
diff changeset
73 var jsonExpression = valueExpression.Tag(0);
cin
parents: 70
diff changeset
74
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
75 _jsonDFA = BuildDFA(jsonExpression).States;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
76 _objectDFA = BuildDFA(objectExpression).States;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
77 _arrayDFA = BuildDFA(arrayExpression).States;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
78 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
79
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
80 static EDFADefinition<JsonTokenType> BuildDFA(Token expr) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
81 var builder = new DFABuilder();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
82 var dfa = new EDFADefinition<JsonTokenType>(_alphabet);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
83 expr.Accept(builder);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
84
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
85 builder.BuildDFA(dfa);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
86 return dfa;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
87 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
88
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
89 JSONScanner m_scanner;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
90 MemberContext m_memberContext;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
91
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
92 JSONElementType m_elementType;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
93 object m_elementValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
94
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
95 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
96 /// Создает новый парсер на основе строки, содержащей JSON
21611344d366 code cleanup
cin
parents: 57
diff changeset
97 /// </summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
98 /// <param name="text"></param>
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
99 public JSONParser(string text)
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
100 : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
101 Safe.ArgumentNotEmpty(text, "text");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
102 m_scanner = new JSONScanner();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
103 m_scanner.Feed(text.ToCharArray());
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
104 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
105
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
106 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
107 /// Создает новый экземпляр парсера, на основе текстового потока.
21611344d366 code cleanup
cin
parents: 57
diff changeset
108 /// </summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
109 /// <param name="reader">Текстовый поток.</param>
21611344d366 code cleanup
cin
parents: 57
diff changeset
110 /// <param name="dispose">Признак того, что парсер должен конролировать время жизни входного потока.</param>
21611344d366 code cleanup
cin
parents: 57
diff changeset
111 public JSONParser(TextReader reader, bool dispose)
21611344d366 code cleanup
cin
parents: 57
diff changeset
112 : base(_jsonDFA, INITIAL_STATE, new JSONParserContext { elementContext = JSONElementContext.None, memberName = String.Empty }) {
21611344d366 code cleanup
cin
parents: 57
diff changeset
113 Safe.ArgumentNotNull(reader, "reader");
21611344d366 code cleanup
cin
parents: 57
diff changeset
114 m_scanner = new JSONScanner();
21611344d366 code cleanup
cin
parents: 57
diff changeset
115 m_scanner.Feed(reader, dispose);
21611344d366 code cleanup
cin
parents: 57
diff changeset
116 }
21611344d366 code cleanup
cin
parents: 57
diff changeset
117
21611344d366 code cleanup
cin
parents: 57
diff changeset
118 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
119 /// Тип текущего элемента на котором стоит парсер.
21611344d366 code cleanup
cin
parents: 57
diff changeset
120 /// </summary>
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
121 public JSONElementType ElementType {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
122 get { return m_elementType; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
123 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
124
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
125 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
126 /// Имя элемента - имя свойства родительского контейнера. Для элементов массивов и корневого всегда
21611344d366 code cleanup
cin
parents: 57
diff changeset
127 /// пустая строка.
21611344d366 code cleanup
cin
parents: 57
diff changeset
128 /// </summary>
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
129 public string ElementName {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
130 get { return m_context.info.memberName; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
131 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
132
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
133 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
134 /// Значение элемента. Только для элементов типа <see cref="JSONElementType.Value"/>, для остальных <c>null</c>
21611344d366 code cleanup
cin
parents: 57
diff changeset
135 /// </summary>
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
136 public object ElementValue {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
137 get { return m_elementValue; }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
138 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
139
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
140 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
141 /// Читает слеюудущий объект из потока
21611344d366 code cleanup
cin
parents: 57
diff changeset
142 /// </summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
143 /// <returns><c>true</c> - операция чтения прошла успешно, <c>false</c> - конец данных</returns>
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
144 public bool Read() {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
145 if (m_context.current == UNREACHEBLE_STATE)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
146 throw new InvalidOperationException("The parser is in invalid state");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
147 object tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
148 JsonTokenType tokenType;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
149 m_context.info.memberName = String.Empty;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
150 while (m_scanner.ReadToken(out tokenValue, out tokenType)) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
151 Move((int)tokenType);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
152 if (m_context.current == UNREACHEBLE_STATE)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
153 UnexpectedToken(tokenValue, tokenType);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
154 switch (tokenType) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
155 case JsonTokenType.BeginObject:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
156 Switch(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
157 _objectDFA,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
158 INITIAL_STATE,
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
159 new JSONParserContext {
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
160 memberName = m_context.info.memberName,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
161 elementContext = JSONElementContext.Object
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
162 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
163 );
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
164 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
165 m_memberContext = MemberContext.MemberName;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
166 m_elementType = JSONElementType.BeginObject;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
167 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
168 case JsonTokenType.EndObject:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
169 Restore();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
170 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
171 m_elementType = JSONElementType.EndObject;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
172 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
173 case JsonTokenType.BeginArray:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
174 Switch(
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
175 _arrayDFA,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
176 INITIAL_STATE,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
177 new JSONParserContext {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
178 memberName = m_context.info.memberName,
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
179 elementContext = JSONElementContext.Array
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
180 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
181 );
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
182 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
183 m_memberContext = MemberContext.MemberValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
184 m_elementType = JSONElementType.BeginArray;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
185 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
186 case JsonTokenType.EndArray:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
187 Restore();
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
188 m_elementValue = null;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
189 m_elementType = JSONElementType.EndArray;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
190 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
191 case JsonTokenType.String:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
192 if (m_memberContext == MemberContext.MemberName) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
193 m_context.info.memberName = (string)tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
194 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
195 } else {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
196 m_elementType = JSONElementType.Value;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
197 m_elementValue = tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
198 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
199 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
200 case JsonTokenType.Number:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
201 m_elementType = JSONElementType.Value;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
202 m_elementValue = tokenValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
203 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
204 case JsonTokenType.Literal:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
205 m_elementType = JSONElementType.Value;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
206 m_elementValue = ParseLiteral((string)tokenValue);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
207 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
208 case JsonTokenType.NameSeparator:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
209 m_memberContext = MemberContext.MemberValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
210 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
211 case JsonTokenType.ValueSeparator:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
212 m_memberContext = m_context.info.elementContext == JSONElementContext.Object ? MemberContext.MemberName : MemberContext.MemberValue;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
213 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
214 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
215 UnexpectedToken(tokenValue, tokenType);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
216 break;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
217 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
218 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
219 if (m_context.info.elementContext != JSONElementContext.None)
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
220 throw new ParserException("Unexpedted end of data");
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
221 return false;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
222 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
223
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
224 object ParseLiteral(string literal) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
225 switch (literal) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
226 case "null":
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
227 return null;
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
228 case "false":
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
229 return false;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
230 case "true":
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
231 return true;
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
232 default:
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
233 UnexpectedToken(literal, JsonTokenType.Literal);
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
234 return null; // avoid compliler error
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
235 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
236 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
237
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
238 void UnexpectedToken(object value, JsonTokenType tokenType) {
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
239 throw new ParserException(String.Format("Unexpected token {0}: '{1}'", tokenType, value));
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
240 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
241
57
7759c80cad95 minor changes
cin
parents: 55
diff changeset
242
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
243 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
244 /// Признак конца потока
21611344d366 code cleanup
cin
parents: 57
diff changeset
245 /// </summary>
57
7759c80cad95 minor changes
cin
parents: 55
diff changeset
246 public bool EOF {
7759c80cad95 minor changes
cin
parents: 55
diff changeset
247 get {
7759c80cad95 minor changes
cin
parents: 55
diff changeset
248 return m_scanner.EOF;
7759c80cad95 minor changes
cin
parents: 55
diff changeset
249 }
7759c80cad95 minor changes
cin
parents: 55
diff changeset
250 }
59
21611344d366 code cleanup
cin
parents: 57
diff changeset
251
21611344d366 code cleanup
cin
parents: 57
diff changeset
252 protected virtual void Dispose(bool disposing) {
21611344d366 code cleanup
cin
parents: 57
diff changeset
253 if (disposing) {
21611344d366 code cleanup
cin
parents: 57
diff changeset
254 m_scanner.Dispose();
21611344d366 code cleanup
cin
parents: 57
diff changeset
255 }
21611344d366 code cleanup
cin
parents: 57
diff changeset
256 }
21611344d366 code cleanup
cin
parents: 57
diff changeset
257
21611344d366 code cleanup
cin
parents: 57
diff changeset
258 /// <summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
259 /// Освобождает парсер и связанный с ним сканнер.
21611344d366 code cleanup
cin
parents: 57
diff changeset
260 /// </summary>
21611344d366 code cleanup
cin
parents: 57
diff changeset
261 public void Dispose() {
21611344d366 code cleanup
cin
parents: 57
diff changeset
262 Dispose(true);
21611344d366 code cleanup
cin
parents: 57
diff changeset
263 GC.SuppressFinalize(this);
21611344d366 code cleanup
cin
parents: 57
diff changeset
264 }
21611344d366 code cleanup
cin
parents: 57
diff changeset
265
21611344d366 code cleanup
cin
parents: 57
diff changeset
266 ~JSONParser() {
21611344d366 code cleanup
cin
parents: 57
diff changeset
267 Dispose(false);
21611344d366 code cleanup
cin
parents: 57
diff changeset
268 }
62
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 59
diff changeset
269
70
0349189d2564 refactoring
cin
parents: 62
diff changeset
270 /// <summary>
0349189d2564 refactoring
cin
parents: 62
diff changeset
271 /// Переходит в конец текущего объекта.
0349189d2564 refactoring
cin
parents: 62
diff changeset
272 /// </summary>
0349189d2564 refactoring
cin
parents: 62
diff changeset
273 public void SeekElementEnd() {
0349189d2564 refactoring
cin
parents: 62
diff changeset
274 var level = Level - 1;
62
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 59
diff changeset
275
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 59
diff changeset
276 Debug.Assert(level >= 0);
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 59
diff changeset
277
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 59
diff changeset
278 while (Level != level)
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 59
diff changeset
279 Read();
62b440d46313 Added Skip method to JSON parser to skip contents of the current node
cin
parents: 59
diff changeset
280 }
55
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
281 }
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
282
c0bf853aa04f Added initial JSON support
cin
parents:
diff changeset
283 }