예제 #1
0
        public void ReadFromMultiSegments()
        {
            //----------------------------15----------------------------28
            var js   = "{\"I\":3,\"S\":\"sys.HelloService.Hello\",\"A\":[123, \"中国\"]}";
            var data = System.Text.Encoding.UTF8.GetBytes(js);

            var bs1 = new TestBytesSegment(15, null);

            data.AsSpan(0, 15).CopyTo(bs1.Buffer.AsSpan()); //sys.的.之前

            //var temp = MemoryPool<byte>.Shared.Rent(20);
            var temp1 = new byte[100];
            var bs2   = new TestBytesSegment(28, bs1);

            data.AsSpan(15, 28).CopyTo(bs2.Buffer.AsSpan()); //123的3之前

            var temp2 = new byte[100];
            var bs3   = new TestBytesSegment(data.Length - 15 - 28, bs2);

            data.AsSpan(15 + 28).CopyTo(bs3.Buffer.AsSpan());

            //var jr = new Utf8JsonReader(data.AsSpan(0, 15), false, default); //sys.的.之前
            var jr = new Utf8JsonReader(
                new ReadOnlySequence <byte>(bs1, 0, bs3, bs3.Buffer.Length), false, default);

            Assert.True(jr.Read() && jr.TokenType == JsonTokenType.StartObject);

            Assert.True(jr.Read() && jr.TokenType == JsonTokenType.PropertyName &&
                        jr.ValueSpan.SequenceEqual(RequireIdPropertyName.AsSpan()));
            Assert.True(jr.Read() && jr.GetInt32() == 3);

            Assert.True(jr.Read() && jr.TokenType == JsonTokenType.PropertyName &&
                        jr.ValueSpan.SequenceEqual(RequireServicePropertyName.AsSpan()));
            Assert.True(jr.Read() && jr.GetString() == "sys.HelloService.Hello");

            Assert.True(jr.Read() && jr.TokenType == JsonTokenType.PropertyName &&
                        jr.ValueTextEquals("A"));

            Assert.True(jr.Read() && jr.TokenType == JsonTokenType.StartArray);
            Assert.True(jr.Read() && jr.GetInt32() == 123);
            Assert.True(jr.Read() && jr.GetString() == "中国");
            Assert.True(jr.Read() && jr.TokenType == JsonTokenType.EndArray);

            Assert.True(jr.Read() && jr.TokenType == JsonTokenType.EndObject);
            Assert.True(!jr.Read());
            //Assert.Equal(jr.Read(), false); //读不完整返回false
            //var jr2 = new Utf8JsonReader(data.AsSpan(15), false, jr.CurrentState);
            //jr2.Read(); //报错误,第一个符号为.
            //Assert.Equal(service, "sys.HelloService.Hello");
        }
예제 #2
0
        public TestBytesSegment(int size, TestBytesSegment previous)
        {
            //Buffer = MemoryPool<byte>.Shared.Rent(size);
            Buffer   = new byte[size];
            Previous = previous;
            if (Previous != null)
            {
                Previous.Next = this;
            }

            //Memory = Buffer.Memory;
            Memory       = Buffer.AsMemory();
            RunningIndex = previous?.RunningIndex + previous?.Memory.Length ?? 0;
        }