Пример #1
0
        public async Task WriteAsyncEnumerableOfAsyncEnumerables <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize)
        {
            if (StreamingSerializer?.IsAsyncSerializer != true)
            {
                return;
            }

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                DefaultBufferSize = bufferSize
            };

            const int OuterEnumerableCount = 5;
            string    expectedJson         = JsonSerializer.Serialize(Enumerable.Repeat(source, OuterEnumerableCount));

            var innerAsyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval);
            var outerAsyncEnumerable =
                new MockedAsyncEnumerable <IAsyncEnumerable <TElement> >(
                    Enumerable.Repeat(innerAsyncEnumerable, OuterEnumerableCount), delayInterval);

            using var stream = new Utf8MemoryStream();
            await StreamingSerializer.SerializeWrapper(stream, outerAsyncEnumerable, options);

            JsonTestHelper.AssertJsonEqual(expectedJson, stream.AsString());
            Assert.Equal(1, outerAsyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(1, outerAsyncEnumerable.TotalDisposedEnumerators);
            Assert.Equal(OuterEnumerableCount, innerAsyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(OuterEnumerableCount, innerAsyncEnumerable.TotalDisposedEnumerators);
        }
Пример #2
0
        public async Task WriteNestedAsyncEnumerable_Nullable <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize)
        {
            if (StreamingSerializer?.IsAsyncSerializer != true)
            {
                return;
            }

            // Primarily tests the ability of NullableConverter to flow async serialization state

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                DefaultBufferSize = bufferSize,
                IncludeFields     = true,
            };

            string expectedJson = JsonSerializer.Serialize <(IEnumerable <TElement>, bool)?>((source, false), options);

            using var stream = new Utf8MemoryStream();
            var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval);
            await StreamingSerializer.SerializeWrapper <(IAsyncEnumerable <TElement>, bool)?>(stream, (asyncEnumerable, false), options);

            JsonTestHelper.AssertJsonEqual(expectedJson, stream.AsString());
            Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
        }
Пример #3
0
        public override async Task <string> SerializeWrapper <T>(T value, JsonTypeInfo <T> jsonTypeInfo)
        {
            using var utf8Stream = new Utf8MemoryStream();
            await SerializeWrapper(utf8Stream, value, jsonTypeInfo);

            return(utf8Stream.AsString());
        }
Пример #4
0
        public override async Task <string> SerializeWrapper(object value, Type inputType, JsonSerializerContext context)
        {
            using var utf8Stream = new Utf8MemoryStream();
            await SerializeWrapper(utf8Stream, value, inputType, context);

            return(utf8Stream.AsString());
        }
Пример #5
0
        public override async Task <string> SerializeWrapper <T>(T value, JsonSerializerOptions options = null)
        {
            using var utf8Stream = new Utf8MemoryStream();
            await SerializeWrapper(utf8Stream, value, options);

            return(utf8Stream.AsString());
        }
Пример #6
0
        public async Task WriteRootLevelAsyncEnumerable <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize)
        {
            if (StreamingSerializer?.IsAsyncSerializer != true)
            {
                return;
            }

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                DefaultBufferSize = bufferSize
            };

            string expectedJson = JsonSerializer.Serialize(source);

            using var stream = new Utf8MemoryStream();
            var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval);
            await StreamingSerializer.SerializeWrapper(stream, asyncEnumerable, options);

            JsonTestHelper.AssertJsonEqual(expectedJson, stream.AsString());
            Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
        }
Пример #7
0
        public async Task WriteSequentialNestedAsyncEnumerables <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize)
        {
            if (StreamingSerializer?.IsAsyncSerializer != true)
            {
                return;
            }

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                DefaultBufferSize = bufferSize
            };

            string expectedJson = JsonSerializer.Serialize(new { Data1 = source, Data2 = source });

            using var stream = new Utf8MemoryStream();
            var asyncEnumerable = new MockedAsyncEnumerable <TElement>(source, delayInterval);
            await StreamingSerializer.SerializeWrapper(stream, new AsyncEnumerableDtoWithTwoProperties <TElement> {
                Data1 = asyncEnumerable, Data2 = asyncEnumerable
            }, options);

            JsonTestHelper.AssertJsonEqual(expectedJson, stream.AsString());
            Assert.Equal(2, asyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(2, asyncEnumerable.TotalDisposedEnumerators);
        }