5
// Aaron Bockover <abockover@novell.com>
7
// Copyright (C) 2008 Novell, Inc.
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34
public class Deserializer
36
private Tokenizer tokenizer = new Tokenizer ();
38
public Deserializer () { }
39
public Deserializer (string input) { SetInput (input); }
40
public Deserializer (Stream stream) { SetInput (stream); }
41
public Deserializer (StreamReader reader) { SetInput (reader); }
43
public Deserializer SetInput (StreamReader reader)
45
tokenizer.SetInput (reader);
49
public Deserializer SetInput (Stream stream)
51
tokenizer.SetInput (stream);
55
public Deserializer SetInput (string input)
57
tokenizer.SetInput (input);
61
public object Deserialize ()
63
Token token = CheckScan (TokenType.Value, true);
71
private object Parse (Token token)
73
if (token.Type == TokenType.ObjectStart) {
74
return ParseObject ();
75
} else if (token.Type == TokenType.ArrayStart) {
82
private JsonObject ParseObject ()
84
JsonObject obj = new JsonObject ();
87
Token key = CheckScan (TokenType.String | TokenType.ObjectFinish);
88
if (key.Type == TokenType.ObjectFinish) {
92
CheckScan (TokenType.Colon);
93
Token value = CheckScan (TokenType.Value);
95
object value_val = value.Value;
96
if (value.Type == TokenType.ArrayStart) {
97
value_val = ParseArray ();
98
} else if (value.Type == TokenType.ObjectStart) {
99
value_val = ParseObject ();
102
obj.Add ((string)key.Value, value_val);
104
Token token = CheckScan (TokenType.Comma | TokenType.ObjectFinish);
105
if (token.Type == TokenType.ObjectFinish) {
113
private JsonArray ParseArray ()
115
JsonArray array = new JsonArray ();
118
Token value = CheckScan (TokenType.Value | TokenType.ArrayFinish);
119
if (value.Type == TokenType.ArrayFinish) {
123
array.Add (Parse (value));
125
Token token = CheckScan (TokenType.Comma | TokenType.ArrayFinish);
126
if (token.Type == TokenType.ArrayFinish) {
134
private Token CheckScan (TokenType expected)
136
return CheckScan (expected, false);
139
private Token CheckScan (TokenType expected, bool eofok)
141
Token token = tokenizer.Scan ();
142
if (token == null && eofok) {
144
} else if (token == null) {
145
UnexpectedEof (expected);
146
} else if ((expected & token.Type) == 0) {
147
UnexpectedToken (expected, token);
152
private void UnexpectedToken (TokenType expected, Token got)
154
throw new ApplicationException (String.Format ("Unexpected token {0} at [{1}:{2}]; expected {3}",
155
got.Type, got.SourceLine, got.SourceColumn, expected));
158
private void UnexpectedEof (TokenType expected)
160
throw new ApplicationException (String.Format ("Unexpected End of File; expected {0}", expected));