2using System.Globalization;
15 private Utf8JsonReader _json;
16 private readonly Stack<object> _tokenStack =
new();
17 private TokenType? _bufferedTokenType =
null;
18 private object? _bufferedTokenValue =
null;
20 private readonly HashSet<TokenType> _closers =
new()
29 private string? _taggedTokenValue =
null;
36 private enum TokenTypeInternal
40 StartPageUnmaterialized,
49 _json =
new Utf8JsonReader(bytes);
59 var bytes = Encoding.UTF8.GetBytes(str);
60 var seq =
new ReadOnlySequence<byte>(bytes);
61 _json =
new Utf8JsonReader(seq);
82 private void SkipInternal()
84 var startCount = _tokenStack.Count;
87 if (_tokenStack.Count < startCount)
break;
97 _taggedTokenValue =
null;
99 if (_bufferedTokenType !=
null)
102 _bufferedTokenType =
null;
111 _bufferedTokenValue =
null;
118 switch (_json.TokenType)
120 case JsonTokenType.PropertyName:
123 case JsonTokenType.None:
125 case JsonTokenType.StartObject:
128 case JsonTokenType.EndObject:
131 case JsonTokenType.StartArray:
135 case JsonTokenType.EndArray:
139 case JsonTokenType.String:
142 case JsonTokenType.True:
145 case JsonTokenType.False:
148 case JsonTokenType.Null:
151 case JsonTokenType.Comment:
152 case JsonTokenType.Number:
169 TokenType.FieldName or TokenType.String =>
GetString(),
170 TokenType.Int =>
GetInt(),
175 TokenType.True or TokenType.False =>
GetBoolean(),
190 throw new InvalidOperationException($
"Fauna token value isn't a {TokenType.String.ToString()} or a {TokenType.FieldName.ToString()}.");
195 if (_bufferedTokenValue !=
null)
return (
string)_bufferedTokenValue;
196 return _json.GetString();
212 return _json.GetBoolean();
230 return DateOnly.Parse(_taggedTokenValue!);
248 return float.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
266 return double.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
284 return decimal.Parse(_taggedTokenValue!, CultureInfo.InvariantCulture);
302 return byte.Parse(_taggedTokenValue!);
320 return Convert.FromBase64String(_taggedTokenValue!);
338 return sbyte.Parse(_taggedTokenValue!);
356 return int.Parse(_taggedTokenValue!);
374 return uint.Parse(_taggedTokenValue!);
391 return short.Parse(_taggedTokenValue!);
408 return ushort.Parse(_taggedTokenValue!);
426 return long.Parse(_taggedTokenValue!);
442 return new Module(_taggedTokenValue!);
451 ValidateTaggedType(
TokenType.EventSource);
466 return DateTime.Parse(_taggedTokenValue!);
481 throw new NotImplementedException();
491 throw new NotImplementedException();
501 throw new NotImplementedException();
511 throw new NotImplementedException();
521 throw new NotImplementedException();
531 throw new NotImplementedException();
541 throw new NotImplementedException();
544 private void ValidateTaggedType(
TokenType type)
546 if (
CurrentTokenType != type || _taggedTokenValue ==
null || _taggedTokenValue.GetType() != typeof(
string))
548 throw new InvalidOperationException($
"CurrentTokenType is a {CurrentTokenType.ToString()}, not a {type.ToString()}.");
552 private void ValidateTaggedTypes(params
TokenType[] types)
554 if (!types.Contains(
CurrentTokenType) || _taggedTokenValue ==
null || _taggedTokenValue.GetType() != typeof(
string))
556 throw new InvalidOperationException($
"CurrentTokenType is a {CurrentTokenType.ToString()}, not in {types}.");
561 private void HandleStartObject()
565 switch (_json.TokenType)
567 case JsonTokenType.PropertyName:
568 switch (_json.GetString())
576 _tokenStack.Push(
TokenType.StartDocument);
591 HandleTaggedString(
TokenType.EventSource);
596 _tokenStack.Push(TokenTypeInternal.StartEscapedObject);
606 if (_json.TokenType == JsonTokenType.String)
608 _bufferedTokenValue = _json.GetString();
610 _tokenStack.Push(TokenTypeInternal.StartPageUnmaterialized);
624 _bufferedTokenType =
TokenType.FieldName;
630 case JsonTokenType.EndObject:
631 _bufferedTokenType =
TokenType.EndObject;
640 private void HandleEndObject()
642 var startToken = _tokenStack.Pop();
653 case TokenTypeInternal.StartPageUnmaterialized:
660 case TokenTypeInternal.StartEscapedObject:
683 private void HandleTaggedString(
TokenType token)
687 _taggedTokenValue = _json.GetString();
691 private bool Advance()
703 private void AdvanceTrue()
Represents error that occur during serialization and deserialization of Fauna data.
Represents a Fauna EventSource for initializing Streams and Feeds.
Represents a module, a singleton object grouping related functionalities. Modules are serialized as @...
TokenType
Enumerates the types of tokens used in Fauna serialization.
@ EventSource
The token type is the Fauna event source 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.
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.
byte[] GetBytes()
Retrieves a byte array 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.
EventSource GetEventSource()
Retrieves an EventSource token string from 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.