2using System.Globalization;
14 private Utf8JsonReader _json;
15 private readonly Stack<object> _tokenStack =
new();
16 private TokenType? _bufferedTokenType =
null;
18 private readonly HashSet<TokenType> _closers =
new()
27 private string? _taggedTokenValue =
null;
34 private enum TokenTypeInternal
46 _json =
new Utf8JsonReader(bytes);
56 var bytes = Encoding.UTF8.GetBytes(str);
57 var seq =
new ReadOnlySequence<byte>(bytes);
58 _json =
new Utf8JsonReader(seq);
79 private void SkipInternal()
81 var startCount = _tokenStack.Count;
84 if (_tokenStack.Count < startCount)
break;
94 _taggedTokenValue =
null;
96 if (_bufferedTokenType !=
null)
99 _bufferedTokenType =
null;
112 switch (_json.TokenType)
114 case JsonTokenType.PropertyName:
117 case JsonTokenType.None:
119 case JsonTokenType.StartObject:
122 case JsonTokenType.EndObject:
125 case JsonTokenType.StartArray:
129 case JsonTokenType.EndArray:
133 case JsonTokenType.String:
136 case JsonTokenType.True:
139 case JsonTokenType.False:
142 case JsonTokenType.Null:
145 case JsonTokenType.Comment:
146 case JsonTokenType.Number:
163 TokenType.FieldName or TokenType.String =>
GetString(),
164 TokenType.Int =>
GetInt(),
169 TokenType.True or TokenType.False =>
GetBoolean(),
183 throw new InvalidOperationException($
"Fauna token value isn't a {TokenType.String.ToString()} or a {TokenType.FieldName.ToString()}.");
188 return _json.GetString();
204 return _json.GetBoolean();
222 return DateOnly.Parse(_taggedTokenValue!);
240 return double.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
258 return decimal.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
276 return int.Parse(_taggedTokenValue!);
294 return long.Parse(_taggedTokenValue!);
310 return new Module(_taggedTokenValue!);
323 return DateTime.Parse(_taggedTokenValue!);
338 throw new NotImplementedException();
348 throw new NotImplementedException();
358 throw new NotImplementedException();
368 throw new NotImplementedException();
378 throw new NotImplementedException();
388 throw new NotImplementedException();
398 throw new NotImplementedException();
401 private void ValidateTaggedType(
TokenType type)
403 if (
CurrentTokenType != type || _taggedTokenValue ==
null || _taggedTokenValue.GetType() != typeof(
string))
405 throw new InvalidOperationException($
"CurrentTokenType is a {CurrentTokenType.ToString()}, not a {type.ToString()}.");
409 private void HandleStartObject()
413 switch (_json.TokenType)
415 case JsonTokenType.PropertyName:
416 switch (_json.GetString())
424 _tokenStack.Push(
TokenType.StartDocument);
441 _tokenStack.Push(TokenTypeInternal.StartEscapedObject);
457 _bufferedTokenType =
TokenType.FieldName;
463 case JsonTokenType.EndObject:
464 _bufferedTokenType =
TokenType.EndObject;
469 throw new SerializationException($
"Unexpected token following StartObject: {_json.TokenType}");
473 private void HandleEndObject()
475 var startToken = _tokenStack.Pop();
490 case TokenTypeInternal.StartEscapedObject:
498 throw new SerializationException($
"Unexpected token {startToken}. This might be a bug.");
513 private void HandleTaggedString(
TokenType token)
517 _taggedTokenValue = _json.GetString();
521 private bool Advance()
529 throw new SerializationException(
"Failed to advance underlying JSON reader.", e);
533 private void AdvanceTrue()
537 throw new SerializationException(
"Unexpected end of underlying JSON reader.");
Represents error that occur during serialization and deserialization of Fauna data.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
TokenType
Enumerates the types of tokens used in Fauna serialization.
@ Module
The token type is the Fauna module.
Represents a reader that provides fast, non-cached, forward-only access to serialized data.
Utf8FaunaReader(ReadOnlySequence< byte > bytes)
Initializes a new Utf8FaunaReader to read from a ReadOnlySequence of bytes.
decimal GetDoubleAsDecimal()
Retrieves a decimal value from the current token.
DateOnly GetDate()
Retrieves a DateOnly value from the current token.
void Skip()
Skips the value of the current token.
DateTime TryGetDateTime(out DateTime value)
Tries to retrieve a DateTime value from the current token.
Module TryGetModule(out Module value)
Tries to retrieve a Module object from the current token.
long TryGetLong(out long value)
Tries to retrieve a long value from the current token.
double GetDouble()
Retrieves a double value from the current token.
Utf8FaunaReader(string str)
Initializes a new Utf8FaunaReader to read from a string.
string TryGetString(out string value)
Tries to retrieve a string value from the current token.
bool TryGetBoolean(out bool value)
Tries to retrieve a boolean value from the current token.
object? GetValue()
Gets the value of the current token.
TokenType CurrentTokenType
Gets the type of the current token.
int TryGetInt(out int value)
Tries to retrieve an integer value from the current token.
long GetLong()
Retrieves a long value from the current token.
bool GetBoolean()
Retrieves a boolean value from the current JSON token.
string? GetString()
Retrieves a string value from the current token.
bool Read()
Reads the next token from the source.
DateTime GetTime()
Retrieves a DateTime value from the current token.
double TryGetDouble(out double value)
Tries to retrieve a double value from the current token.
int GetInt()
Retrieves an integer value from the current token.
Module GetModule()
Retrieves a Module object from the current token.