This commit is contained in:
Looly
2023-03-10 13:07:05 +08:00
parent 869b12000d
commit 35feac4eae
13 changed files with 123 additions and 336 deletions

View File

@@ -1,81 +0,0 @@
package cn.hutool.json;
import cn.hutool.core.lang.mutable.MutableEntry;
import cn.hutool.core.math.NumberUtil;
import java.io.IOException;
import java.io.Writer;
import java.util.function.Predicate;
/**
* JSON数字表示
*
* @author looly
* @since 6.0.0
*/
public class JSONNumber extends Number implements JSON {
private static final long serialVersionUID = 1L;
private final Number value;
/**
* 配置项
*/
private final JSONConfig config;
/**
* 构造
*
* @param value 值
* @param config JSON配置
*/
public JSONNumber(final Number value, final JSONConfig config) {
this.value = value;
this.config = config;
}
@Override
public JSONConfig getConfig() {
return this.config;
}
@Override
public int size() {
return 1;
}
@Override
public Writer write(final Writer writer, final int indentFactor, final int indent,
final Predicate<MutableEntry<Object, Object>> predicate) throws JSONException {
try {
writer.write(toString());
} catch (final IOException e) {
throw new RuntimeException(e);
}
return writer;
}
@Override
public int intValue() {
return this.value.intValue();
}
@Override
public long longValue() {
return this.value.longValue();
}
@Override
public float floatValue() {
return this.value.floatValue();
}
@Override
public double doubleValue() {
return this.value.doubleValue();
}
@Override
public String toString() {
return NumberUtil.toStr(this.value);
}
}

View File

@@ -1,76 +0,0 @@
package cn.hutool.json;
import cn.hutool.core.lang.mutable.MutableEntry;
import java.io.IOException;
import java.io.Writer;
import java.util.function.Predicate;
/**
* JSON字符串表示一个字符串
*
* @author looly
* @since 6.0.0
*/
public class JSONString implements JSON, CharSequence {
private static final long serialVersionUID = 1L;
private final char[] value;
/**
* 配置项
*/
private final JSONConfig config;
/**
* 构造
*
* @param value char数组
* @param config 配置项
*/
public JSONString(final char[] value, final JSONConfig config) {
this.value = value;
this.config = config;
}
@Override
public JSONConfig getConfig() {
return this.config;
}
@Override
public int size() {
return length();
}
@Override
public Writer write(final Writer writer, final int indentFactor, final int indent,
final Predicate<MutableEntry<Object, Object>> predicate) throws JSONException {
try {
writer.write(this.value);
} catch (final IOException e) {
throw new RuntimeException(e);
}
return writer;
}
@Override
public int length() {
return this.value.length;
}
@Override
public char charAt(final int index) {
return this.value[index];
}
@Override
public CharSequence subSequence(final int start, final int end) {
return ((start == 0) && (end == this.value.length)) ? this
: new String(this.value, start, end);
}
@Override
public String toString() {
return String.valueOf(this.value);
}
}

View File

@@ -2,6 +2,7 @@ package cn.hutool.json;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.reflect.TypeReference;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ObjUtil;
@@ -27,7 +28,6 @@ import java.util.List;
public class JSONUtil {
// -------------------------------------------------------------------- Pause start
/**
* 创建JSONObject
*
@@ -139,8 +139,8 @@ public class JSONUtil {
* @param obj 对象
* @return JSON
*/
public static JSON parse(final Object obj) {
return JSONConverter.INSTANCE.toJSON(obj);
public static Object parse(final Object obj) {
return parse(obj, null);
}
/**
@@ -154,10 +154,12 @@ public class JSONUtil {
*
* @param obj 对象
* @param config JSON配置{@code null}使用默认配置
* @return JSON
* @since 5.3.1
* @return JSONJSONObject or JSONArray
*/
public static JSON parse(final Object obj, final JSONConfig config) {
public static Object parse(final Object obj, final JSONConfig config) {
if(null == config){
return JSONConverter.INSTANCE.toJSON(obj);
}
return JSONConverter.of(config).toJSON(obj);
}
@@ -173,6 +175,7 @@ public class JSONUtil {
// -------------------------------------------------------------------- Parse end
// -------------------------------------------------------------------- Read start
/**
* 读取JSON
*
@@ -182,11 +185,7 @@ public class JSONUtil {
* @throws IORuntimeException IO异常
*/
public static JSON readJSON(final File file, final Charset charset) throws IORuntimeException {
try (final Reader reader = FileUtil.getReader(file, charset)) {
return parse(reader);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return (JSON) FileUtil.read(file, charset, JSONUtil::parse);
}
/**
@@ -198,11 +197,7 @@ public class JSONUtil {
* @throws IORuntimeException IO异常
*/
public static JSONObject readJSONObject(final File file, final Charset charset) throws IORuntimeException {
try (final Reader reader = FileUtil.getReader(file, charset)) {
return parseObj(reader);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return FileUtil.read(file, charset, JSONUtil::parseObj);
}
/**
@@ -214,67 +209,23 @@ public class JSONUtil {
* @throws IORuntimeException IO异常
*/
public static JSONArray readJSONArray(final File file, final Charset charset) throws IORuntimeException {
try (final Reader reader = FileUtil.getReader(file, charset)) {
return parseArray(reader);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
return FileUtil.read(file, charset, JSONUtil::parseArray);
}
// -------------------------------------------------------------------- Read end
// -------------------------------------------------------------------- toString start
/**
* 转JSON字符串
* 转换为格式化后的JSON字符串
*
* @param json JSON
* @param indentFactor 每一级别的缩进
* @param obj Bean对象
* @return JSON字符串
*/
public static String toJsonStr(final JSON json, final int indentFactor) {
if (null == json) {
return null;
public static String toJsonPrettyStr(Object obj) {
obj = parse(obj);
if (obj instanceof JSON) {
return ((JSON) obj).toStringPretty();
}
return json.toJSONString(indentFactor);
}
/**
* 转为JSON字符串
*
* @param json JSON
* @return JSON字符串
*/
public static String toJsonStr(final JSON json) {
if (null == json) {
return null;
}
return json.toJSONString(0);
}
/**
* 转为JSON字符串并写出到write
*
* @param json JSON
* @param writer Writer
* @since 5.3.3
*/
public static void toJsonStr(final JSON json, final Writer writer) {
if (null != json) {
json.write(writer);
}
}
/**
* 转为JSON字符串
*
* @param json JSON
* @return JSON字符串
*/
public static String toJsonPrettyStr(final JSON json) {
if (null == json) {
return null;
}
return json.toStringPretty();
return StrUtil.toStringOrNull(obj);
}
/**
@@ -299,7 +250,7 @@ public class JSONUtil {
public static String toJsonStr(final Object obj, final JSONConfig jsonConfig) {
// 自定义规则,优先级高于全局规则
final JSONValueWriter valueWriter = InternalJSONUtil.getValueWriter(obj);
if(null != valueWriter){
if (null != valueWriter) {
final StringWriter stringWriter = new StringWriter();
final JSONWriter jsonWriter = JSONWriter.of(stringWriter, 0, 0, null);
// 用户对象自定义实现了JSONValueWriter接口理解为需要自定义输出
@@ -307,17 +258,10 @@ public class JSONUtil {
return stringWriter.toString();
}
if (null == obj) {
if(null == obj){
return null;
}
if (obj instanceof CharSequence) {
return StrUtil.str((CharSequence) obj);
}
if (obj instanceof Number) {
return obj.toString();
}
return toJsonStr(parse(obj, jsonConfig));
return parse(obj, jsonConfig).toString();
}
/**
@@ -327,20 +271,20 @@ public class JSONUtil {
* @param writer Writer
* @since 5.3.3
*/
public static void toJsonStr(final Object obj, final Writer writer) {
public static void toJsonStr(Object obj, final Writer writer) {
if (null != obj) {
toJsonStr(parse(obj), writer);
}
}
obj = parse(obj);
if (obj instanceof JSON) {
((JSON) obj).write(writer);
}
/**
* 转换为格式化后的JSON字符串
*
* @param obj Bean对象
* @return JSON字符串
*/
public static String toJsonPrettyStr(final Object obj) {
return toJsonPrettyStr(parse(obj));
// 普通值
try {
writer.write(obj.toString());
} catch (final IOException e) {
throw new IORuntimeException(e);
}
}
}
/**
@@ -393,7 +337,7 @@ public class JSONUtil {
* @since 5.8.0
*/
public static <T> T toBean(final String jsonString, final JSONConfig config, final Class<T> beanClass) {
return toBean(parse(jsonString, config), beanClass);
return toBean(jsonString, config, (Type) beanClass);
}
/**
@@ -405,23 +349,12 @@ public class JSONUtil {
* @param config JSON配置
* @param type Bean类型
* @return 实体类对象
* @throws JSONException 提供的JSON字符串不支持转Bean或字符串错误
*/
public static <T> T toBean(final String jsonString, final JSONConfig config, final Type type) {
public static <T> T toBean(final String jsonString, final JSONConfig config, final Type type) throws JSONException {
return toBean(parse(jsonString, config), type);
}
/**
* 转为实体类对象,转换异常将被抛出
*
* @param <T> Bean类型
* @param json JSONObject
* @param beanClass 实体类对象
* @return 实体类对象
*/
public static <T> T toBean(final JSONObject json, final Class<T> beanClass) {
return null == json ? null : json.toBean(beanClass);
}
/**
* 转为实体类对象
*
@@ -431,24 +364,27 @@ public class JSONUtil {
* @return 实体类对象
* @since 4.6.2
*/
public static <T> T toBean(final JSON json, final TypeReference<T> typeReference) {
public static <T> T toBean(final Object json, final TypeReference<T> typeReference) {
return toBean(json, typeReference.getType());
}
/**
* 转为实体类对象
*
* @param <T> Bean类型
* @param json JSONObject
* @param beanType 实体类对象类型
* @param <T> Bean类型
* @param json JSONObject
* @param type 实体类对象类型
* @return 实体类对象
* @since 4.3.2
*/
public static <T> T toBean(final JSON json, final Type beanType) {
public static <T> T toBean(final Object json, final Type type) {
if (null == json) {
return null;
}
return json.toBean(beanType);
if (json instanceof JSON) {
return ((JSON) json).toBean(type);
}
throw new JSONException("Unsupported json string to bean : {}", json);
}
// -------------------------------------------------------------------- toBean end
@@ -562,13 +498,13 @@ public class JSONUtil {
* @param json JSONObject或JSONArray
* @return 是否为空
*/
public static boolean isEmpty(final JSON json){
if(null == json){
public static boolean isEmpty(final JSON json) {
if (null == json) {
return true;
}
if(json instanceof JSONObject){
if (json instanceof JSONObject) {
return ((JSONObject) json).isEmpty();
} else if(json instanceof JSONArray){
} else if (json instanceof JSONArray) {
return ((JSONArray) json).isEmpty();
}
return false;

View File

@@ -111,32 +111,34 @@ public class JSONConverter implements Converter {
/**
* 实现Object对象转换为{@link JSON},支持的对象:
* <ul>
* <li>String: 转换为相应的对象</li>
* <li>String: 转换为相应的对象</li>
* <li>Array、Iterable、Iterator转换为JSONArray</li>
* <li>Bean对象转为JSONObject</li>
* <li>Number返回原对象</li>
* </ul>
*
* @param obj 被转换的对象
* @return 转换后的对象
* @throws JSONException 转换异常
*/
public JSON toJSON(final Object obj) throws JSONException {
public Object toJSON(final Object obj) throws JSONException {
final JSON json;
if (obj instanceof JSON) {
json = (JSON) obj;
if (obj instanceof JSON || obj instanceof Number) {
return obj;
} else if (obj instanceof CharSequence) {
final String jsonStr = StrUtil.trim((CharSequence) obj);
switch (jsonStr.charAt(0)){
case '"':
case '\'':
return new JSONString(jsonStr.toCharArray(), config);
// JSON字符串值
return jsonStr;
case '[':
return new JSONArray(jsonStr, config);
case '{':
return new JSONObject(jsonStr, config);
default:
if(NumberUtil.isNumber(jsonStr)){
return new JSONNumber(NumberUtil.toBigDecimal(jsonStr), config);
return NumberUtil.toBigDecimal(jsonStr);
}
throw new JSONException("Unsupported String to JSON: {}", jsonStr);
}

View File

@@ -3,32 +3,34 @@ package cn.hutool.json;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
public class IssueI6LBZATest {
@Test
public void parseJSONStringTest() {
final String a = "\"a\"";
final JSON parse = JSONUtil.parse(a);
Assert.assertEquals(JSONString.class, parse.getClass());
final Object parse = JSONUtil.parse(a);
Assert.assertEquals(String.class, parse.getClass());
}
@Test
public void parseJSONStringTest2() {
final String a = "'a'";
final JSON parse = JSONUtil.parse(a);
Assert.assertEquals(JSONString.class, parse.getClass());
final Object parse = JSONUtil.parse(a);
Assert.assertEquals(String.class, parse.getClass());
}
@Test(expected = JSONException.class)
public void parseJSONErrorTest() {
final String a = "a";
final JSON parse = JSONUtil.parse(a);
Assert.assertEquals(JSONString.class, parse.getClass());
final Object parse = JSONUtil.parse(a);
Assert.assertEquals(String.class, parse.getClass());
}
@Test
public void parseJSONNumberTest() {
final String a = "123";
final JSON parse = JSONUtil.parse(a);
Assert.assertEquals(JSONNumber.class, parse.getClass());
final Object parse = JSONUtil.parse(a);
Assert.assertEquals(BigDecimal.class, parse.getClass());
}
}

View File

@@ -42,8 +42,7 @@ public class JSONArrayTest {
@Test
public void addNullTest() {
final List<String> aaa = ListUtil.view("aaa", null);
final String jsonStr = JSONUtil.toJsonStr(JSONUtil.parse(aaa,
JSONConfig.of().setIgnoreNullValue(false)));
final String jsonStr = JSONUtil.toJsonStr(JSONUtil.parse(aaa, JSONConfig.of().setIgnoreNullValue(false)));
Assert.assertEquals("[\"aaa\",null]", jsonStr);
}

View File

@@ -23,7 +23,7 @@ public class JSONPathTest {
@Test
public void getByPathTest2(){
final String str = "{'accountId':111}";
final JSON json = JSONUtil.parse(str);
final JSON json = (JSON) JSONUtil.parse(str);
final Long accountId = JSONUtil.getByPath(json, "$.accountId", 0L);
Assert.assertEquals(111L, accountId.longValue());
}

View File

@@ -2,6 +2,7 @@ package cn.hutool.json;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.math.NumberUtil;
import cn.hutool.json.serialize.JSONStringer;
@@ -235,9 +236,11 @@ public class JSONUtilTest {
Assert.assertEquals("<key1>v1</key1><key2>a</key2><key2>b</key2><key2>c</key2>", xmlStr);
}
@Test
@Test(expected = JSONException.class)
public void toJsonStrOfStringTest(){
final String a = "a";
// 普通字符串不能解析为JSON字符串必须由双引号或者单引号包裹
final String s = JSONUtil.toJsonStr(a);
Assert.assertEquals(a, s);
}