Пример #1
0
        public static bool TryParseRequestMessageHeapable(ref ReadOnlySequence <byte> buffer)
        {
            if (!TryParseMessage(ref buffer, out var payload))
            {
                return(false);
            }

            var reader = new Utf8Json();

            Utf8Json.Reader json = reader.GetReader(payload);
            CheckRead(ref json);
            EnsureObjectStart(ref json);

            string protocol        = null;
            int?   protocolVersion = null;

            var completed = false;

            while (!completed && CheckRead(ref json))
            {
                switch (json.TokenType)
                {
                case JsonTokenType.PropertyName:
                    ReadOnlySpan <byte> memberName = json.Value;

                    if (memberName.SequenceEqual(ProtocolPropertyNameUtf8))
                    {
                        protocol = ReadAsString(ref json, ProtocolPropertyName);
                    }
                    else if (memberName.SequenceEqual(ProtocolVersionPropertyNameUtf8))
                    {
                        protocolVersion = ReadAsInt32(ref json, ProtocolVersionPropertyName);
                    }
                    else
                    {
                        json.Skip();
                    }
                    break;

                case JsonTokenType.EndObject:
                    completed = true;
                    break;

                default:
                    throw new InvalidDataException($"Unexpected token '{json.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}'.");
            }
            json.Dispose();
            return(true);
        }
Пример #2
0
        public static byte[] HeapableJsonLabSequenceReturnBytesHelper(byte[] data, out int length, JsonReaderOptions options = JsonReaderOptions.Default)
        {
            ReadOnlySequence <byte> sequence = CreateSegments(data);
            var json = new Utf8Json()
            {
                Options = options
            };

            Utf8Json.Reader reader = json.GetReader(sequence);
            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);
        }