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