コード例 #1
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);
        }
コード例 #2
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);
        }
コード例 #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 WriteAsyncEnumerable_ElementSerializationThrows_ShouldDisposeEnumerator()
        {
            using var stream = new Utf8MemoryStream();
            var asyncEnumerable = new MockedAsyncEnumerable <IEnumerable <int> >(Enumerable.Repeat(ThrowingEnumerable(), 2));

            await Assert.ThrowsAsync <DivideByZeroException>(async() => await JsonSerializerWrapperForStream.SerializeWrapper(stream, new { Data = asyncEnumerable }));

            Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
コード例 #7
0
        public async Task WriteAsyncEnumerable_LongRunningEnumeration_Cancellation()
        {
            var longRunningEnumerable = new MockedAsyncEnumerable <int>(
                source: Enumerable.Range(1, 100),
                delayInterval: 1,
                delay: TimeSpan.FromMinutes(1));

            using var utf8Stream = new Utf8MemoryStream();
            using var cts        = new CancellationTokenSource(delay: TimeSpan.FromSeconds(5));
            await Assert.ThrowsAsync <TaskCanceledException>(async() =>
                                                             await JsonSerializer.SerializeAsync(utf8Stream, longRunningEnumerable, cancellationToken: cts.Token));

            Assert.Equal(1, longRunningEnumerable.TotalCreatedEnumerators);
            Assert.Equal(1, longRunningEnumerable.TotalDisposedEnumerators);
        }
コード例 #8
0
        public async Task WriteAsyncEnumerable_ElementSerializationThrows_ShouldDisposeEnumerator()
        {
            if (StreamingSerializer?.IsAsyncSerializer != true)
            {
                return;
            }

            using var stream = new Utf8MemoryStream();
            var asyncEnumerable = new MockedAsyncEnumerable <IEnumerable <int> >(Enumerable.Repeat(ThrowingEnumerable(), 2));

            await Assert.ThrowsAsync <DivideByZeroException>(async() => await StreamingSerializer.SerializeWrapper(stream, new AsyncEnumerableDto <IEnumerable <int> > {
                Data = asyncEnumerable
            }));

            Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
コード例 #9
0
        public async Task WriteNestedAsyncEnumerable <TElement>(IEnumerable <TElement> source, int delayInterval, int bufferSize)
        {
            JsonSerializerOptions options = new JsonSerializerOptions
            {
                DefaultBufferSize = bufferSize
            };

            string expectedJson = await JsonSerializerWrapperForString.SerializeWrapper(new { Data = source });

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

            JsonTestHelper.AssertJsonEqual(expectedJson, stream.ToString());
            Assert.Equal(1, asyncEnumerable.TotalCreatedEnumerators);
            Assert.Equal(1, asyncEnumerable.TotalDisposedEnumerators);
        }
コード例 #10
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);
        }
コード例 #11
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);
        }
コード例 #12
0
 public override async Task <object> DeserializeWrapper(string json, Type returnType, JsonSerializerContext context)
 {
     using var utf8Stream = new Utf8MemoryStream(json);
     return(await DeserializeWrapper(utf8Stream, returnType, context));
 }
コード例 #13
0
 public override async Task <T> DeserializeWrapper <T>(string json, JsonTypeInfo <T> jsonTypeInfo)
 {
     using var utf8Stream = new Utf8MemoryStream(json);
     return(await DeserializeWrapper(utf8Stream, jsonTypeInfo));
 }
コード例 #14
0
 public override async Task <object> DeserializeWrapper(string json, Type returnType, JsonSerializerOptions options = null)
 {
     using var utf8Stream = new Utf8MemoryStream(json);
     return(await DeserializeWrapper(utf8Stream, returnType, options));
 }
コード例 #15
0
 public override async Task <T> DeserializeWrapper <T>(string json, JsonSerializerOptions options = null)
 {
     using var utf8Stream = new Utf8MemoryStream(json);
     return(await DeserializeWrapper <T>(utf8Stream, options));
 }