public static void JsonPathIsAccurate(string json, string expectedPath) { JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <KeyValuePair <int, int> >(json)); Assert.Contains(expectedPath, ex.ToString()); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <KeyValuePair <int, int> >(json)); Assert.Contains(expectedPath, ex.ToString()); }
public async Task JsonPathIsAccurate(string json, string expectedPath) { JsonException ex = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <KeyValuePair <int, int> >(json)); Assert.Contains(expectedPath, ex.ToString()); var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true }; ex = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <KeyValuePair <int, int> >(json)); Assert.Contains(expectedPath, ex.ToString()); }
public static void WriteCyclicFail(int depth) { var rootObj = new TestClassWithCycle("root"); CreateObjectHierarchy(1, depth, rootObj); { var options = new JsonSerializerOptions(); options.MaxDepth = depth + 1; // No exception since depth was not greater than MaxDepth. string json = JsonSerializer.Serialize(rootObj, options); Assert.False(string.IsNullOrEmpty(json)); } { var options = new JsonSerializerOptions(); options.MaxDepth = depth; JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Serialize(rootObj, options)); // Exception should contain the path and MaxDepth. // Since the last Parent property is null, the serializer moves onto the Children property. string expectedPath = "$" + string.Concat(Enumerable.Repeat(".Parent", depth - 1)); Assert.Contains(expectedPath, ex.Path); Assert.Contains(depth.ToString(), ex.ToString()); } }
public static void JsonPathIsAccurate_PropertyNamingPolicy(string json, string expectedPath) { var options = new JsonSerializerOptions { PropertyNamingPolicy = new LeadingUnderscorePolicy() }; JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Deserialize <KeyValuePair <int, int> >(json, options)); Assert.Contains(expectedPath, ex.ToString()); }
public async Task JsonPathIsAccurate_PropertyNamingPolicy(string json, string expectedPath) { var options = new JsonSerializerOptions { PropertyNamingPolicy = new LeadingUnderscorePolicy() }; JsonException ex = await Assert.ThrowsAsync <JsonException>(async() => await Serializer.DeserializeWrapper <KeyValuePair <int, int> >(json, options)); Assert.Contains(expectedPath, ex.ToString()); }
static void RunTest(int maxDepth, int effectiveMaxDepth) { var options = new JsonSerializerOptions { MaxDepth = maxDepth }; var newOptions = new JsonSerializerOptions(options); Assert.Equal(maxDepth, options.MaxDepth); Assert.Equal(maxDepth, newOptions.MaxDepth); // Test for default effective max depth in exception message. var myList = new List <object>(); myList.Add(myList); string effectiveMaxDepthAsStr = effectiveMaxDepth.ToString(); JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Serialize(myList, options)); Assert.Contains(effectiveMaxDepthAsStr, ex.ToString()); ex = Assert.Throws <JsonException>(() => JsonSerializer.Serialize(myList, newOptions)); Assert.Contains(effectiveMaxDepthAsStr, ex.ToString()); }
[InlineData(70, 0, 63)] // 0 (or default) max depth is treated as 64 public static void WriteCyclicFail(int objectHierarchyDepth, int maxDepth, int expectedPathDepth) { var rootObj = new TestClassWithCycle("root"); CreateObjectHierarchy(1, objectHierarchyDepth, rootObj); var options = new JsonSerializerOptions(); options.MaxDepth = maxDepth; JsonException ex = Assert.Throws <JsonException>(() => JsonSerializer.Serialize(rootObj, options)); // Exception should contain the path and MaxDepth. // Since the last Parent property is null, the serializer moves onto the Children property. string expectedPath = "$" + string.Concat(Enumerable.Repeat(".Parent", expectedPathDepth)); Assert.Contains(expectedPath, ex.Path); Assert.Contains(maxDepth.ToString(), ex.ToString()); }