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