From b6944fd18d25a0d3d6140b54f2dd7e294e551d15 Mon Sep 17 00:00:00 2001 From: Looly Date: Tue, 5 Sep 2023 14:32:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DJSONUtil.parse()=E6=BA=A2?= =?UTF-8?q?=E5=87=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/dromara/hutool/json/JSONParser.java | 6 +++--- .../org/dromara/hutool/json/JSONTokener.java | 16 ++++++++++------ .../hutool/json/convert/JSONConverter.java | 2 +- .../org/dromara/hutool/json/Issue3289Test.java | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/JSONParser.java b/hutool-json/src/main/java/org/dromara/hutool/json/JSONParser.java index c382aac79..7eb63b151 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/JSONParser.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/JSONParser.java @@ -84,7 +84,7 @@ public class JSONParser { } default: tokener.back(); - key = tokener.nextValue().toString(); + key = tokener.nextValue(true).toString(); } // The key is followed by ':'. @@ -94,7 +94,7 @@ public class JSONParser { throw tokener.syntaxError("Expected a ':' after a key"); } - jsonObject.set(key, tokener.nextValue(), predicate); + jsonObject.set(key, tokener.nextValue(false), predicate); // Pairs are separated by ','. @@ -136,7 +136,7 @@ public class JSONParser { jsonArray.addRaw(null, predicate); } else { x.back(); - jsonArray.addRaw(x.nextValue(), predicate); + jsonArray.addRaw(x.nextValue(false), predicate); } switch (x.nextClean()) { case CharUtil.COMMA: diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/JSONTokener.java b/hutool-json/src/main/java/org/dromara/hutool/json/JSONTokener.java index 75e49d6d8..44fc33d88 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/JSONTokener.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/JSONTokener.java @@ -345,15 +345,16 @@ public class JSONTokener extends ReaderWrapper { * @return Boolean, Double, Integer, JSONArray, JSONObject, Long, or String * @throws JSONException 语法错误 */ - public Object nextValue() throws JSONException { + public Object nextValue(final boolean getOnlyStringValue) throws JSONException { char c = this.nextClean(); - final String string; - switch (c) { case '"': case '\'': return this.nextString(c); case '{': + if(getOnlyStringValue){ + throw this.syntaxError("String value must not begin with '{'"); + } this.back(); try { return new JSONObject(this, this.config); @@ -361,6 +362,9 @@ public class JSONTokener extends ReaderWrapper { throw new JSONException("JSONObject depth too large to process.", e); } case '[': + if(getOnlyStringValue){ + throw this.syntaxError("String value must not begin with '['"); + } this.back(); try { return new JSONArray(this, this.config); @@ -382,11 +386,11 @@ public class JSONTokener extends ReaderWrapper { } this.back(); - string = sb.toString().trim(); - if (0 == string.length()) { + final String valueString = sb.toString().trim(); + if (valueString.isEmpty()) { throw this.syntaxError("Missing value"); } - return InternalJSONUtil.stringToValue(string); + return getOnlyStringValue ? valueString : InternalJSONUtil.stringToValue(valueString); } /** diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java b/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java index 1264ec5ea..486f24735 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/convert/JSONConverter.java @@ -154,7 +154,7 @@ public class JSONConverter implements Converter { return new JSONObject(jsonStr, config); default: // RFC8259,JSON字符串值、number, boolean, or null - final Object value = new JSONTokener(jsonStr, config).nextValue(); + final Object value = new JSONTokener(jsonStr, config).nextValue(false); if(ObjUtil.equals(value, jsonStr)){ // 原值返回,意味着非正常数字、Boolean或null throw new JSONException("Unsupported JSON String: {}", jsonStr); diff --git a/hutool-json/src/test/java/org/dromara/hutool/json/Issue3289Test.java b/hutool-json/src/test/java/org/dromara/hutool/json/Issue3289Test.java index 82ff48888..2d1a17c3e 100755 --- a/hutool-json/src/test/java/org/dromara/hutool/json/Issue3289Test.java +++ b/hutool-json/src/test/java/org/dromara/hutool/json/Issue3289Test.java @@ -7,7 +7,7 @@ public class Issue3289Test { @Test void parseTest() { Assertions.assertThrows(JSONException.class, () -> { - final String s = "{\"G\":00,[,,[0E5,6E9,6E5,6E9,6E5,6E9,6E5,6E9,6E9,6E5,true,6E5,6E9,6E5,6E9,6956,EE,5E9,6E5,RE,6E9,6E9,6E5,6E9,6E5,6E9,6E5,6E9,6E5,6E962756779,4141697],]}"; + String s = "{\"G\":00,[6E962756779]}"; JSONUtil.parse(s); }); }