add Optional support

This commit is contained in:
Looly
2024-08-02 00:15:46 +08:00
parent 82a7417a9d
commit 8f55ac21d6
5 changed files with 70 additions and 30 deletions

View File

@@ -181,7 +181,7 @@ public final class InternalJSONUtil {
* @param string 字符串 * @param string 字符串
* @return 适合在JSON中显示的字符串 * @return 适合在JSON中显示的字符串
*/ */
public static String quote(final String string) { public static String quote(final CharSequence string) {
return quote(string, true); return quote(string, true);
} }
@@ -195,7 +195,7 @@ public final class InternalJSONUtil {
* @return 适合在JSON中显示的字符串 * @return 适合在JSON中显示的字符串
* @since 3.3.1 * @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(); return quote(string, new StringWriter(), isWrap).toString();
} }
@@ -208,7 +208,7 @@ public final class InternalJSONUtil {
* @param writer Writer * @param writer Writer
* @throws IORuntimeException IO异常 * @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); quote(str, writer, true);
} }
@@ -224,7 +224,7 @@ public final class InternalJSONUtil {
* @throws IORuntimeException IO异常 * @throws IORuntimeException IO异常
* @since 3.3.1 * @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 { try {
return _quote(str, writer, isWrap); return _quote(str, writer, isWrap);
} catch (final IOException e) { } catch (final IOException e) {
@@ -332,7 +332,7 @@ public final class InternalJSONUtil {
* @throws IOException IO异常 * @throws IOException IO异常
* @since 3.3.1 * @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 (StrUtil.isEmpty(str)) {
if (isWrap) { if (isWrap) {
writer.write("\"\""); writer.write("\"\"");

View File

@@ -21,6 +21,7 @@ import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.convert.Converter; import org.dromara.hutool.core.convert.Converter;
import org.dromara.hutool.core.convert.RegisterConverter; import org.dromara.hutool.core.convert.RegisterConverter;
import org.dromara.hutool.core.convert.impl.*; 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.map.MapWrapper;
import org.dromara.hutool.core.reflect.ConstructorUtil; import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.reflect.TypeReference; import org.dromara.hutool.core.reflect.TypeReference;
@@ -35,10 +36,7 @@ import org.dromara.hutool.json.serialize.JSONStringer;
import java.io.Serializable; import java.io.Serializable;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalAccessor;
import java.util.Collection; import java.util.*;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
/** /**
* JSON转换器实现Object对象转换为{@link JSON},支持的对象: * JSON转换器实现Object对象转换为{@link JSON},支持的对象:
@@ -136,10 +134,17 @@ public class JSONConverter implements Converter, Serializable {
* @throws JSONException 转换异常 * @throws JSONException 转换异常
*/ */
@SuppressWarnings("resource") @SuppressWarnings("resource")
public Object toJSON(final Object obj) throws JSONException { public Object toJSON(Object obj) throws JSONException {
if(null == obj){ if(null == obj){
return null; return null;
} }
if(obj instanceof Optional){
obj = ((Optional<?>) obj).orElse(null);
} else if(obj instanceof Opt){
obj = ((Opt<?>) obj).get();
}
final JSON json; final JSON json;
if (obj instanceof JSON || obj instanceof Number || obj instanceof Boolean) { if (obj instanceof JSON || obj instanceof Number || obj instanceof Boolean) {
return obj; return obj;
@@ -159,8 +164,8 @@ public class JSONConverter implements Converter, Serializable {
// RFC8259JSON字符串值、number, boolean, or null // RFC8259JSON字符串值、number, boolean, or null
final Object value = new JSONTokener(jsonStr, config).nextValue(false); final Object value = new JSONTokener(jsonStr, config).nextValue(false);
if(ObjUtil.equals(value, jsonStr)){ if(ObjUtil.equals(value, jsonStr)){
// 原值返回意味着非正常数字、Boolean或null // 非可解析的字符串,原样返回
throw new JSONException("Unsupported JSON String: {}", jsonStr); return InternalJSONUtil.quote((CharSequence) value);
} }
return value; return value;
} }

View File

@@ -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);
}
}

View File

@@ -32,11 +32,10 @@ public class IssueI6LBZATest {
@Test @Test
public void parseJSONErrorTest() { public void parseJSONErrorTest() {
Assertions.assertThrows(JSONException.class, ()->{
final String a = "a"; final String a = "a";
final Object parse = JSONUtil.parse(a); final Object parse = JSONUtil.parse(a);
Assertions.assertEquals(String.class, parse.getClass()); Assertions.assertEquals(String.class, parse.getClass());
}); Assertions.assertEquals("\"a\"", parse);
} }
@Test @Test

View File

@@ -31,13 +31,6 @@ public class JSONUtilTest {
@Test @Test
public void parseInvalid() { public void parseInvalid() {
Assertions.assertThrows(JSONException.class, ()->{
JSONUtil.parse("abc");
});
}
@Test
public void parseInvalid2() {
Assertions.assertThrows(JSONException.class, ()->{ Assertions.assertThrows(JSONException.class, ()->{
JSONUtil.parse("'abc"); JSONUtil.parse("'abc");
}); });
@@ -312,13 +305,10 @@ public class JSONUtilTest {
@Test @Test
public void toJsonStrOfStringTest(){ public void toJsonStrOfStringTest(){
Assertions.assertThrows(JSONException.class, ()->{
final String a = "a"; final String a = "a";
// 普通字符串不能解析为JSON字符串必须由双引号或者单引号包裹
final String s = JSONUtil.toJsonStr(a); final String s = JSONUtil.toJsonStr(a);
Assertions.assertEquals(a, s); Assertions.assertEquals("\"a\"", s);
});
} }
@Test @Test