예제 #1
0
        public async Task NestedJsonFileCircularDependencyTest(int depthFactor)
        {
            const int ListLength = 2;

            int length = ListLength * depthFactor;

            List <Order>[] orders = new List <Order> [length];
            orders[0] = JsonTestHelper.PopulateLargeObject(1000);
            for (int i = 1; i < length; i++)
            {
                orders[i] = JsonTestHelper.PopulateLargeObject(1);
                orders[i - 1][0].RelatedOrder = orders[i];
            }

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                IgnoreNullValues = true
            };

            // Ensure no exception for default settings (MaxDepth=64) and no cycle.
            JsonSerializer.Serialize(orders[0], options);

            // Create a cycle.
            orders[length - 1][0].RelatedOrder = orders[0];

            Assert.Throws <JsonException> (() => JsonSerializer.Serialize(orders[0], options));

            using (var memoryStream = new MemoryStream())
            {
                await Assert.ThrowsAsync <JsonException>(async() => await Serializer.SerializeWrapper(memoryStream, orders[0], options));
            }
        }
예제 #2
0
        public async Task VeryLargeJsonFileTest(int payloadSize, bool ignoreNull, bool writeIndented)
        {
            List <Order> list = JsonTestHelper.PopulateLargeObject(payloadSize);

            JsonSerializerOptions options = new JsonSerializerOptions
            {
                IgnoreNullValues = ignoreNull,
                WriteIndented    = writeIndented
            };

            string json = JsonSerializer.Serialize(list, options);

            // Sync case.
            {
                List <Order> deserializedList = JsonSerializer.Deserialize <List <Order> >(json, options);
                Assert.Equal(payloadSize, deserializedList.Count);

                string jsonSerialized = JsonSerializer.Serialize(deserializedList, options);
                Assert.Equal(json, jsonSerialized);
            }

            // Async case.
            using (var memoryStream = new MemoryStream())
            {
                await Serializer.SerializeWrapper(memoryStream, list, options);

                string jsonSerialized = Encoding.UTF8.GetString(memoryStream.ToArray());
                Assert.Equal(json, jsonSerialized);

                memoryStream.Position = 0;
                List <Order> deserializedList = await Serializer.DeserializeWrapper <List <Order> >(memoryStream, options);

                Assert.Equal(payloadSize, deserializedList.Count);
            }
        }
예제 #3
0
        public async Task CompareResultsAgainstSerializer()
        {
            List <Order> obj      = JsonTestHelper.PopulateLargeObject(2);
            string       expected = await Serializer.SerializeWrapper(obj);

            JsonArray jArray = await Serializer.DeserializeWrapper <JsonArray>(expected);

            string actual = jArray.ToJsonString();

            Assert.Equal(expected, actual);

            jArray = JsonNode.Parse(expected).AsArray();
            actual = jArray.ToJsonString();
            Assert.Equal(expected, actual);
        }
예제 #4
0
        [InlineData(16, false, false)] // This results a reader\writer depth of 324 which currently works on all test platforms.
        public async Task DeepNestedJsonFileTest(int depthFactor, bool ignoreNull, bool writeIndented)
        {
            const int ListLength = 10;

            int length = ListLength * depthFactor;

            List <Order>[] orders = new List <Order> [length];
            orders[0] = JsonTestHelper.PopulateLargeObject(1);
            for (int i = 1; i < length; i++)
            {
                orders[i] = JsonTestHelper.PopulateLargeObject(1);
                orders[i - 1][0].RelatedOrder = orders[i];
            }

            JsonSerializerOptions options = new JsonSerializerOptions()
            {
                MaxDepth         = (ListLength * depthFactor * 2) + 4, // Order-to-RelatedOrder has a depth of 2.
                IgnoreNullValues = ignoreNull,
                WriteIndented    = writeIndented
            };
            string json = JsonSerializer.Serialize(orders[0], options);

            // Sync case.
            {
                List <Order> deserializedList = JsonSerializer.Deserialize <List <Order> >(json, options);

                string jsonSerialized = JsonSerializer.Serialize(deserializedList, options);
                Assert.Equal(json, jsonSerialized);
            }

            // Async case.
            using (var memoryStream = new MemoryStream())
            {
                await Serializer.SerializeWrapper(memoryStream, orders[0], options);

                string jsonSerialized = Encoding.UTF8.GetString(memoryStream.ToArray());
                Assert.Equal(json, jsonSerialized);

                memoryStream.Position = 0;
                List <Order> deserializedList = await Serializer.DeserializeWrapper <List <Order> >(memoryStream, options);
            }
        }