2using System.Globalization;
15 private Utf8JsonReader _json;
16 private readonly Stack<object> _tokenStack =
new();
17 private TokenType? _bufferedTokenType =
null;
19 private readonly HashSet<TokenType> _closers =
new()
28 private string? _taggedTokenValue =
null;
35 private enum TokenTypeInternal
47 _json =
new Utf8JsonReader(bytes);
57 var bytes = Encoding.UTF8.GetBytes(str);
58 var seq =
new ReadOnlySequence<byte>(bytes);
59 _json =
new Utf8JsonReader(seq);
80 private void SkipInternal()
82 var startCount = _tokenStack.Count;
85 if (_tokenStack.Count < startCount)
break;
95 _taggedTokenValue =
null;
97 if (_bufferedTokenType !=
null)
100 _bufferedTokenType =
null;
113 switch (_json.TokenType)
115 case JsonTokenType.PropertyName:
118 case JsonTokenType.None:
120 case JsonTokenType.StartObject:
123 case JsonTokenType.EndObject:
126 case JsonTokenType.StartArray:
130 case JsonTokenType.EndArray:
134 case JsonTokenType.String:
137 case JsonTokenType.True:
140 case JsonTokenType.False:
143 case JsonTokenType.Null:
146 case JsonTokenType.Comment:
147 case JsonTokenType.Number:
164 TokenType.FieldName or TokenType.String =>
GetString(),
165 TokenType.Int =>
GetInt(),
170 TokenType.True or TokenType.False =>
GetBoolean(),
184 throw new InvalidOperationException($
"Fauna token value isn't a {TokenType.String.ToString()} or a {TokenType.FieldName.ToString()}.");
189 return _json.GetString();
205 return _json.GetBoolean();
223 return DateOnly.Parse(_taggedTokenValue!);
241 return float.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
259 return double.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
277 return decimal.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
295 return byte.Parse(_taggedTokenValue!);
313 return sbyte.Parse(_taggedTokenValue!);
331 return int.Parse(_taggedTokenValue!);
349 return uint.Parse(_taggedTokenValue!);
366 return short.Parse(_taggedTokenValue!);
383 return ushort.Parse(_taggedTokenValue!);
401 return long.Parse(_taggedTokenValue!);
417 return new Module(_taggedTokenValue!);
428 return new Stream(_taggedTokenValue!);
441 return DateTime.Parse(_taggedTokenValue!);
456 throw new NotImplementedException();
466 throw new NotImplementedException();
476 throw new NotImplementedException();
486 throw new NotImplementedException();
496 throw new NotImplementedException();
506 throw new NotImplementedException();
516 throw new NotImplementedException();
519 private void ValidateTaggedType(
TokenType type)
521 if (
CurrentTokenType != type || _taggedTokenValue ==
null || _taggedTokenValue.GetType() != typeof(
string))
523 throw new InvalidOperationException($
"CurrentTokenType is a {CurrentTokenType.ToString()}, not a {type.ToString()}.");
527 private void ValidateTaggedTypes(params
TokenType[] types)
529 if (!types.Contains(
CurrentTokenType) || _taggedTokenValue ==
null || _taggedTokenValue.GetType() != typeof(
string))
531 throw new InvalidOperationException($
"CurrentTokenType is a {CurrentTokenType.ToString()}, not in {types}.");
536 private void HandleStartObject()
540 switch (_json.TokenType)
542 case JsonTokenType.PropertyName:
543 switch (_json.GetString())
551 _tokenStack.Push(
TokenType.StartDocument);
571 _tokenStack.Push(TokenTypeInternal.StartEscapedObject);
587 _bufferedTokenType =
TokenType.FieldName;
593 case JsonTokenType.EndObject:
594 _bufferedTokenType =
TokenType.EndObject;
599 throw new SerializationException($
"Unexpected token following StartObject: {_json.TokenType}");
603 private void HandleEndObject()
605 var startToken = _tokenStack.Pop();
620 case TokenTypeInternal.StartEscapedObject:
628 throw new SerializationException($
"Unexpected token {startToken}. This might be a bug.");
643 private void HandleTaggedString(
TokenType token)
647 _taggedTokenValue = _json.GetString();
651 private bool Advance()
659 throw new SerializationException(
"Failed to advance underlying JSON reader.", e);
663 private void AdvanceTrue()
667 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 @...
Represents a Fauna stream token.
TokenType
Enumerates the types of tokens used in Fauna serialization.
@ Module
The token type is the Fauna module.
@ Stream
The token type is the Fauna stream token.
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.
byte GetByte()
Retrieves an byte value from the current token.
Module TryGetModule(out Module value)
Tries to retrieve a Module object from the current token.
Stream GetStream()
Retrieves a Stream token string from the current token.
short GetShort()
Retrieves an short value 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.
uint GetUnsignedInt()
Retrieves an unsigned integer value from the current token.
ushort GetUnsignedShort()
Retrieves an unsigned short 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.
float GetFloat()
Retrieves a float value from the current 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.
sbyte GetUnsignedByte()
Retrieves an unsigned byte 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.