public static bool TryParseRequestMessage(ref ReadOnlySequence <byte> buffer) { if (!TryParseMessage(ref buffer, out var payload)) { return(false); } var reader = new Utf8JsonReader(payload); CheckRead(ref reader); EnsureObjectStart(ref reader); string protocol = null; int? protocolVersion = null; var completed = false; while (!completed && CheckRead(ref reader)) { switch (reader.TokenType) { case JsonTokenType.PropertyName: ReadOnlySpan <byte> memberName = reader.Value; if (memberName.SequenceEqual(ProtocolPropertyNameUtf8)) { protocol = ReadAsString(ref reader, ProtocolPropertyName); } else if (memberName.SequenceEqual(ProtocolVersionPropertyNameUtf8)) { protocolVersion = ReadAsInt32(ref reader, ProtocolVersionPropertyName); } else { reader.Skip(); } break; case JsonTokenType.EndObject: completed = true; break; default: throw new InvalidDataException($"Unexpected token '{reader.TokenType}' when reading handshake request JSON."); } } if (protocol == null) { throw new InvalidDataException($"Missing required property '{ProtocolPropertyName}'."); } if (protocolVersion == null) { throw new InvalidDataException($"Missing required property '{ProtocolVersionPropertyName}'."); } reader.Dispose(); return(true); }
public static bool TryParseResponseMessage(ref ReadOnlySequence <byte> buffer) { if (!TryParseMessage(ref buffer, out var payload)) { return(false); } var reader = new Utf8JsonReader(payload); CheckRead(ref reader); EnsureObjectStart(ref reader); int? minorVersion = null; string error = null; var completed = false; while (!completed && CheckRead(ref reader)) { switch (reader.TokenType) { case JsonTokenType.PropertyName: ReadOnlySpan <byte> memberName = reader.Value; if (memberName.SequenceEqual(TypePropertyNameUtf8)) { // a handshake response does not have a type // check the incoming message was not any other type of message throw new InvalidDataException("Handshake response should not have a 'type' value."); } else if (memberName.SequenceEqual(ErrorPropertyNameUtf8)) { error = ReadAsString(ref reader, ErrorPropertyName); } else if (memberName.SequenceEqual(MinorVersionPropertyNameUtf8)) { minorVersion = ReadAsInt32(ref reader, MinorVersionPropertyName); } else { reader.Skip(); } break; case JsonTokenType.EndObject: completed = true; break; default: throw new InvalidDataException($"Unexpected token '{reader.TokenType}' when reading handshake response JSON."); } } ; reader.Dispose(); return(true); }
public void ReaderSystemTextJsonLabMultiSpanSequenceEmptyLoop() { var json = new Utf8JsonReader(_sequence); while (json.Read()) { ; } json.Dispose(); }
public static byte[] JsonLabSequenceReturnBytesHelper(byte[] data, out int length, JsonReaderOptions options = JsonReaderOptions.Default) { ReadOnlySequence <byte> sequence = CreateSegments(data); var reader = new Utf8JsonReader(sequence) { Options = options }; byte[] result = JsonLabReaderLoop(data.Length, out length, ref reader); reader.Dispose(); // TODO: Should we reset the value and valuetype once we are done? //Assert.True(reader.Value.IsEmpty); //Assert.Equal(JsonValueType.Unknown, reader.ValueType); return(result); }
private static (JsonReaderState, string) ProcessData(ReadOnlySequence <byte> ros, bool isFinalBlock, JsonReaderState state = default) { var builder = new StringBuilder(); var json = new Utf8JsonReader(ros, isFinalBlock, state); while (json.Read()) { switch (json.TokenType) { case JsonTokenType.PropertyName: builder.Append(json.GetValueAsString()); break; } } json.Dispose(); return(json.CurrentState, builder.ToString()); }