From 606f11ced18de52964bc7a3f34ac52f7ed685581 Mon Sep 17 00:00:00 2001 From: ZhangJian He Date: Sat, 1 Jul 2023 11:32:48 +0800 Subject: [PATCH] [feature] allow user custom max depth --- .../minidev/json/parser/JSONParserBase.java | 6 +++- .../json/parser/JSONParserByteArray.java | 4 +++ .../json/parser/JSONParserInputStream.java | 4 +++ .../minidev/json/parser/JSONParserMemory.java | 4 +++ .../minidev/json/parser/JSONParserReader.java | 4 +++ .../minidev/json/parser/JSONParserStream.java | 4 +++ .../minidev/json/parser/JSONParserString.java | 4 +++ .../minidev/json/parser/ParserOptions.java | 32 +++++++++++++++++++ 8 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 json-smart/src/main/java/net/minidev/json/parser/ParserOptions.java diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java index 87a9329a..b2711f40 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserBase.java @@ -93,6 +93,10 @@ abstract class JSONParserBase { protected final boolean unrestictBigDigit; public JSONParserBase(int permissiveMode) { + this(permissiveMode, new ParserOptions()); + } + + public JSONParserBase(int permissiveMode, ParserOptions parserOptions) { this.acceptNaN = (permissiveMode & JSONParser.ACCEPT_NAN) > 0; this.acceptNonQuote = (permissiveMode & JSONParser.ACCEPT_NON_QUOTE) > 0; this.acceptSimpleQuote = (permissiveMode & JSONParser.ACCEPT_SIMPLE_QUOTE) > 0; @@ -103,7 +107,7 @@ public JSONParserBase(int permissiveMode) { this.useHiPrecisionFloat = (permissiveMode & JSONParser.USE_HI_PRECISION_FLOAT) > 0; this.checkTaillingData = (permissiveMode & (JSONParser.ACCEPT_TAILLING_DATA | JSONParser.ACCEPT_TAILLING_SPACE)) != (JSONParser.ACCEPT_TAILLING_DATA - | JSONParser.ACCEPT_TAILLING_SPACE); + | JSONParser.ACCEPT_TAILLING_SPACE); this.checkTaillingSpace = (permissiveMode & JSONParser.ACCEPT_TAILLING_SPACE) == 0; this.reject127 = (permissiveMode & JSONParser.REJECT_127_CHAR) > 0; this.unrestictBigDigit = (permissiveMode & JSONParser.BIG_DIGIT_UNRESTRICTED) > 0; diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserByteArray.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserByteArray.java index bafcc987..fe9fab15 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserByteArray.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserByteArray.java @@ -34,6 +34,10 @@ public JSONParserByteArray(int permissiveMode) { super(permissiveMode); } + public JSONParserByteArray(int permissiveMode, ParserOptions parserOptions) { + super(permissiveMode, parserOptions); + } + /** * use to return Primitive Type, or String, Or JsonObject or JsonArray * generated by a ContainerFactory diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserInputStream.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserInputStream.java index 7a1fb89b..2d11975b 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserInputStream.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserInputStream.java @@ -33,6 +33,10 @@ public JSONParserInputStream(int permissiveMode) { super(permissiveMode); } + public JSONParserInputStream(int permissiveMode, ParserOptions parserOptions) { + super(permissiveMode, parserOptions); + } + /** * use to return Primitive Type, or String, Or JsonObject or JsonArray * generated by a ContainerFactory diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserMemory.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserMemory.java index bece6aee..49f88307 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserMemory.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserMemory.java @@ -35,6 +35,10 @@ public JSONParserMemory(int permissiveMode) { super(permissiveMode); } + public JSONParserMemory(int permissiveMode, ParserOptions parserOptions) { + super(permissiveMode, parserOptions); + } + protected void readNQString(boolean[] stop) throws IOException { int start = pos; skipNQString(stop); diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserReader.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserReader.java index 9e90f7c9..993dd3bc 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserReader.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserReader.java @@ -36,6 +36,10 @@ public JSONParserReader(int permissiveMode) { super(permissiveMode); } + public JSONParserReader(int permissiveMode, ParserOptions parserOptions) { + super(permissiveMode, parserOptions); + } + /** * use to return Primitive Type, or String, Or JsonObject or JsonArray * generated by a ContainerFactory diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserStream.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserStream.java index 737ea97b..841c63d9 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserStream.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserStream.java @@ -34,6 +34,10 @@ public JSONParserStream(int permissiveMode) { super(permissiveMode); } + public JSONParserStream(int permissiveMode, ParserOptions parserOptions) { + super(permissiveMode, parserOptions); + } + protected void readNQString(boolean[] stop) throws IOException { sb.clear(); skipNQString(stop); diff --git a/json-smart/src/main/java/net/minidev/json/parser/JSONParserString.java b/json-smart/src/main/java/net/minidev/json/parser/JSONParserString.java index d792a224..15f6adc8 100644 --- a/json-smart/src/main/java/net/minidev/json/parser/JSONParserString.java +++ b/json-smart/src/main/java/net/minidev/json/parser/JSONParserString.java @@ -31,6 +31,10 @@ public JSONParserString(int permissiveMode) { super(permissiveMode); } + public JSONParserString(int permissiveMode, ParserOptions parserOptions) { + super(permissiveMode, parserOptions); + } + /** * use to return Primitive Type, or String, Or JsonObject or JsonArray * generated by a ContainerFactory diff --git a/json-smart/src/main/java/net/minidev/json/parser/ParserOptions.java b/json-smart/src/main/java/net/minidev/json/parser/ParserOptions.java new file mode 100644 index 00000000..a540595f --- /dev/null +++ b/json-smart/src/main/java/net/minidev/json/parser/ParserOptions.java @@ -0,0 +1,32 @@ +package net.minidev.json.parser; + +/* + * Copyright 2011-2023 JSON-SMART authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +public class ParserOptions { + + private int maxDepth; + + public ParserOptions() { + } + + public int getMaxDepth() { + return maxDepth; + } + + public void setMaxDepth(int maxDepth) { + this.maxDepth = maxDepth; + } +}