Class JsonParser
Parses a JSON document from a binary stream. This implementation utilizes a BinaryReader and offers functionality for tokenizing and constructing JSON objects, arrays, and values.
Inherited Members
Namespace: TriLibCore.Utils
Assembly: TriLibCore.dll
Syntax
public class JsonParser
Constructors
JsonParser(BinaryReader, bool)
Initializes a new instance of the JsonParser class with the specified BinaryReader.
Declaration
public JsonParser(BinaryReader binaryReader, bool createKeys = false)
Parameters
| Type | Name | Description |
|---|---|---|
| BinaryReader | binaryReader | The BinaryReader to read JSON data from. |
| bool | createKeys | A value indicating whether parsed string keys should be stored within the resulting JsonParser.JsonValue objects for later retrieval. |
Methods
ParseRootValue()
Parses the root JSON value, which must be an object (i.e., enclosed in curly braces).
Declaration
public JsonParser.JsonValue ParseRootValue()
Returns
| Type | Description |
|---|---|
| JsonParser.JsonValue | A JsonParser.JsonValue representing the root object. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the root token is not a '{' character. |
ParseValues(ref JsonValue, bool)
Parses JSON values, either within an array context ("[ ]") or as part of an object's key-value pairs.
This method reads tokens from the underlying stream and constructs JsonParser.JsonValue children for
the given parentValue.
Declaration
public void ParseValues(ref JsonParser.JsonValue parentValue, bool insideArray)
Parameters
| Type | Name | Description |
|---|---|---|
| JsonParser.JsonValue | parentValue | The parent JsonParser.JsonValue to which parsed child values are added. |
| bool | insideArray | true if the values belong to an array (e.g. "[...]"); false if they are part of an object's properties. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown if an unexpected token is encountered. |
ReadToken(out long, out long, out bool, out char)
Reads a JSON token from the underlying BinaryReader and returns its position, length, hash, and additional characteristics (e.g., whether it is a string).
Declaration
public long ReadToken(out long tokenLength, out long tokenHash, out bool isString, out char initialChar)
Parameters
| Type | Name | Description |
|---|---|---|
| long | tokenLength | Outputs the length of the token in bytes. |
| long | tokenHash | Outputs the computed hash value for the token's contents. |
| bool | isString | Outputs a value indicating whether this token is delimited as a string. |
| char | initialChar | Outputs the first character of the token (for single-character tokens) or the delimiter (for strings). |
Returns
| Type | Description |
|---|---|
| long | The position in the stream at which the token begins. |