mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
4a2b5b548f
commit
ace5fcd89b
@ -207,9 +207,8 @@ public final class InternalJSONUtil {
|
|||||||
* @param key 键
|
* @param key 键
|
||||||
* @param value 值
|
* @param value 值
|
||||||
* @param predicate 属性过滤器,{@link Predicate#test(Object)}为{@code true}保留
|
* @param predicate 属性过滤器,{@link Predicate#test(Object)}为{@code true}保留
|
||||||
* @return JSONObject
|
|
||||||
*/
|
*/
|
||||||
public static JSONObject propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) {
|
public static void propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) {
|
||||||
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
|
final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT);
|
||||||
final int last = path.length - 1;
|
final int last = path.length - 1;
|
||||||
JSONObject target = jsonObject;
|
JSONObject target = jsonObject;
|
||||||
@ -223,7 +222,6 @@ public final class InternalJSONUtil {
|
|||||||
target = nextTarget;
|
target = nextTarget;
|
||||||
}
|
}
|
||||||
target.set(path[last], value, predicate);
|
target.set(path[last], value, predicate);
|
||||||
return jsonObject;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -296,11 +294,10 @@ public final class InternalJSONUtil {
|
|||||||
*
|
*
|
||||||
* @param str 字符串
|
* @param str 字符串
|
||||||
* @param writer Writer
|
* @param writer Writer
|
||||||
* @return Writer
|
|
||||||
* @throws IORuntimeException IO异常
|
* @throws IORuntimeException IO异常
|
||||||
*/
|
*/
|
||||||
public static Writer quote(final String str, final Writer writer) throws IORuntimeException {
|
public static void quote(final String str, final Writer writer) throws IORuntimeException {
|
||||||
return quote(str, writer, true);
|
quote(str, writer, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -107,7 +107,9 @@ public interface JSON extends Cloneable, Serializable {
|
|||||||
* @see BeanPath#get(Object)
|
* @see BeanPath#get(Object)
|
||||||
* @since 4.0.6
|
* @since 4.0.6
|
||||||
*/
|
*/
|
||||||
<T> T getByPath(String expression, Class<T> resultType);
|
default <T> T getByPath(final String expression, final Class<T> resultType){
|
||||||
|
return JSONConverterOld.jsonConvert(resultType, getByPath(expression), getConfig());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 格式化打印JSON,缩进为4个空格
|
* 格式化打印JSON,缩进为4个空格
|
||||||
@ -159,17 +161,6 @@ public interface JSON extends Cloneable, Serializable {
|
|||||||
*/
|
*/
|
||||||
Writer write(Writer writer, int indentFactor, int indent, final Predicate<MutableEntry<Object, Object>> predicate) throws JSONException;
|
Writer write(Writer writer, int indentFactor, int indent, final Predicate<MutableEntry<Object, Object>> predicate) throws JSONException;
|
||||||
|
|
||||||
/**
|
|
||||||
* 转为实体类对象,转换异常将被抛出
|
|
||||||
*
|
|
||||||
* @param <T> Bean类型
|
|
||||||
* @param clazz 实体类
|
|
||||||
* @return 实体类对象
|
|
||||||
*/
|
|
||||||
default <T> T toBean(final Class<T> clazz) {
|
|
||||||
return toBean((Type) clazz);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转为实体类对象
|
* 转为实体类对象
|
||||||
*
|
*
|
||||||
|
@ -8,7 +8,6 @@ import cn.hutool.core.lang.mutable.MutableEntry;
|
|||||||
import cn.hutool.core.lang.mutable.MutableObj;
|
import cn.hutool.core.lang.mutable.MutableObj;
|
||||||
import cn.hutool.core.text.StrJoiner;
|
import cn.hutool.core.text.StrJoiner;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
import cn.hutool.json.convert.JSONConverterOld;
|
|
||||||
import cn.hutool.json.mapper.ArrayMapper;
|
import cn.hutool.json.mapper.ArrayMapper;
|
||||||
import cn.hutool.json.serialize.JSONWriter;
|
import cn.hutool.json.serialize.JSONWriter;
|
||||||
|
|
||||||
@ -193,11 +192,6 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
|||||||
return (index < 0 || index >= this.size()) ? defaultValue : this.rawList.get(index);
|
return (index < 0 || index >= this.size()) ? defaultValue : this.rawList.get(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T getByPath(final String expression, final Class<T> resultType) {
|
|
||||||
return JSONConverterOld.jsonConvert(resultType, getByPath(expression), getConfig());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Append an object value. This increases the array's length by one. <br>
|
* Append an object value. This increases the array's length by one. <br>
|
||||||
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)}
|
||||||
|
@ -5,7 +5,6 @@ import cn.hutool.core.lang.mutable.MutableEntry;
|
|||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
import cn.hutool.core.map.MapWrapper;
|
import cn.hutool.core.map.MapWrapper;
|
||||||
import cn.hutool.core.util.ObjUtil;
|
import cn.hutool.core.util.ObjUtil;
|
||||||
import cn.hutool.json.convert.JSONConverterOld;
|
|
||||||
import cn.hutool.json.mapper.ObjectMapper;
|
import cn.hutool.json.mapper.ObjectMapper;
|
||||||
import cn.hutool.json.serialize.JSONWriter;
|
import cn.hutool.json.serialize.JSONWriter;
|
||||||
|
|
||||||
@ -183,11 +182,6 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
|
|||||||
return this.getOrDefault(key, defaultValue);
|
return this.getOrDefault(key, defaultValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T getByPath(final String expression, final Class<T> resultType) {
|
|
||||||
return JSONConverterOld.jsonConvert(resultType, getByPath(expression), getConfig());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PUT 键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
* PUT 键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||||
*
|
*
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package cn.hutool.json;
|
package cn.hutool.json;
|
||||||
|
|
||||||
import cn.hutool.core.bean.BeanUtil;
|
import cn.hutool.core.bean.BeanUtil;
|
||||||
import cn.hutool.json.convert.JSONConverterOld;
|
|
||||||
import cn.hutool.json.serialize.JSONDeserializer;
|
import cn.hutool.json.serialize.JSONDeserializer;
|
||||||
import cn.hutool.json.serialize.JSONString;
|
import cn.hutool.json.serialize.JSONString;
|
||||||
|
|
||||||
@ -29,8 +28,8 @@ public class JSONSupport implements JSONString, JSONDeserializer<Object> {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Object deserialize(final JSON json) {
|
public Object deserialize(final JSON json) {
|
||||||
final JSONSupport support = JSONConverterOld.jsonToBean(getClass(), json, false);
|
// TODO 经过两次转换,效率差,待优化
|
||||||
BeanUtil.copyProperties(support, this);
|
BeanUtil.copyProperties(json.toBean(getClass()), this);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
53
hutool-json/src/test/java/Issue2555Test.java
Executable file
53
hutool-json/src/test/java/Issue2555Test.java
Executable file
@ -0,0 +1,53 @@
|
|||||||
|
import cn.hutool.json.JSON;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import cn.hutool.json.serialize.JSONDeserializer;
|
||||||
|
import cn.hutool.json.serialize.JSONObjectSerializer;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Issue2555Test {
|
||||||
|
@Test
|
||||||
|
public void serAndDeserTest(){
|
||||||
|
JSONUtil.putSerializer(MyType.class, new MySerializer());
|
||||||
|
JSONUtil.putDeserializer(MyType.class, new MyDeserializer());
|
||||||
|
|
||||||
|
final SimpleObj simpleObj = new SimpleObj();
|
||||||
|
final MyType child = new MyType();
|
||||||
|
child.setAddress("addrValue1");
|
||||||
|
simpleObj.setMyType(child);
|
||||||
|
|
||||||
|
final String json = JSONUtil.toJsonStr(simpleObj);
|
||||||
|
Assert.assertEquals("{\"myType\":{\"addr\":\"addrValue1\"}}", json);
|
||||||
|
|
||||||
|
//MyDeserializer不会被调用
|
||||||
|
final SimpleObj simpleObj2 = JSONUtil.toBean(json, SimpleObj.class);
|
||||||
|
Assert.assertEquals("addrValue1", simpleObj2.getMyType().getAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class MyType {
|
||||||
|
private String address;
|
||||||
|
}
|
||||||
|
@Data
|
||||||
|
public static class SimpleObj {
|
||||||
|
private MyType myType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MySerializer implements JSONObjectSerializer<MyType> {
|
||||||
|
@Override
|
||||||
|
public void serialize(JSONObject json, MyType bean) {
|
||||||
|
json.set("addr", bean.getAddress());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class MyDeserializer implements JSONDeserializer<MyType> {
|
||||||
|
@Override
|
||||||
|
public MyType deserialize(JSON json) {
|
||||||
|
final MyType myType = new MyType();
|
||||||
|
myType.setAddress(((JSONObject)json).getStr("addr"));
|
||||||
|
return myType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user