diff --git a/Src/Newtonsoft.Json.Tests/Issues/Issue2900.cs b/Src/Newtonsoft.Json.Tests/Issues/Issue2900.cs new file mode 100644 index 000000000..40dfaa896 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Issues/Issue2900.cs @@ -0,0 +1,330 @@ +using System; +using System.Linq; +using Newtonsoft.Json.Linq; +#if DNXCORE50 +using Xunit; +using Test = Xunit.FactAttribute; +using Assert = Newtonsoft.Json.Tests.XUnitAssert; +using TestCase = Xunit.InlineDataAttribute; +#else +using NUnit.Framework; +#endif + +namespace Newtonsoft.Json.Tests.Issues +{ + public class Issue2900 + { + [Test] + //https://github.com/JamesNK/Newtonsoft.Json/issues/2900 + public void Override64DepthLevelOnJObjectParse() + { + + var json = @" +{ + ""level1"": { + ""level2"": { + ""level3"": { + ""level4"": { + ""level5"": { + ""level6"": { + ""level7"": { + ""level8"": { + ""level9"": { + ""level10"": { + ""level11"": { + ""level12"": { + ""level13"": { + ""level14"": { + ""level15"": { + ""level16"": { + ""level17"": { + ""level18"": { + ""level19"": { + ""level20"": { + ""level21"": { + ""level22"": { + ""level23"": { + ""level24"": { + ""level25"": { + ""level26"": { + ""level27"": { + ""level28"": { + ""level29"": { + ""level30"": { + ""level31"": { + ""level32"": { + ""level33"": { + ""level34"": { + ""level35"": { + ""level36"": { + ""level37"": { + ""level38"": { + ""level39"": { + ""level40"": { + ""level41"": { + ""level42"": { + ""level43"": { + ""level44"": { + ""level45"": { + ""level46"": { + ""level47"": { + ""level48"": { + ""level49"": { + ""level50"": { + ""level51"": { + ""level52"": { + ""level53"": { + ""level54"": { + ""level55"": { + ""level56"": { + ""level57"": { + ""level58"": { + ""level59"": { + ""level60"": { + ""level61"": { + ""level62"": { + ""level63"": { + ""level64"": { + ""level65"": { + ""data"": ""value"" + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } +} + "; + + var settings = new JsonLoadSettings + { + MaxDepth = 128 + }; + + + ExceptionAssert.Throws(() => + { + JObject.Parse(json); + }); + + JObject.Parse(json, settings); + + } + + [Test] + //https://github.com/JamesNK/Newtonsoft.Json/issues/2900 + public void Override64DepthLevelOnJArrayParse() + { + + var json = @" +[{ + ""level1"": { + ""level2"": { + ""level3"": { + ""level4"": { + ""level5"": { + ""level6"": { + ""level7"": { + ""level8"": { + ""level9"": { + ""level10"": { + ""level11"": { + ""level12"": { + ""level13"": { + ""level14"": { + ""level15"": { + ""level16"": { + ""level17"": { + ""level18"": { + ""level19"": { + ""level20"": { + ""level21"": { + ""level22"": { + ""level23"": { + ""level24"": { + ""level25"": { + ""level26"": { + ""level27"": { + ""level28"": { + ""level29"": { + ""level30"": { + ""level31"": { + ""level32"": { + ""level33"": { + ""level34"": { + ""level35"": { + ""level36"": { + ""level37"": { + ""level38"": { + ""level39"": { + ""level40"": { + ""level41"": { + ""level42"": { + ""level43"": { + ""level44"": { + ""level45"": { + ""level46"": { + ""level47"": { + ""level48"": { + ""level49"": { + ""level50"": { + ""level51"": { + ""level52"": { + ""level53"": { + ""level54"": { + ""level55"": { + ""level56"": { + ""level57"": { + ""level58"": { + ""level59"": { + ""level60"": { + ""level61"": { + ""level62"": { + ""level63"": { + ""level64"": { + ""level65"": { + ""data"": ""value"" + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } +}] + "; + + var settings = new JsonLoadSettings + { + MaxDepth = 128 + }; + + + ExceptionAssert.Throws(() => + { + JArray.Parse(json); + }); + + + JArray.Parse(json, settings); + + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.sln.DotSettings b/Src/Newtonsoft.Json.sln.DotSettings index 72cfc3640..6c5e82286 100644 --- a/Src/Newtonsoft.Json.sln.DotSettings +++ b/Src/Newtonsoft.Json.sln.DotSettings @@ -15,6 +15,7 @@ True True True + True True True True diff --git a/Src/Newtonsoft.Json/Linq/JArray.cs b/Src/Newtonsoft.Json/Linq/JArray.cs index c589e211b..0d25cf81b 100644 --- a/Src/Newtonsoft.Json/Linq/JArray.cs +++ b/Src/Newtonsoft.Json/Linq/JArray.cs @@ -172,6 +172,12 @@ internal override JToken CloneToken(JsonCloneSettings? settings = null) { using (JsonReader reader = new JsonTextReader(new StringReader(json))) { + var maxDepth = settings?.MaxDepth; + if (maxDepth != null) + { + reader.MaxDepth = maxDepth; + } + JArray a = Load(reader, settings); while (reader.Read()) diff --git a/Src/Newtonsoft.Json/Linq/JObject.cs b/Src/Newtonsoft.Json/Linq/JObject.cs index fab460da4..190e49a57 100644 --- a/Src/Newtonsoft.Json/Linq/JObject.cs +++ b/Src/Newtonsoft.Json/Linq/JObject.cs @@ -469,6 +469,12 @@ public JToken? this[string propertyName] { using (JsonReader reader = new JsonTextReader(new StringReader(json))) { + var maxDepth = settings?.MaxDepth; + if (maxDepth != null) + { + reader.MaxDepth = maxDepth; + } + JObject o = Load(reader, settings); while (reader.Read()) diff --git a/Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs b/Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs index 2c224ac2d..1d5987bfc 100644 --- a/Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs +++ b/Src/Newtonsoft.Json/Linq/JsonLoadSettings.cs @@ -36,6 +36,11 @@ public class JsonLoadSettings private LineInfoHandling _lineInfoHandling; private DuplicatePropertyNameHandling _duplicatePropertyNameHandling; + /// + /// Setting MaxDepth to override if necessary. + /// + public int? MaxDepth { get; set; } + /// /// Initializes a new instance of the class. ///