mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -19,7 +19,7 @@ package org.dromara.hutool.json;
|
||||
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.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -30,7 +30,7 @@ public class CustomSerializeTest {
|
||||
|
||||
@BeforeEach
|
||||
public void init() {
|
||||
SerializerManager.getInstance().register(CustomBean.class,
|
||||
TypeAdapterManager.getInstance().register(CustomBean.class,
|
||||
(JSONSerializer<CustomBean>) (bean, context) ->
|
||||
((JSONObject)context.getContextJson()).set("customName", bean.name));
|
||||
}
|
||||
@@ -55,7 +55,7 @@ public class CustomSerializeTest {
|
||||
|
||||
@Test
|
||||
public void deserializeTest() {
|
||||
SerializerManager.getInstance().register(CustomBean.class, (JSONDeserializer<CustomBean>) (json, deserializeType) -> {
|
||||
TypeAdapterManager.getInstance().register(CustomBean.class, (JSONDeserializer<CustomBean>) (json, deserializeType) -> {
|
||||
final CustomBean customBean = new CustomBean();
|
||||
customBean.name = ((JSONObject) json).getStr("customName");
|
||||
return customBean;
|
||||
|
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import lombok.Data;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -23,12 +24,25 @@ import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* https://github.com/dromara/hutool/issues/1399<br>
|
||||
* 异常SQLException实现了Iterable导致被识别为列表,可能造成死循环,此处按照字符串处理
|
||||
* Throwable的默认序列化策略
|
||||
*/
|
||||
public class Issue1399Test {
|
||||
@Test
|
||||
void sqlExceptionTest() {
|
||||
final JSONObject set = JSONUtil.ofObj().set("error", new SQLException("test"));
|
||||
Assertions.assertEquals("{\"error\":\"java.sql.SQLException: test\"}", set.toString());
|
||||
|
||||
final String jsonStr = set.toString();
|
||||
Assertions.assertEquals("{\"error\":\"java.sql.SQLException: test\"}", jsonStr);
|
||||
|
||||
final ErrorBean bean = set.toBean(ErrorBean.class);
|
||||
Assertions.assertNotNull(bean);
|
||||
Assertions.assertNotNull(bean.getError());
|
||||
Assertions.assertEquals(SQLException.class, bean.getError().getClass());
|
||||
Assertions.assertEquals("test", bean.getError().getMessage());
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class ErrorBean {
|
||||
private SQLException error;
|
||||
}
|
||||
}
|
||||
|
@@ -20,7 +20,7 @@ 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.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -29,8 +29,8 @@ import java.lang.reflect.Type;
|
||||
public class Issue2555Test {
|
||||
@Test
|
||||
public void serAndDeserTest(){
|
||||
SerializerManager.getInstance().register(MyType.class, new MySerializer());
|
||||
SerializerManager.getInstance().register(MyType.class, new MyDeserializer());
|
||||
TypeAdapterManager.getInstance().register(MyType.class, new MySerializer());
|
||||
TypeAdapterManager.getInstance().register(MyType.class, new MyDeserializer());
|
||||
|
||||
final SimpleObj simpleObj = new SimpleObj();
|
||||
final MyType child = new MyType();
|
||||
|
@@ -20,7 +20,7 @@ import lombok.Data;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.json.serializer.JSONContext;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
import org.dromara.hutool.json.serializer.SerializerManager;
|
||||
import org.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -33,7 +33,7 @@ public class Issue3086Test {
|
||||
|
||||
@Test
|
||||
public void serializeTest() {
|
||||
SerializerManager.getInstance().register(TestBean.class, new TestBean());
|
||||
TypeAdapterManager.getInstance().register(TestBean.class, new TestBean());
|
||||
|
||||
final List<SimpleGrantedAuthority> strings = ListUtil.of(
|
||||
new SimpleGrantedAuthority("ROLE_admin"),
|
||||
|
@@ -17,19 +17,30 @@
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.lang.Console;
|
||||
import org.dromara.hutool.core.reflect.ClassUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
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.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class Issue3504Test {
|
||||
@Test
|
||||
public void test3504() {
|
||||
// 考虑到安全性,Class默认不支持序列化和反序列化,需要自行注册
|
||||
TypeAdapterManager.getInstance().register(Class.class,
|
||||
(JSONSerializer<Class<?>>) (bean, context) -> new JSONPrimitive(bean.getName(), ObjUtil.apply(context, JSONContext::config)));
|
||||
TypeAdapterManager.getInstance().register(Class.class,
|
||||
(JSONDeserializer<Class<?>>) (json, deserializeType) -> ClassUtil.forName((String)json.asJSONPrimitive().getValue(), true, null));
|
||||
|
||||
final JsonBean jsonBean = new JsonBean();
|
||||
jsonBean.setName("test");
|
||||
jsonBean.setClasses(new Class[]{String.class});
|
||||
final String huToolJsonStr = JSONUtil.toJsonStr(jsonBean);
|
||||
Console.log(huToolJsonStr);
|
||||
final JsonBean bean = JSONUtil.toBean(huToolJsonStr, JsonBean.class);
|
||||
|
||||
final String jsonStr = JSONUtil.toJsonStr(jsonBean);
|
||||
final JsonBean bean = JSONUtil.toBean(jsonStr, JsonBean.class);
|
||||
Assertions.assertNotNull(bean);
|
||||
Assertions.assertEquals("test", bean.getName());
|
||||
}
|
||||
|
@@ -16,8 +16,8 @@
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.annotation.Alias;
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.annotation.Alias;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -25,8 +25,10 @@ public class Issue867Test {
|
||||
|
||||
@Test
|
||||
public void toBeanTest(){
|
||||
final String json = "{\"abc_1d\":\"123\",\"abc_d\":\"456\",\"abc_de\":\"789\"}";
|
||||
final Test02 bean = JSONUtil.toBean(JSONUtil.parseObj(json),Test02.class);
|
||||
final String jsonStr = "{\"abc_1d\":\"123\",\"abc_d\":\"456\",\"abc_de\":\"789\"}";
|
||||
final JSONObject json = JSONUtil.parseObj(jsonStr);
|
||||
//Console.log(json);
|
||||
final Test02 bean = JSONUtil.toBean(json,Test02.class);
|
||||
Assertions.assertEquals("123", bean.getAbc1d());
|
||||
Assertions.assertEquals("456", bean.getAbcD());
|
||||
Assertions.assertEquals("789", bean.getAbcDe());
|
||||
|
@@ -20,7 +20,7 @@ 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.dromara.hutool.json.serializer.TypeAdapterManager;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -31,7 +31,7 @@ public class IssuesI44E4HTest {
|
||||
|
||||
@Test
|
||||
public void deserializerTest(){
|
||||
SerializerManager.getInstance().register(TestDto.class, (JSONDeserializer<TestDto>) (json, deserializeType) -> {
|
||||
TypeAdapterManager.getInstance().register(TestDto.class, (JSONDeserializer<TestDto>) (json, deserializeType) -> {
|
||||
final TestDto testDto = new TestDto();
|
||||
testDto.setMd(new AcBizModuleMd("name1", ((JSONObject)json).getStr("md")));
|
||||
return testDto;
|
||||
|
@@ -1,91 +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.writer;
|
||||
|
||||
import lombok.Data;
|
||||
import org.dromara.hutool.core.convert.Converter;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ValueWriterManagerTest {
|
||||
|
||||
@BeforeEach
|
||||
public void init(){
|
||||
ValueWriterManager.getInstance().register(new ValueWriter() {
|
||||
@Override
|
||||
public void write(final JSONWriter writer, final Object value) {
|
||||
writer.writeRaw(String.valueOf(((CustomSubBean)value).getId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean test(final Object value) {
|
||||
return value instanceof CustomSubBean;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customWriteTest(){
|
||||
final CustomSubBean customBean = new CustomSubBean();
|
||||
customBean.setId(12);
|
||||
customBean.setName("aaa");
|
||||
final String s = JSONUtil.toJsonStr(customBean);
|
||||
Assertions.assertEquals("12", s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customWriteSubTest(){
|
||||
final CustomSubBean customSubBean = new CustomSubBean();
|
||||
customSubBean.setId(12);
|
||||
customSubBean.setName("aaa");
|
||||
final CustomBean customBean = new CustomBean();
|
||||
customBean.setId(1);
|
||||
customBean.setSub(customSubBean);
|
||||
|
||||
final String s = JSONUtil.toJsonStr(customBean);
|
||||
Assertions.assertEquals("{\"id\":1,\"sub\":12}", s);
|
||||
|
||||
// 自定义转换
|
||||
final JSONConfig jsonConfig = JSONConfig.of();
|
||||
final Converter converter = jsonConfig.getConverter();
|
||||
jsonConfig.setConverter((targetType, value) -> {
|
||||
if(targetType == CustomSubBean.class){
|
||||
final CustomSubBean subBean = new CustomSubBean();
|
||||
subBean.setId((Integer) value);
|
||||
return subBean;
|
||||
}
|
||||
return converter.convert(targetType, value);
|
||||
});
|
||||
final CustomBean customBean1 = JSONUtil.parseObj(s, jsonConfig).toBean(CustomBean.class);
|
||||
Assertions.assertEquals(12, customBean1.getSub().getId());
|
||||
}
|
||||
|
||||
@Data
|
||||
static class CustomSubBean {
|
||||
private int id;
|
||||
private String name;
|
||||
}
|
||||
|
||||
@Data
|
||||
static class CustomBean{
|
||||
private int id;
|
||||
private CustomSubBean sub;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user