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 字符串
* @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("\"\"");

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.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 {
// RFC8259JSON字符串值、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;
}