diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java b/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java index 1a56631c0..fe5d358e6 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/InternalJSONUtil.java @@ -181,7 +181,7 @@ public final class InternalJSONUtil { * @param string 字符串 * @return 适合在JSON中显示的字符串 */ - public static String quote(final String string) { + public static String quote(final CharSequence string) { return quote(string, true); } @@ -195,7 +195,7 @@ public final class InternalJSONUtil { * @return 适合在JSON中显示的字符串 * @since 3.3.1 */ - public static String quote(final String string, final boolean isWrap) { + public static String quote(final CharSequence string, final boolean isWrap) { return quote(string, new StringWriter(), isWrap).toString(); } @@ -208,7 +208,7 @@ public final class InternalJSONUtil { * @param writer Writer * @throws IORuntimeException IO异常 */ - public static void quote(final String str, final Writer writer) throws IORuntimeException { + public static void quote(final CharSequence str, final Writer writer) throws IORuntimeException { quote(str, writer, true); } @@ -224,7 +224,7 @@ public final class InternalJSONUtil { * @throws IORuntimeException IO异常 * @since 3.3.1 */ - public static Writer quote(final String str, final Writer writer, final boolean isWrap) throws IORuntimeException { + public static Writer quote(final CharSequence str, final Writer writer, final boolean isWrap) throws IORuntimeException { try { return _quote(str, writer, isWrap); } catch (final IOException e) { @@ -332,7 +332,7 @@ public final class InternalJSONUtil { * @throws IOException IO异常 * @since 3.3.1 */ - private static Writer _quote(final String str, final Writer writer, final boolean isWrap) throws IOException { + private static Writer _quote(final CharSequence str, final Writer writer, final boolean isWrap) throws IOException { if (StrUtil.isEmpty(str)) { if (isWrap) { writer.write("\"\""); 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 db44bde4f..7ce043539 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 @@ -21,6 +21,7 @@ import org.dromara.hutool.core.convert.ConvertException; import org.dromara.hutool.core.convert.Converter; import org.dromara.hutool.core.convert.RegisterConverter; import org.dromara.hutool.core.convert.impl.*; +import org.dromara.hutool.core.lang.Opt; import org.dromara.hutool.core.map.MapWrapper; import org.dromara.hutool.core.reflect.ConstructorUtil; import org.dromara.hutool.core.reflect.TypeReference; @@ -35,10 +36,7 @@ import org.dromara.hutool.json.serialize.JSONStringer; import java.io.Serializable; import java.lang.reflect.Type; import java.time.temporal.TemporalAccessor; -import java.util.Collection; -import java.util.Date; -import java.util.Iterator; -import java.util.Map; +import java.util.*; /** * JSON转换器,实现Object对象转换为{@link JSON},支持的对象: @@ -136,10 +134,17 @@ public class JSONConverter implements Converter, Serializable { * @throws JSONException 转换异常 */ @SuppressWarnings("resource") - public Object toJSON(final Object obj) throws JSONException { + public Object toJSON(Object obj) throws JSONException { if(null == obj){ return null; } + + if(obj instanceof Optional){ + obj = ((Optional) obj).orElse(null); + } else if(obj instanceof Opt){ + obj = ((Opt) obj).get(); + } + final JSON json; if (obj instanceof JSON || obj instanceof Number || obj instanceof Boolean) { return obj; @@ -159,8 +164,8 @@ public class JSONConverter implements Converter, Serializable { // RFC8259,JSON字符串值、number, boolean, or null final Object value = new JSONTokener(jsonStr, config).nextValue(false); if(ObjUtil.equals(value, jsonStr)){ - // 原值返回,意味着非正常数字、Boolean或null - throw new JSONException("Unsupported JSON String: {}", jsonStr); + // 非可解析的字符串,原样返回 + return InternalJSONUtil.quote((CharSequence) value); } return value; } diff --git a/hutool-json/src/test/java/org/dromara/hutool/json/Issue3681Test.java b/hutool-json/src/test/java/org/dromara/hutool/json/Issue3681Test.java new file mode 100644 index 000000000..15e7ac25c --- /dev/null +++ b/hutool-json/src/test/java/org/dromara/hutool/json/Issue3681Test.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024. looly(loolly@aliyun.com) + * Hutool is licensed under Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * https://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, + * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, + * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. + * See the Mulan PSL v2 for more details. + */ + +package org.dromara.hutool.json; + +import org.dromara.hutool.core.lang.Opt; +import org.dromara.hutool.core.map.MapUtil; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +public class Issue3681Test { + @Test + void toJsonStrOfOptionalTest() { + String abc = JSONUtil.toJsonStr(Optional.of("abc")); + Assertions.assertEquals("\"abc\"", abc); + + abc = JSONUtil.toJsonStr(Optional.of("123")); + Assertions.assertEquals("123", abc); + } + + @Test + void toJsonStrOfOptionalTest2() { + final String abc = JSONUtil.toJsonStr(Optional.of(MapUtil.of("a", 1))); + Assertions.assertEquals("{\"a\":1}", abc); + } + + @Test + void toJsonStrOfOptTest() { + String abc = JSONUtil.toJsonStr(Opt.of("abc")); + Assertions.assertEquals("\"abc\"", abc); + + abc = JSONUtil.toJsonStr(Opt.of("123")); + Assertions.assertEquals("123", abc); + } +} diff --git a/hutool-json/src/test/java/org/dromara/hutool/json/IssueI6LBZATest.java b/hutool-json/src/test/java/org/dromara/hutool/json/IssueI6LBZATest.java index 74d87c119..aea68a360 100644 --- a/hutool-json/src/test/java/org/dromara/hutool/json/IssueI6LBZATest.java +++ b/hutool-json/src/test/java/org/dromara/hutool/json/IssueI6LBZATest.java @@ -32,11 +32,10 @@ public class IssueI6LBZATest { @Test public void parseJSONErrorTest() { - Assertions.assertThrows(JSONException.class, ()->{ - final String a = "a"; - final Object parse = JSONUtil.parse(a); - Assertions.assertEquals(String.class, parse.getClass()); - }); + final String a = "a"; + final Object parse = JSONUtil.parse(a); + Assertions.assertEquals(String.class, parse.getClass()); + Assertions.assertEquals("\"a\"", parse); } @Test diff --git a/hutool-json/src/test/java/org/dromara/hutool/json/JSONUtilTest.java b/hutool-json/src/test/java/org/dromara/hutool/json/JSONUtilTest.java index f2acb147c..74b73de53 100644 --- a/hutool-json/src/test/java/org/dromara/hutool/json/JSONUtilTest.java +++ b/hutool-json/src/test/java/org/dromara/hutool/json/JSONUtilTest.java @@ -31,13 +31,6 @@ public class JSONUtilTest { @Test public void parseInvalid() { - Assertions.assertThrows(JSONException.class, ()->{ - JSONUtil.parse("abc"); - }); - } - - @Test - public void parseInvalid2() { Assertions.assertThrows(JSONException.class, ()->{ JSONUtil.parse("'abc"); }); @@ -312,13 +305,10 @@ public class JSONUtilTest { @Test public void toJsonStrOfStringTest(){ - Assertions.assertThrows(JSONException.class, ()->{ - final String a = "a"; + final String a = "a"; - // 普通字符串不能解析为JSON字符串,必须由双引号或者单引号包裹 - final String s = JSONUtil.toJsonStr(a); - Assertions.assertEquals(a, s); - }); + final String s = JSONUtil.toJsonStr(a); + Assertions.assertEquals("\"a\"", s); } @Test