Пример #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 bool TryParseResponseMessageHeapable(ref ReadOnlySequence <byte> buffer)
        {
            if (!TryParseMessage(ref buffer, out var payload))
            {
                return(false);
            }

            var reader = new Utf8Json();
            var json   = reader.GetReader(payload);

            CheckRead(ref json);
            EnsureObjectStart(ref json);

            int?   minorVersion = null;
            string error        = null;

            var completed = false;

            while (!completed && CheckRead(ref json))
            {
                switch (json.TokenType)
                {
                case JsonTokenType.PropertyName:
                    ReadOnlySpan <byte> memberName = json.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 json, ErrorPropertyName);
                    }
                    else if (memberName.SequenceEqual(MinorVersionPropertyNameUtf8))
                    {
                        minorVersion = ReadAsInt32(ref json, MinorVersionPropertyName);
                    }
                    else
                    {
                        json.Skip();
                    }
                    break;

                case JsonTokenType.EndObject:
                    completed = true;
                    break;

                default:
                    throw new InvalidDataException($"Unexpected token '{json.TokenType}' when reading handshake response JSON.");
                }
            }
            ;

            json.Dispose();
            return(true);
        }
Пример #3
0
        public void HeapableReaderSystemTextJsonLabSpanEmptyLoop()
        {
            var reader = new Utf8Json();

            Utf8Json.Reader json = reader.GetReader(_dataUtf8);
            while (json.Read())
            {
                ;
            }
        }
Пример #4
0
        public static object JsonLabReturnObjectHelperHeapable(byte[] data, JsonReaderOptions options = JsonReaderOptions.Default)
        {
            var reader = new Utf8Json()
            {
                Options = options
            };

            Utf8Json.Reader json = reader.GetReader(data);
            return(JsonLabReaderLoop(ref json));
        }
Пример #5
0
        public static byte[] HeapableJsonLabReturnBytesHelper(byte[] data, out int length, JsonReaderOptions options = JsonReaderOptions.Default)
        {
            var json = new Utf8Json()
            {
                Options = options
            };

            Utf8Json.Reader reader = json.GetReader(data);
            return(JsonLabReaderLoop(data.Length, out length, ref reader));
        }
Пример #6
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);
        }
Пример #7
0
        public static void HeapableJsonLabEmptyLoopHelper(byte[] data)
        {
            var reader = new Utf8Json();

            Utf8Json.Reader json = reader.GetReader(data);
            while (json.Read())
            {
                JsonTokenType tokenType = json.TokenType;
                switch (tokenType)
                {
                case JsonTokenType.StartObject:
                case JsonTokenType.EndObject:
                    break;

                case JsonTokenType.StartArray:
                case JsonTokenType.EndArray:
                    break;

                case JsonTokenType.PropertyName:
                    break;

                case JsonTokenType.String:
                    break;

                case JsonTokenType.Number:
                    break;

                case JsonTokenType.True:
                    break;

                case JsonTokenType.False:
                    break;

                case JsonTokenType.Null:
                    break;

                default:
                    throw new ArgumentException();
                }
            }
        }