From ace5fcd89b40ef2baf816820297b16e0168f2665 Mon Sep 17 00:00:00 2001 From: Looly Date: Sun, 4 Sep 2022 23:42:15 +0800 Subject: [PATCH] fix code --- .../java/cn/hutool/json/InternalJSONUtil.java | 9 ++-- .../src/main/java/cn/hutool/json/JSON.java | 15 ++---- .../main/java/cn/hutool/json/JSONArray.java | 6 --- .../main/java/cn/hutool/json/JSONObject.java | 6 --- .../main/java/cn/hutool/json/JSONSupport.java | 5 +- hutool-json/src/test/java/Issue2555Test.java | 53 +++++++++++++++++++ 6 files changed, 61 insertions(+), 33 deletions(-) create mode 100755 hutool-json/src/test/java/Issue2555Test.java diff --git a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java index 52cf59582..29e71776d 100755 --- a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java +++ b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java @@ -207,9 +207,8 @@ public final class InternalJSONUtil { * @param key 键 * @param value 值 * @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> predicate) { + public static void propertyPut(final JSONObject jsonObject, final Object key, final Object value, final Predicate> predicate) { final String[] path = StrUtil.splitToArray(Convert.toStr(key), CharUtil.DOT); final int last = path.length - 1; JSONObject target = jsonObject; @@ -223,7 +222,6 @@ public final class InternalJSONUtil { target = nextTarget; } target.set(path[last], value, predicate); - return jsonObject; } /** @@ -296,11 +294,10 @@ public final class InternalJSONUtil { * * @param str 字符串 * @param writer Writer - * @return Writer * @throws IORuntimeException IO异常 */ - public static Writer quote(final String str, final Writer writer) throws IORuntimeException { - return quote(str, writer, true); + public static void quote(final String str, final Writer writer) throws IORuntimeException { + quote(str, writer, true); } /** diff --git a/hutool-json/src/main/java/cn/hutool/json/JSON.java b/hutool-json/src/main/java/cn/hutool/json/JSON.java index f4561b9d3..dc282d680 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSON.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSON.java @@ -107,7 +107,9 @@ public interface JSON extends Cloneable, Serializable { * @see BeanPath#get(Object) * @since 4.0.6 */ - T getByPath(String expression, Class resultType); + default T getByPath(final String expression, final Class resultType){ + return JSONConverterOld.jsonConvert(resultType, getByPath(expression), getConfig()); + } /** * 格式化打印JSON,缩进为4个空格 @@ -159,17 +161,6 @@ public interface JSON extends Cloneable, Serializable { */ Writer write(Writer writer, int indentFactor, int indent, final Predicate> predicate) throws JSONException; - /** - * 转为实体类对象,转换异常将被抛出 - * - * @param Bean类型 - * @param clazz 实体类 - * @return 实体类对象 - */ - default T toBean(final Class clazz) { - return toBean((Type) clazz); - } - /** * 转为实体类对象 * diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONArray.java b/hutool-json/src/main/java/cn/hutool/json/JSONArray.java index 8fbd73162..c8ca774b3 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONArray.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONArray.java @@ -8,7 +8,6 @@ import cn.hutool.core.lang.mutable.MutableEntry; import cn.hutool.core.lang.mutable.MutableObj; import cn.hutool.core.text.StrJoiner; import cn.hutool.core.util.ObjUtil; -import cn.hutool.json.convert.JSONConverterOld; import cn.hutool.json.mapper.ArrayMapper; import cn.hutool.json.serialize.JSONWriter; @@ -193,11 +192,6 @@ public class JSONArray implements JSON, JSONGetter, List, Rando return (index < 0 || index >= this.size()) ? defaultValue : this.rawList.get(index); } - @Override - public T getByPath(final String expression, final Class resultType) { - return JSONConverterOld.jsonConvert(resultType, getByPath(expression), getConfig()); - } - /** * Append an object value. This increases the array's length by one.
* 加入元素,数组长度+1,等同于 {@link JSONArray#add(Object)} diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java index a56427eda..645a41d60 100755 --- a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java @@ -5,7 +5,6 @@ import cn.hutool.core.lang.mutable.MutableEntry; import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapWrapper; import cn.hutool.core.util.ObjUtil; -import cn.hutool.json.convert.JSONConverterOld; import cn.hutool.json.mapper.ObjectMapper; import cn.hutool.json.serialize.JSONWriter; @@ -183,11 +182,6 @@ public class JSONObject extends MapWrapper implements JSON, JSON return this.getOrDefault(key, defaultValue); } - @Override - public T getByPath(final String expression, final Class resultType) { - return JSONConverterOld.jsonConvert(resultType, getByPath(expression), getConfig()); - } - /** * PUT 键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除 * diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java b/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java index 8660e849b..2e170d320 100644 --- a/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java +++ b/hutool-json/src/main/java/cn/hutool/json/JSONSupport.java @@ -1,7 +1,6 @@ package cn.hutool.json; import cn.hutool.core.bean.BeanUtil; -import cn.hutool.json.convert.JSONConverterOld; import cn.hutool.json.serialize.JSONDeserializer; import cn.hutool.json.serialize.JSONString; @@ -29,8 +28,8 @@ public class JSONSupport implements JSONString, JSONDeserializer { */ @Override public Object deserialize(final JSON json) { - final JSONSupport support = JSONConverterOld.jsonToBean(getClass(), json, false); - BeanUtil.copyProperties(support, this); + // TODO 经过两次转换,效率差,待优化 + BeanUtil.copyProperties(json.toBean(getClass()), this); return this; } diff --git a/hutool-json/src/test/java/Issue2555Test.java b/hutool-json/src/test/java/Issue2555Test.java new file mode 100755 index 000000000..d62c63bb5 --- /dev/null +++ b/hutool-json/src/test/java/Issue2555Test.java @@ -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 { + @Override + public void serialize(JSONObject json, MyType bean) { + json.set("addr", bean.getAddress()); + } + } + + public static class MyDeserializer implements JSONDeserializer { + @Override + public MyType deserialize(JSON json) { + final MyType myType = new MyType(); + myType.setAddress(((JSONObject)json).getStr("addr")); + return myType; + } + } +}