JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.71k stars 3.24k forks source link

Please allow setting the max depth in JObject and JArray's Parse method #2900

Open tradercentric opened 11 months ago

tradercentric commented 11 months ago

For Avro message deserialization, I am in need for Confluent to support parsing Avro schema that is more than 64 depth levels where Confluent library is using Newtonsoft library to parse into Avro schema object.

Currently, there is an issue logged with with Apache Avro where Confluent is not able parse Avro schema where the json is over 64 depth level.

The references to the issue are logged at

https://issues.apache.org/jira/browse/AVRO-3856 https://github.com/apache/avro/blob/41b3c08ca5da192786c2b08546e691b3126e1856/lang/csharp/src/apache/main/Schema/Schema.cs#L250

I think we would need the community help to add the feature to set the max depth for JObject and JArray Parse method. Here is what I am thinking if below updates are feasible.

In Newtonsoft.Json.Linq.JsonLoadSettings,

public int? MaxDepth { get; set; } // add

In Newtonsoft.Json.Linq.JObject

    public new static JObject Parse(string json, JsonLoadSettings? settings)
    {
        using (JsonReader reader = new JsonTextReader(new StringReader(json)))
        {
            var maxDepth = settings?.MaxDepth;         //add
            if (maxDepth != null)                                   //add
            {
                reader.MaxDepth = maxDepth;               //add
            }

In Newtonsoft.Json.Linq.JArray

    public new static JArray Parse(string json, JsonLoadSettings? settings)
    {
        using (JsonReader reader = new JsonTextReader(new StringReader(json)))
        {
            var maxDepth = settings?.MaxDepth;        //add
            if (maxDepth != null)                                  //add
            {
                reader.MaxDepth = maxDepth;             //add
            }

I attached a schame.txt which contain a json more than 64 depth level for replicate the issue.

schema.txt

P.S. I noticed Microsoft.Text.Json.Nodes support the passing of max depth, so I hope Newtonsoft can support the feature as well.

using System.Text.Json; using System.Text.Json.Nodes;

var schema = File.ReadAllText("Schemas/schema.txt");

JsonDocumentOptions options = new() { MaxDepth = 128 };

JsonNode.Parse(schema, null, options);

tradercentric commented 9 months ago

First time I really tried to contribute to open source community by providing fixes but was getting nowhere with Newtonsoft, or Apache Avro. For the closing, our company architects approved the source team to use string for the value for XML format. To all who cares I wish you with good luck using deeply nested Avro schema.

Reference to: https://issues.apache.org/jira/browse/AVRO-3856