Load deep nested schema using JSchema

I have a huge JSON schema with a part of it looking like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{
"if":{
"required":[
"product_category"
],
"properties":{
"product_category":{
"contains":{
"required":[
"value"
],
"properties":{
"value":{
"enum":[
"20005000"
]
}
}
}
}
}
},
"then":{
"properties":{
"product_subcategory":{
"items":{
"properties":{
"value":{
"enum":[
"20005085",
"20005088",
"20005065",
"20005068",
"20005401",
"20005115",
"20005118",
"20005010",
"20005505",
"20005105",
"20005108",
"20005135"
]
}
}
}
}
}
},
"else":{
"if":{
"required":[
"product_category"
],
"properties":{
"product_category":{
"contains":{
"required":[
"value"
],
"properties":{
"value":{
"enum":[
"20055000"
]
}
}
}
}
}
},
"then":{
"properties":{
"product_subcategory":{
"items":{
"properties":{
"value":{
"enum":[
"20055005",
"20055010",
"20055015",
"20055020",
"20055040",
"20055045",
"20055050",
"20055075",
"20055085",
"20055090",
"20055095",
"20055100",
"20055110"
]
}
}
}
}
}
},
"else":{
"if":{
"required":[
"product_category"
],
"properties":{
"product_category":{
"contains":{
"required":[
"value"
],
"properties":{
"value":{
"enum":[
"20056000"
]
}
}
}
}
}
},
"then":{
"properties":{
"product_subcategory":{
"items":{
"properties":{
"value":{
"enum":[
"20056010",
"20056005",
"20056050",
"20056020",
"20056025",
"20056100",
"20056070",
"20056090",
"20056040",
"20056035",
"20056030"
]
}
}
}
},
"else":{
"if":"more nested if-then-else here"
}
}
}
}
}
}
</code>
<code>{ "if":{ "required":[ "product_category" ], "properties":{ "product_category":{ "contains":{ "required":[ "value" ], "properties":{ "value":{ "enum":[ "20005000" ] } } } } } }, "then":{ "properties":{ "product_subcategory":{ "items":{ "properties":{ "value":{ "enum":[ "20005085", "20005088", "20005065", "20005068", "20005401", "20005115", "20005118", "20005010", "20005505", "20005105", "20005108", "20005135" ] } } } } } }, "else":{ "if":{ "required":[ "product_category" ], "properties":{ "product_category":{ "contains":{ "required":[ "value" ], "properties":{ "value":{ "enum":[ "20055000" ] } } } } } }, "then":{ "properties":{ "product_subcategory":{ "items":{ "properties":{ "value":{ "enum":[ "20055005", "20055010", "20055015", "20055020", "20055040", "20055045", "20055050", "20055075", "20055085", "20055090", "20055095", "20055100", "20055110" ] } } } } } }, "else":{ "if":{ "required":[ "product_category" ], "properties":{ "product_category":{ "contains":{ "required":[ "value" ], "properties":{ "value":{ "enum":[ "20056000" ] } } } } } }, "then":{ "properties":{ "product_subcategory":{ "items":{ "properties":{ "value":{ "enum":[ "20056010", "20056005", "20056050", "20056020", "20056025", "20056100", "20056070", "20056090", "20056040", "20056035", "20056030" ] } } } }, "else":{ "if":"more nested if-then-else here" } } } } } } </code>
{
   "if":{
      "required":[
         "product_category"
      ],
      "properties":{
         "product_category":{
            "contains":{
               "required":[
                  "value"
               ],
               "properties":{
                  "value":{
                     "enum":[
                        "20005000"
                     ]
                  }
               }
            }
         }
      }
   },
   "then":{
      "properties":{
         "product_subcategory":{
            "items":{
               "properties":{
                  "value":{
                     "enum":[
                        "20005085",
                        "20005088",
                        "20005065",
                        "20005068",
                        "20005401",
                        "20005115",
                        "20005118",
                        "20005010",
                        "20005505",
                        "20005105",
                        "20005108",
                        "20005135"
                     ]
                  }
               }
            }
         }
      }
   },
   "else":{
      "if":{
         "required":[
            "product_category"
         ],
         "properties":{
            "product_category":{
               "contains":{
                  "required":[
                     "value"
                  ],
                  "properties":{
                     "value":{
                        "enum":[
                           "20055000"
                        ]
                     }
                  }
               }
            }
         }
      },
      "then":{
         "properties":{
            "product_subcategory":{
               "items":{
                  "properties":{
                     "value":{
                        "enum":[
                           "20055005",
                           "20055010",
                           "20055015",
                           "20055020",
                           "20055040",
                           "20055045",
                           "20055050",
                           "20055075",
                           "20055085",
                           "20055090",
                           "20055095",
                           "20055100",
                           "20055110"
                        ]
                     }
                  }
               }
            }
         }
      },
      "else":{
         "if":{
            "required":[
               "product_category"
            ],
            "properties":{
               "product_category":{
                  "contains":{
                     "required":[
                        "value"
                     ],
                     "properties":{
                        "value":{
                           "enum":[
                              "20056000"
                           ]
                        }
                     }
                  }
               }
            }
         },
         "then":{
            "properties":{
               "product_subcategory":{
                  "items":{
                     "properties":{
                        "value":{
                           "enum":[
                              "20056010",
                              "20056005",
                              "20056050",
                              "20056020",
                              "20056025",
                              "20056100",
                              "20056070",
                              "20056090",
                              "20056040",
                              "20056035",
                              "20056030"
                           ]
                        }
                     }
                  }
               },
               "else":{
                  "if":"more nested if-then-else here"
               }
            }
         }
      }
   }
}

I want to call JSchema.Load() on it. The problem is that depth of that JSON exceeds default MaxDepth = 64 and Load() method throws Newtonsoft.Json.JsonReaderExceptio. My code looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>using (var stringReader = new StringReader(json))
using (var jsonReader = new JsonTextReader(stringReader))
{
var schema = JSchema.Load(jsonReader);
}
</code>
<code>using (var stringReader = new StringReader(json)) using (var jsonReader = new JsonTextReader(stringReader)) { var schema = JSchema.Load(jsonReader); } </code>
using (var stringReader = new StringReader(json))
using (var jsonReader = new JsonTextReader(stringReader))
{
    var schema = JSchema.Load(jsonReader);
}

I’ve tried to set JsonTextReader.MaxDepth to 256 but then I get StackOverflowException.
I wrote custom JsonTextReader implementation which shouldn’t go deeper after reaching maxDepth:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class DepthLimitingJsonReader : JsonTextReader
{
private readonly int _maxDepth;
private int _currentDepth;
public DepthLimitingJsonReader(TextReader reader, int maxDepth)
: base(reader)
{
_maxDepth = maxDepth;
_currentDepth = 0;
}
public override bool Read()
{
bool result = base.Read();
if (result)
{
if (TokenType == JsonToken.StartObject || TokenType == JsonToken.StartArray)
{
_currentDepth++;
// If depth exceeds maxDepth, skip the content inside this object or array
if (_currentDepth > _maxDepth && Path.EndsWith("else"))
{
Skip();
}
}
else if (TokenType == JsonToken.EndObject || TokenType == JsonToken.EndArray)
{
_currentDepth--;
}
}
return result;
}
}
</code>
<code>class DepthLimitingJsonReader : JsonTextReader { private readonly int _maxDepth; private int _currentDepth; public DepthLimitingJsonReader(TextReader reader, int maxDepth) : base(reader) { _maxDepth = maxDepth; _currentDepth = 0; } public override bool Read() { bool result = base.Read(); if (result) { if (TokenType == JsonToken.StartObject || TokenType == JsonToken.StartArray) { _currentDepth++; // If depth exceeds maxDepth, skip the content inside this object or array if (_currentDepth > _maxDepth && Path.EndsWith("else")) { Skip(); } } else if (TokenType == JsonToken.EndObject || TokenType == JsonToken.EndArray) { _currentDepth--; } } return result; } } </code>
class DepthLimitingJsonReader : JsonTextReader
    {
        private readonly int _maxDepth;
        private int _currentDepth;

        public DepthLimitingJsonReader(TextReader reader, int maxDepth)
            : base(reader)
        {
            _maxDepth = maxDepth;
            _currentDepth = 0;
        }

        public override bool Read()
        {
            bool result = base.Read();

            if (result)
            {
                if (TokenType == JsonToken.StartObject || TokenType == JsonToken.StartArray)
                {
                    _currentDepth++;
                    // If depth exceeds maxDepth, skip the content inside this object or array
                    if (_currentDepth > _maxDepth && Path.EndsWith("else"))
                    {
                        Skip();
                    }
                }
                else if (TokenType == JsonToken.EndObject || TokenType == JsonToken.EndArray)
                {
                    _currentDepth--;
                }
            }

            return result;
        }
    }

But anyway Skip() method process tokens deeply within the structure and still exceptions are thrown. I want ignore everything what is deeper than maxDepth. How can I do that?

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật