change Serializer style

This commit is contained in:
Looly
2024-09-12 02:10:19 +08:00
parent 27ff80d71f
commit 5409001e34
32 changed files with 626 additions and 561 deletions

View File

@@ -16,8 +16,10 @@
package org.dromara.hutool.json;
import org.dromara.hutool.json.serialize.JSONObjectSerializer;
import lombok.ToString;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.JSONSerializer;
import org.dromara.hutool.json.serializer.SerializerManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -27,8 +29,10 @@ import java.util.Date;
public class CustomSerializeTest {
@BeforeEach
public void init(){
JSONUtil.putSerializer(CustomBean.class, (JSONObjectSerializer<CustomBean>) (json, bean) -> json.set("customName", bean.name));
public void init() {
SerializerManager.getInstance().register(CustomBean.class,
(JSONSerializer<CustomBean>) (bean, context) ->
((JSONObject)context.getContextJson()).set("customName", bean.name));
}
@Test
@@ -51,9 +55,9 @@ public class CustomSerializeTest {
@Test
public void deserializeTest() {
JSONUtil.putDeserializer(CustomBean.class, json -> {
SerializerManager.getInstance().register(CustomBean.class, (JSONDeserializer<CustomBean>) (json, deserializeType) -> {
final CustomBean customBean = new CustomBean();
customBean.name = ((JSONObject)json).getStr("customName");
customBean.name = ((JSONObject) json).getStr("customName");
return customBean;
});

View File

@@ -16,17 +16,21 @@
package org.dromara.hutool.json;
import org.dromara.hutool.json.serialize.JSONDeserializer;
import org.dromara.hutool.json.serialize.JSONObjectSerializer;
import lombok.Data;
import org.dromara.hutool.json.serializer.JSONContext;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.JSONSerializer;
import org.dromara.hutool.json.serializer.SerializerManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Type;
public class Issue2555Test {
@Test
public void serAndDeserTest(){
JSONUtil.putSerializer(MyType.class, new MySerializer());
JSONUtil.putDeserializer(MyType.class, new MyDeserializer());
SerializerManager.getInstance().register(MyType.class, new MySerializer());
SerializerManager.getInstance().register(MyType.class, new MyDeserializer());
final SimpleObj simpleObj = new SimpleObj();
final MyType child = new MyType();
@@ -50,16 +54,16 @@ public class Issue2555Test {
private MyType myType;
}
public static class MySerializer implements JSONObjectSerializer<MyType> {
public static class MySerializer implements JSONSerializer<MyType> {
@Override
public void serialize(final JSONObject json, final MyType bean) {
json.set("addr", bean.getAddress());
public JSON serialize(final MyType bean, final JSONContext context) {
return ((JSONObject)context.getContextJson()).set("addr", bean.getAddress());
}
}
public static class MyDeserializer implements JSONDeserializer<MyType> {
@Override
public MyType deserialize(final JSON json) {
public MyType deserialize(final JSON json, final Type deserializeType) {
final MyType myType = new MyType();
myType.setAddress(((JSONObject)json).getStr("addr"));
return myType;

View File

@@ -18,7 +18,9 @@ package org.dromara.hutool.json;
import lombok.Data;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.json.serialize.JSONObjectSerializer;
import org.dromara.hutool.json.serializer.JSONContext;
import org.dromara.hutool.json.serializer.JSONSerializer;
import org.dromara.hutool.json.serializer.SerializerManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -31,7 +33,7 @@ public class Issue3086Test {
@Test
public void serializeTest() {
JSONUtil.putSerializer(TestBean.class, new TestBean());
SerializerManager.getInstance().register(TestBean.class, new TestBean());
final List<SimpleGrantedAuthority> strings = ListUtil.of(
new SimpleGrantedAuthority("ROLE_admin"),
@@ -61,14 +63,19 @@ public class Issue3086Test {
}
@Data
static class TestBean implements JSONObjectSerializer<TestBean> {
static class TestBean implements JSONSerializer<TestBean> {
private Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
@Override
public void serialize(final JSONObject json, final TestBean testBean) {
final List<String> strings = testBean.getAuthorities()
public JSON serialize(final TestBean bean, final JSONContext context) {
final List<String> strings = bean.getAuthorities()
.stream().map(SimpleGrantedAuthority::getAuthority).collect(Collectors.toList());
json.set("authorities",strings);
JSONObject contextJson = (JSONObject) context.getContextJson();
if(null == contextJson){
contextJson = new JSONObject(context.config());
}
contextJson.set("authorities",strings);
return contextJson;
}
}
}

View File

@@ -19,10 +19,11 @@ package org.dromara.hutool.json;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.dromara.hutool.core.reflect.TypeReference;
import org.dromara.hutool.json.serialize.JSONDeserializer;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
@@ -38,7 +39,7 @@ public class IssueI7M2GZTest {
private Integer parsed;
@Override
public JSONBeanParserImpl deserialize(final JSON json) {
public JSONBeanParserImpl deserialize(final JSON json, final Type deserializeType) {
setName("new Object");
setParsed(12);
return this;

View File

@@ -16,11 +16,11 @@
package org.dromara.hutool.json;
import org.dromara.hutool.json.serialize.GlobalSerializeMapping;
import org.dromara.hutool.json.serialize.JSONDeserializer;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.SerializerManager;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -31,7 +31,7 @@ public class IssuesI44E4HTest {
@Test
public void deserializerTest(){
GlobalSerializeMapping.putDeserializer(TestDto.class, (JSONDeserializer<TestDto>) json -> {
SerializerManager.getInstance().register(TestDto.class, (JSONDeserializer<TestDto>) (json, deserializeType) -> {
final TestDto testDto = new TestDto();
testDto.setMd(new AcBizModuleMd("name1", ((JSONObject)json).getStr("md")));
return testDto;

View File

@@ -16,6 +16,7 @@
package org.dromara.hutool.json;
import lombok.Data;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.convert.ConvertException;
import org.dromara.hutool.core.io.file.FileUtil;
@@ -25,7 +26,6 @@ import org.dromara.hutool.core.util.CharsetUtil;
import org.dromara.hutool.json.test.bean.Exam;
import org.dromara.hutool.json.test.bean.JsonNode;
import org.dromara.hutool.json.test.bean.KeyBean;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -34,6 +34,9 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* JSONArray单元测试
*
@@ -47,19 +50,19 @@ public class JSONArrayTest {
final JSONObject jsonObject = new JSONObject();
JSONArray jsonArray = new JSONArray(jsonObject, JSONConfig.of());
Assertions.assertEquals(new JSONArray(), jsonArray);
assertEquals(new JSONArray(), jsonArray);
jsonObject.set("key1", "value1");
jsonArray = new JSONArray(jsonObject, JSONConfig.of());
Assertions.assertEquals(1, jsonArray.size());
Assertions.assertEquals("[{\"key1\":\"value1\"}]", jsonArray.toString());
assertEquals(1, jsonArray.size());
assertEquals("[{\"key1\":\"value1\"}]", jsonArray.toString());
}
@Test
public void addNullTest() {
final List<String> aaa = ListUtil.view("aaa", null);
final String jsonStr = JSONUtil.toJsonStr(JSONUtil.parse(aaa, JSONConfig.of().setIgnoreNullValue(false)));
Assertions.assertEquals("[\"aaa\",null]", jsonStr);
assertEquals("[\"aaa\",null]", jsonStr);
}
@Test
@@ -72,14 +75,14 @@ public class JSONArrayTest {
array.add("value2");
array.add("value3");
Assertions.assertEquals(array.get(0), "value1");
assertEquals(array.get(0), "value1");
}
@Test
public void parseTest() {
final String jsonStr = "[\"value1\", \"value2\", \"value3\"]";
final JSONArray array = JSONUtil.parseArray(jsonStr);
Assertions.assertEquals(array.get(0), "value1");
assertEquals(array.get(0), "value1");
}
@Test
@@ -90,7 +93,7 @@ public class JSONArrayTest {
// 不忽略null则null的键值对被保留
jsonArray = JSONUtil.parseArray(jsonStr, JSONConfig.of().setIgnoreNullValue(false));
Assertions.assertTrue(jsonArray.getJSONObject(1).containsKey("result"));
assertTrue(jsonArray.getJSONObject(1).containsKey("result"));
}
@Test
@@ -99,7 +102,7 @@ public class JSONArrayTest {
final JSONObject obj0 = array.getJSONObject(0);
final Exam exam = JSONUtil.toBean(obj0, Exam.class);
Assertions.assertEquals("0", exam.getAnswerArray()[0].getSeq());
assertEquals("0", exam.getAnswerArray()[0].getSeq());
}
@Test
@@ -114,8 +117,8 @@ public class JSONArrayTest {
final ArrayList<KeyBean> list = ListUtil.of(b1, b2);
final JSONArray jsonArray = JSONUtil.parseArray(list);
Assertions.assertEquals("aValue1", jsonArray.getJSONObject(0).getStr("akey"));
Assertions.assertEquals("bValue2", jsonArray.getJSONObject(1).getStr("bkey"));
assertEquals("aValue1", jsonArray.getJSONObject(0).getStr("akey"));
assertEquals("bValue2", jsonArray.getJSONObject(1).getStr("bkey"));
}
@Test
@@ -138,11 +141,11 @@ public class JSONArrayTest {
Assertions.assertFalse(userList.isEmpty());
Assertions.assertSame(User.class, userList.get(0).getClass());
Assertions.assertEquals(Integer.valueOf(111), userList.get(0).getId());
Assertions.assertEquals(Integer.valueOf(112), userList.get(1).getId());
assertEquals(Integer.valueOf(111), userList.get(0).getId());
assertEquals(Integer.valueOf(112), userList.get(1).getId());
Assertions.assertEquals("test1", userList.get(0).getName());
Assertions.assertEquals("test2", userList.get(1).getName());
assertEquals("test1", userList.get(0).getName());
assertEquals("test2", userList.get(1).getName());
}
@Test
@@ -156,11 +159,11 @@ public class JSONArrayTest {
Assertions.assertFalse(list.isEmpty());
Assertions.assertSame(Dict.class, list.get(0).getClass());
Assertions.assertEquals(Integer.valueOf(111), list.get(0).getInt("id"));
Assertions.assertEquals(Integer.valueOf(112), list.get(1).getInt("id"));
assertEquals(Integer.valueOf(111), list.get(0).getInt("id"));
assertEquals(Integer.valueOf(112), list.get(1).getInt("id"));
Assertions.assertEquals("test1", list.get(0).getStr("name"));
Assertions.assertEquals("test2", list.get(1).getStr("name"));
assertEquals("test1", list.get(0).getStr("name"));
assertEquals("test2", list.get(1).getStr("name"));
}
@Test
@@ -184,8 +187,8 @@ public class JSONArrayTest {
final List<KeyBean> list = ja.toList(KeyBean.class);
Assertions.assertNull(list.get(0));
Assertions.assertEquals("avalue", list.get(1).getAkey());
Assertions.assertEquals("bvalue", list.get(1).getBkey());
assertEquals("avalue", list.get(1).getAkey());
assertEquals("bvalue", list.get(1).getBkey());
}
@Test
@@ -209,28 +212,28 @@ public class JSONArrayTest {
final JSONArray jsonArray = JSONUtil.parseArray(mapList);
final List<JsonNode> nodeList = jsonArray.toList(JsonNode.class);
Assertions.assertEquals(Long.valueOf(0L), nodeList.get(0).getId());
Assertions.assertEquals(Long.valueOf(1L), nodeList.get(1).getId());
Assertions.assertEquals(Long.valueOf(0L), nodeList.get(2).getId());
Assertions.assertEquals(Long.valueOf(0L), nodeList.get(3).getId());
assertEquals(Long.valueOf(0L), nodeList.get(0).getId());
assertEquals(Long.valueOf(1L), nodeList.get(1).getId());
assertEquals(Long.valueOf(0L), nodeList.get(2).getId());
assertEquals(Long.valueOf(0L), nodeList.get(3).getId());
Assertions.assertEquals(Integer.valueOf(0), nodeList.get(0).getParentId());
Assertions.assertEquals(Integer.valueOf(1), nodeList.get(1).getParentId());
Assertions.assertEquals(Integer.valueOf(0), nodeList.get(2).getParentId());
Assertions.assertEquals(Integer.valueOf(0), nodeList.get(3).getParentId());
assertEquals(Integer.valueOf(0), nodeList.get(0).getParentId());
assertEquals(Integer.valueOf(1), nodeList.get(1).getParentId());
assertEquals(Integer.valueOf(0), nodeList.get(2).getParentId());
assertEquals(Integer.valueOf(0), nodeList.get(3).getParentId());
Assertions.assertEquals("0", nodeList.get(0).getName());
Assertions.assertEquals("1", nodeList.get(1).getName());
Assertions.assertEquals("+0", nodeList.get(2).getName());
Assertions.assertEquals("-0", nodeList.get(3).getName());
assertEquals("0", nodeList.get(0).getName());
assertEquals("1", nodeList.get(1).getName());
assertEquals("+0", nodeList.get(2).getName());
assertEquals("-0", nodeList.get(3).getName());
}
@Test
public void getByPathTest() {
final String jsonStr = "[{\"id\": \"1\",\"name\": \"a\"},{\"id\": \"2\",\"name\": \"b\"}]";
final JSONArray jsonArray = JSONUtil.parseArray(jsonStr);
Assertions.assertEquals("b", jsonArray.getByPath("[1].name"));
Assertions.assertEquals("b", JSONUtil.getByPath(jsonArray, "[1].name"));
assertEquals("b", jsonArray.getByPath("[1].name"));
assertEquals("b", JSONUtil.getByPath(jsonArray, "[1].name"));
}
@Test
@@ -238,12 +241,12 @@ public class JSONArrayTest {
JSONArray jsonArray = new JSONArray();
jsonArray.set(3, "test");
// 默认忽略null值因此空位无值只有一个值
Assertions.assertEquals(1, jsonArray.size());
assertEquals(1, jsonArray.size());
jsonArray = new JSONArray(JSONConfig.of().setIgnoreNullValue(false));
jsonArray.set(2, "test");
// 第三个位置插入值0~2都是null
Assertions.assertEquals(3, jsonArray.size());
assertEquals(3, jsonArray.size());
}
// https://github.com/dromara/hutool/issues/1858
@@ -251,8 +254,8 @@ public class JSONArrayTest {
public void putTest2() {
final JSONArray jsonArray = new JSONArray();
jsonArray.put(0, 1);
Assertions.assertEquals(1, jsonArray.size());
Assertions.assertEquals(1, jsonArray.get(0));
assertEquals(1, jsonArray.size());
assertEquals(1, jsonArray.get(0));
}
private static Map<String, String> buildMap(final String id, final String parentId, final String name) {
@@ -278,7 +281,7 @@ public class JSONArrayTest {
.set(true);
final String s = json1.toJSONString(0, (pair) -> pair.getValue().equals("value2"));
Assertions.assertEquals("[\"value2\"]", s);
assertEquals("[\"value2\"]", s);
}
@Test
@@ -290,7 +293,7 @@ public class JSONArrayTest {
.set(true);
final String s = json1.toJSONString(0, (pair) -> !pair.getValue().equals("value2"));
Assertions.assertEquals("[\"value1\",\"value3\",true]", s);
assertEquals("[\"value1\",\"value3\",true]", s);
}
@Test
@@ -298,7 +301,7 @@ public class JSONArrayTest {
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false));
array.set(null);
Assertions.assertEquals("[null]", array.toString());
assertEquals("[null]", array.toString());
}
@Test
@@ -306,8 +309,8 @@ public class JSONArrayTest {
final String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> mutable.get().toString().contains("111"));
Assertions.assertEquals(1, array.size());
Assertions.assertTrue(array.getJSONObject(0).containsKey("id"));
assertEquals(1, array.size());
assertTrue(array.getJSONObject(0).containsKey("id"));
}
@Test
@@ -322,8 +325,20 @@ public class JSONArrayTest {
mutable.set(o);
return true;
});
Assertions.assertEquals(2, array.size());
Assertions.assertTrue(array.getJSONObject(0).containsKey("id"));
Assertions.assertEquals("test1_edit", array.getJSONObject(0).get("name"));
assertEquals(2, array.size());
assertTrue(array.getJSONObject(0).containsKey("id"));
assertEquals("test1_edit", array.getJSONObject(0).get("name"));
}
@Test
void jsonIterTest() {
final JSONArray array = JSONUtil.ofArray();
array.add(JSONUtil.ofObj().set("name", "aaa"));
array.add(JSONUtil.ofObj().set("name", "bbb"));
array.add(JSONUtil.ofObj().set("name", "ccc"));
StringBuilder result = new StringBuilder();
array.jsonIter(JSONObject.class).forEach(result::append);
assertEquals("{\"name\":\"aaa\"}{\"name\":\"bbb\"}{\"name\":\"ccc\"}", result.toString());
}
}

View File

@@ -16,11 +16,13 @@
package org.dromara.hutool.json;
import org.dromara.hutool.json.serialize.JSONDeserializer;
import lombok.Data;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Type;
public class JSONDeserializerTest {
@Test
@@ -33,14 +35,14 @@ public class JSONDeserializerTest {
}
@Data
static class TestBean implements JSONDeserializer<Object> {
static class TestBean implements JSONDeserializer<TestBean> {
private String name;
private String address;
@Override
public Object deserialize(final JSON value) {
final JSONObject valueObj = (JSONObject) value;
public TestBean deserialize(final JSON json, final Type deserializeType) {
final JSONObject valueObj = (JSONObject) json;
this.name = valueObj.getStr("customName");
this.address = valueObj.getStr("customAddress");
return this;

View File

@@ -1,61 +0,0 @@
/*
* Copyright (c) 2024 Hutool Team and hutool.cn
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.dromara.hutool.json;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class JSONSupportTest {
/**
* https://github.com/dromara/hutool/issues/1779
* 在JSONSupport的JSONBeanParse中如果使用json.toBean会导致JSONBeanParse.parse方法反复递归调用最终栈溢出<br>
* 因此parse方法默认实现必须避开JSONBeanParse.parse调用。
*/
@Test
public void parseTest() {
final String jsonstr = "{\n" +
" \"location\": \"https://hutool.cn\",\n" +
" \"message\": \"这是一条测试消息\",\n" +
" \"requestId\": \"123456789\",\n" +
" \"traceId\": \"987654321\"\n" +
"}";
final TestBean testBean = JSONUtil.toBean(jsonstr, TestBean.class);
Assertions.assertEquals("https://hutool.cn", testBean.getLocation());
Assertions.assertEquals("这是一条测试消息", testBean.getMessage());
Assertions.assertEquals("123456789", testBean.getRequestId());
Assertions.assertEquals("987654321", testBean.getTraceId());
}
@EqualsAndHashCode(callSuper = true)
@Data
static class TestBean extends JSONSupport{
private String location;
private String message;
private String requestId;
private String traceId;
}
}

View File

@@ -21,7 +21,7 @@ import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.json.serialize.JSONStringer;
import org.dromara.hutool.json.serializer.JSONStringer;
import org.dromara.hutool.json.test.bean.Price;
import org.dromara.hutool.json.test.bean.UserA;
import org.dromara.hutool.json.test.bean.UserC;

View File

@@ -46,7 +46,6 @@ public class JSONEngineTest {
@Test
void writeTimeZoneTest() {
// TODO Hutool无法序列化TimeZone等特殊对象
Arrays.stream(engineNames).forEach(this::assertWriteTimeZone);
}