BGC Tools
Static Public Member Functions | Private Member Functions | Private Attributes
LightJson.Serialization.JsonReader Class Reference

Represents a reader that can read JsonValues. More...

Collaboration diagram for LightJson.Serialization.JsonReader:
Collaboration graph
[legend]

Static Public Member Functions

static JsonValue Parse (TextReader reader)
 Creates a JsonValue by using the given TextReader. More...
 
static JsonValue Parse (string source)
 Creates a JsonValue by reader the JSON message in the given string. More...
 
static JsonValue ParseFile (string path)
 Creates a JsonValue by reading the given file. More...
 

Private Member Functions

 JsonReader (TextReader reader)
 
string ReadJsonKey ()
 
JsonValue ReadJsonValue ()
 
JsonValue ReadNull ()
 
JsonValue ReadBoolean ()
 
void ReadDigits (StringBuilder builder)
 
JsonValue ReadNumber ()
 
string ReadString ()
 
int ReadHexDigit ()
 
char ReadUnicodeLiteral ()
 
JsonObject ReadObject ()
 
JsonObject ReadObject (JsonObject jsonObject)
 
JsonArray ReadArray ()
 
JsonArray ReadArray (JsonArray jsonArray)
 
JsonValue Parse ()
 

Private Attributes

readonly TextScanner scanner
 

Detailed Description

Represents a reader that can read JsonValues.

Definition at line 13 of file JsonReader.cs.

Constructor & Destructor Documentation

◆ JsonReader()

LightJson.Serialization.JsonReader.JsonReader ( TextReader  reader)
inlineprivate

Definition at line 17 of file JsonReader.cs.

18  {
19  scanner = new TextScanner(reader);
20  }
readonly TextScanner scanner
Definition: JsonReader.cs:15

Member Function Documentation

◆ Parse() [1/3]

JsonValue LightJson.Serialization.JsonReader.Parse ( )
inlineprivate

◆ Parse() [2/3]

static JsonValue LightJson.Serialization.JsonReader.Parse ( TextReader  reader)
inlinestatic

Creates a JsonValue by using the given TextReader.

Parameters
readerThe TextReader used to read a JSON message.

Definition at line 355 of file JsonReader.cs.

References LightJson.Serialization.JsonReader.Parse().

356  {
357  if (reader == null)
358  {
359  throw new ArgumentNullException(nameof(reader));
360  }
361 
362  return new JsonReader(reader).Parse();
363  }
JsonReader(TextReader reader)
Definition: JsonReader.cs:17
Here is the call graph for this function:

◆ Parse() [3/3]

static JsonValue LightJson.Serialization.JsonReader.Parse ( string  source)
inlinestatic

Creates a JsonValue by reader the JSON message in the given string.

Parameters
sourceThe string containing the JSON message.

Definition at line 369 of file JsonReader.cs.

References LightJson.Serialization.JsonReader.Parse().

370  {
371  if (source == null)
372  {
373  throw new ArgumentNullException(nameof(source));
374  }
375 
376  using (StringReader reader = new StringReader(source))
377  {
378  return new JsonReader(reader).Parse();
379  }
380  }
JsonReader(TextReader reader)
Definition: JsonReader.cs:17
Here is the call graph for this function:

◆ ParseFile()

static JsonValue LightJson.Serialization.JsonReader.ParseFile ( string  path)
inlinestatic

Creates a JsonValue by reading the given file.

Parameters
pathThe file path to be read.

Definition at line 386 of file JsonReader.cs.

References LightJson.Serialization.JsonReader.Parse().

Referenced by BGC.IO.FileReader.ReadJsonFile().

387  {
388  if (path == null)
389  {
390  throw new ArgumentNullException(nameof(path));
391  }
392 
393  using (StreamReader reader = new StreamReader(path))
394  {
395  return new JsonReader(reader).Parse();
396  }
397  }
JsonReader(TextReader reader)
Definition: JsonReader.cs:17
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ReadArray() [1/2]

JsonArray LightJson.Serialization.JsonReader.ReadArray ( )
private

◆ ReadArray() [2/2]

JsonArray LightJson.Serialization.JsonReader.ReadArray ( JsonArray  jsonArray)
inlineprivate

Definition at line 303 of file JsonReader.cs.

References LightJson.JsonArray.Add().

304  {
305  scanner.Assert('[');
306 
308 
309  if (scanner.Peek() == ']')
310  {
311  scanner.Read();
312  }
313  else
314  {
315  while (true)
316  {
318 
319  JsonValue value = ReadJsonValue();
320 
321  jsonArray.Add(value);
322 
324 
325  char next = scanner.Read();
326 
327  if (next == ']')
328  {
329  break;
330  }
331  else if (next == ',')
332  {
333  continue;
334  }
335 
336  throw new JsonParseException(
337  type: ErrorType.InvalidOrUnexpectedCharacter,
338  position: scanner.Position);
339  }
340  }
341 
342  return jsonArray;
343  }
TextPosition Position
The position of the scanner within the text.
Definition: TextScanner.cs:18
readonly TextScanner scanner
Definition: JsonReader.cs:15
void Assert(char next)
Verifies that the given character matches the next character in the stream. If the characters do not ...
Definition: TextScanner.cs:105
void SkipWhitespace()
Advances the scanner to next non-whitespace character.
Definition: TextScanner.cs:92
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57
Here is the call graph for this function:

◆ ReadBoolean()

JsonValue LightJson.Serialization.JsonReader.ReadBoolean ( )
inlineprivate

Definition at line 63 of file JsonReader.cs.

64  {
65  switch (scanner.Peek())
66  {
67  case 't':
68  scanner.Assert("true");
69  return true;
70 
71  case 'f':
72  scanner.Assert("false");
73  return false;
74 
75  default:
76  throw new JsonParseException(
77  type: ErrorType.InvalidOrUnexpectedCharacter,
78  position: scanner.Position);
79  }
80  }
TextPosition Position
The position of the scanner within the text.
Definition: TextScanner.cs:18
readonly TextScanner scanner
Definition: JsonReader.cs:15
void Assert(char next)
Verifies that the given character matches the next character in the stream. If the characters do not ...
Definition: TextScanner.cs:105
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40

◆ ReadDigits()

void LightJson.Serialization.JsonReader.ReadDigits ( StringBuilder  builder)
inlineprivate

Definition at line 82 of file JsonReader.cs.

83  {
84  while (scanner.CanRead && char.IsDigit(scanner.Peek()))
85  {
86  builder.Append(scanner.Read());
87  }
88  }
readonly TextScanner scanner
Definition: JsonReader.cs:15
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57
bool CanRead
Indicates whether there are still characters to be read.
Definition: TextScanner.cs:21

◆ ReadHexDigit()

int LightJson.Serialization.JsonReader.ReadHexDigit ( )
inlineprivate

Definition at line 203 of file JsonReader.cs.

204  {
205  switch (char.ToUpper(scanner.Read()))
206  {
207  case '0': return 0;
208  case '1': return 1;
209  case '2': return 2;
210  case '3': return 3;
211  case '4': return 4;
212  case '5': return 5;
213  case '6': return 6;
214  case '7': return 7;
215  case '8': return 8;
216  case '9': return 9;
217  case 'A': return 10;
218  case 'B': return 11;
219  case 'C': return 12;
220  case 'D': return 13;
221  case 'E': return 14;
222  case 'F': return 15;
223  default:
224  throw new JsonParseException(
225  type: ErrorType.InvalidOrUnexpectedCharacter,
226  position: scanner.Position);
227  }
228  }
TextPosition Position
The position of the scanner within the text.
Definition: TextScanner.cs:18
readonly TextScanner scanner
Definition: JsonReader.cs:15
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57

◆ ReadJsonKey()

string LightJson.Serialization.JsonReader.ReadJsonKey ( )
private

◆ ReadJsonValue()

JsonValue LightJson.Serialization.JsonReader.ReadJsonValue ( )
inlineprivate

Definition at line 24 of file JsonReader.cs.

25  {
27 
28  char next = scanner.Peek();
29 
30  if (char.IsNumber(next))
31  {
32  return ReadNumber();
33  }
34 
35  switch (next)
36  {
37  case '{': return ReadObject();
38 
39  case '[': return ReadArray();
40 
41  case '"': return ReadString();
42 
43  case '-': return ReadNumber();
44 
45  case 't':
46  case 'f': return ReadBoolean();
47 
48  case 'n': return ReadNull();
49 
50  default:
51  throw new JsonParseException(
52  type: ErrorType.InvalidOrUnexpectedCharacter,
53  position: scanner.Position);
54  }
55  }
TextPosition Position
The position of the scanner within the text.
Definition: TextScanner.cs:18
readonly TextScanner scanner
Definition: JsonReader.cs:15
void SkipWhitespace()
Advances the scanner to next non-whitespace character.
Definition: TextScanner.cs:92
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40

◆ ReadNull()

JsonValue LightJson.Serialization.JsonReader.ReadNull ( )
inlineprivate

Definition at line 57 of file JsonReader.cs.

References LightJson.JsonValue.Null.

58  {
59  scanner.Assert("null");
60  return JsonValue.Null;
61  }
readonly TextScanner scanner
Definition: JsonReader.cs:15
void Assert(char next)
Verifies that the given character matches the next character in the stream. If the characters do not ...
Definition: TextScanner.cs:105

◆ ReadNumber()

JsonValue LightJson.Serialization.JsonReader.ReadNumber ( )
inlineprivate

Definition at line 90 of file JsonReader.cs.

91  {
92  StringBuilder builder = new StringBuilder();
93 
94  if (scanner.Peek() == '-')
95  {
96  builder.Append(scanner.Read());
97  }
98 
99  if (scanner.Peek() == '0')
100  {
101  builder.Append(scanner.Read());
102  }
103  else
104  {
105  ReadDigits(builder);
106  }
107 
108  if (scanner.CanRead && scanner.Peek() == '.')
109  {
110  builder.Append(scanner.Read());
111  ReadDigits(builder);
112  }
113 
114  if (scanner.CanRead && char.ToLowerInvariant(scanner.Peek()) == 'e')
115  {
116  builder.Append(scanner.Read());
117 
118  char next = scanner.Peek();
119 
120  switch (next)
121  {
122  case '+':
123  case '-':
124  builder.Append(scanner.Read());
125  break;
126  }
127 
128  ReadDigits(builder);
129  }
130 
131  return double.Parse(
132  s: builder.ToString(),
133  provider: CultureInfo.InvariantCulture);
134  }
void ReadDigits(StringBuilder builder)
Definition: JsonReader.cs:82
readonly TextScanner scanner
Definition: JsonReader.cs:15
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57
bool CanRead
Indicates whether there are still characters to be read.
Definition: TextScanner.cs:21

◆ ReadObject() [1/2]

JsonObject LightJson.Serialization.JsonReader.ReadObject ( )
private

◆ ReadObject() [2/2]

JsonObject LightJson.Serialization.JsonReader.ReadObject ( JsonObject  jsonObject)
inlineprivate

Definition at line 244 of file JsonReader.cs.

References LightJson.JsonObject.Add(), and LightJson.JsonObject.ContainsKey().

245  {
246  scanner.Assert('{');
247 
249 
250  if (scanner.Peek() == '}')
251  {
252  scanner.Read();
253  }
254  else
255  {
256  while (true)
257  {
259 
260  string key = ReadJsonKey();
261 
262  if (jsonObject.ContainsKey(key))
263  {
264  throw new JsonParseException(
265  type: ErrorType.DuplicateObjectKeys,
266  position: scanner.Position);
267  }
268 
270 
271  scanner.Assert(':');
272 
274 
275  JsonValue value = ReadJsonValue();
276 
277  jsonObject.Add(key, value);
278 
280 
281  char next = scanner.Read();
282 
283  if (next == '}')
284  {
285  break;
286  }
287  else if (next == ',')
288  {
289  continue;
290  }
291 
292  throw new JsonParseException(
293  type: ErrorType.InvalidOrUnexpectedCharacter,
294  position: scanner.Position);
295  }
296  }
297 
298  return jsonObject;
299  }
TextPosition Position
The position of the scanner within the text.
Definition: TextScanner.cs:18
readonly TextScanner scanner
Definition: JsonReader.cs:15
void Assert(char next)
Verifies that the given character matches the next character in the stream. If the characters do not ...
Definition: TextScanner.cs:105
void SkipWhitespace()
Advances the scanner to next non-whitespace character.
Definition: TextScanner.cs:92
char Peek()
Reads the next character in the stream without changing the current position.
Definition: TextScanner.cs:40
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57
Here is the call graph for this function:

◆ ReadString()

string LightJson.Serialization.JsonReader.ReadString ( )
inlineprivate

Definition at line 136 of file JsonReader.cs.

137  {
138  StringBuilder builder = new StringBuilder();
139 
140  scanner.Assert('"');
141 
142  while (true)
143  {
144  char c = scanner.Read();
145 
146  if (c == '\\')
147  {
148  c = scanner.Read();
149 
150  switch (char.ToLower(c))
151  {
152  case '"': // "
153  case '\\': // \
154  case '/': // /
155  builder.Append(c);
156  break;
157  case 'b':
158  builder.Append('\b');
159  break;
160  case 'f':
161  builder.Append('\f');
162  break;
163  case 'n':
164  builder.Append('\n');
165  break;
166  case 'r':
167  builder.Append('\r');
168  break;
169  case 't':
170  builder.Append('\t');
171  break;
172  case 'u':
173  builder.Append(ReadUnicodeLiteral());
174  break;
175  default:
176  throw new JsonParseException(
177  type: ErrorType.InvalidOrUnexpectedCharacter,
178  position: scanner.Position);
179  }
180  }
181  else if (c == '"')
182  {
183  break;
184  }
185  else
186  {
187  if (char.IsControl(c))
188  {
189  throw new JsonParseException(
190  type: ErrorType.InvalidOrUnexpectedCharacter,
191  position: scanner.Position);
192  }
193  else
194  {
195  builder.Append(c);
196  }
197  }
198  }
199 
200  return builder.ToString();
201  }
TextPosition Position
The position of the scanner within the text.
Definition: TextScanner.cs:18
readonly TextScanner scanner
Definition: JsonReader.cs:15
void Assert(char next)
Verifies that the given character matches the next character in the stream. If the characters do not ...
Definition: TextScanner.cs:105
char Read()
Reads the next character in the stream, advancing the text position.
Definition: TextScanner.cs:57

◆ ReadUnicodeLiteral()

char LightJson.Serialization.JsonReader.ReadUnicodeLiteral ( )
inlineprivate

Definition at line 230 of file JsonReader.cs.

231  {
232  int value = 0;
233 
234  value += ReadHexDigit() * 4096; // 16^3
235  value += ReadHexDigit() * 256; // 16^2
236  value += ReadHexDigit() * 16; // 16^1
237  value += ReadHexDigit(); // 16^0
238 
239  return (char)value;
240  }

Field Documentation

◆ scanner

readonly TextScanner LightJson.Serialization.JsonReader.scanner
private

Definition at line 15 of file JsonReader.cs.


The documentation for this class was generated from the following file: