mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-08-18 20:38:02 +08:00
fix code
This commit is contained in:
@@ -33,7 +33,7 @@ public class CustomSerializeTest {
|
||||
TypeAdapterManager.getInstance().register(CustomBean.class,
|
||||
(JSONSerializer<CustomBean>) (bean, context) ->{
|
||||
final JSONObject contextJson = context.getOrCreateObj();
|
||||
return contextJson.set("customName", bean.name);
|
||||
return contextJson.putObj("customName", bean.name);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class CustomSerializeTest {
|
||||
final CustomBean customBean = new CustomBean();
|
||||
customBean.name = "testName";
|
||||
|
||||
final JSONObject obj = JSONUtil.ofObj().set("customBean", customBean);
|
||||
final JSONObject obj = JSONUtil.ofObj().putObj("customBean", customBean);
|
||||
Assertions.assertEquals("testName", obj.getJSONObject("customBean").getStr("customName"));
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ import java.sql.SQLException;
|
||||
public class Issue1399Test {
|
||||
@Test
|
||||
void sqlExceptionTest() {
|
||||
final JSONObject set = JSONUtil.ofObj().set("error", new SQLException("test"));
|
||||
final JSONObject set = JSONUtil.ofObj().putObj("error", new SQLException("test"));
|
||||
|
||||
final String jsonStr = set.toString();
|
||||
Assertions.assertEquals("{\"error\":\"java.sql.SQLException: test\"}", jsonStr);
|
||||
|
||||
@@ -71,7 +71,7 @@ public class Issue2090Test {
|
||||
@Test
|
||||
public void monthTest(){
|
||||
final JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.set("month", Month.JANUARY);
|
||||
jsonObject.putObj("month", Month.JANUARY);
|
||||
Assertions.assertEquals("{\"month\":1}", jsonObject.toString());
|
||||
|
||||
final JSON parse = JSONUtil.parse(Month.JANUARY);
|
||||
@@ -83,7 +83,7 @@ public class Issue2090Test {
|
||||
@Test
|
||||
public void weekTest(){
|
||||
final JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.set("week", DayOfWeek.SUNDAY);
|
||||
jsonObject.putObj("week", DayOfWeek.SUNDAY);
|
||||
Assertions.assertEquals("{\"week\":7}", jsonObject.toString());
|
||||
|
||||
final JSON parse = JSONUtil.parse(DayOfWeek.SUNDAY);
|
||||
|
||||
@@ -57,7 +57,7 @@ public class Issue2555Test {
|
||||
public static class MySerializer implements JSONSerializer<MyType> {
|
||||
@Override
|
||||
public JSON serialize(final MyType bean, final JSONContext context) {
|
||||
return context.getOrCreateObj().set("addr", bean.getAddress());
|
||||
return context.getOrCreateObj().putObj("addr", bean.getAddress());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ public class Issue2572Test {
|
||||
final Set<DayOfWeek> weeks = new HashSet<>();
|
||||
weeks.add(DayOfWeek.MONDAY);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("weeks", weeks);
|
||||
obj.putObj("weeks", weeks);
|
||||
Assertions.assertEquals("{\"weeks\":[1]}", obj.toString());
|
||||
|
||||
final Map<String, Set<DayOfWeek>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<DayOfWeek>>>() {
|
||||
@@ -46,7 +46,7 @@ public class Issue2572Test {
|
||||
final Set<Month> months = new HashSet<>();
|
||||
months.add(Month.DECEMBER);
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("months", months);
|
||||
obj.putObj("months", months);
|
||||
Assertions.assertEquals("{\"months\":[12]}", obj.toString());
|
||||
|
||||
final Map<String, Set<Month>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<Month>>>() {
|
||||
@@ -59,7 +59,7 @@ public class Issue2572Test {
|
||||
final Set<MonthDay> monthDays = new HashSet<>();
|
||||
monthDays.add(MonthDay.of(Month.DECEMBER, 1));
|
||||
final JSONObject obj = new JSONObject();
|
||||
obj.set("monthDays", monthDays);
|
||||
obj.putObj("monthDays", monthDays);
|
||||
Assertions.assertEquals("{\"monthDays\":[\"--12-01\"]}", obj.toString());
|
||||
|
||||
final Map<String, Set<MonthDay>> monthDays1 = obj.toBean(new TypeReference<Map<String, Set<MonthDay>>>() {
|
||||
|
||||
@@ -70,7 +70,7 @@ public class Issue3086Test {
|
||||
public JSON serialize(final TestBean bean, final JSONContext context) {
|
||||
final List<String> strings = bean.getAuthorities()
|
||||
.stream().map(SimpleGrantedAuthority::getAuthority).collect(Collectors.toList());
|
||||
return context.getOrCreateObj().set("authorities",strings);
|
||||
return context.getOrCreateObj().putObj("authorities",strings);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@ public class IssueI3EGJPTest {
|
||||
@Test
|
||||
public void hutoolMapToBean() {
|
||||
final JSONObject paramJson = new JSONObject();
|
||||
paramJson.set("is_booleana", "1");
|
||||
paramJson.set("is_booleanb", true);
|
||||
paramJson.putObj("is_booleana", "1");
|
||||
paramJson.putObj("is_booleanb", true);
|
||||
final ConvertDO convertDO = paramJson.toBean(ConvertDO.class);
|
||||
|
||||
Assertions.assertTrue(convertDO.isBooleana());
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.junit.jupiter.api.Test;
|
||||
public class IssueI59LW4Test {
|
||||
@Test
|
||||
public void bytesTest(){
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().set("bytes", new byte[]{1});
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().putObj("bytes", new byte[]{1});
|
||||
Assertions.assertEquals("{\"bytes\":[1]}", jsonObject.toString());
|
||||
|
||||
final byte[] bytes = jsonObject.getBytes("bytes");
|
||||
@@ -31,7 +31,7 @@ public class IssueI59LW4Test {
|
||||
|
||||
@Test
|
||||
public void bytesInJSONArrayTest(){
|
||||
final JSONArray jsonArray = JSONUtil.ofArray().set(new byte[]{1});
|
||||
final JSONArray jsonArray = JSONUtil.ofArray().addObj(new byte[]{1});
|
||||
Assertions.assertEquals("[[1]]", jsonArray.toString());
|
||||
|
||||
final byte[] bytes = jsonArray.getBytes(0);
|
||||
|
||||
@@ -34,7 +34,7 @@ public class IssueI7VM64Test {
|
||||
map.put("a", "1");
|
||||
|
||||
final JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.set("c", map);
|
||||
jsonObject.putObj("c", map);
|
||||
map.put("b", 2);
|
||||
|
||||
//Console.log("Hutool JSON: " + jsonObject);
|
||||
|
||||
@@ -51,7 +51,7 @@ public class JSONArrayTest {
|
||||
JSONArray jsonArray = JSONUtil.parseArray(jsonObject, JSONConfig.of());
|
||||
assertEquals(new JSONArray(), jsonArray);
|
||||
|
||||
jsonObject.set("key1", "value1");
|
||||
jsonObject.putObj("key1", "value1");
|
||||
jsonArray = JSONUtil.parseArray(jsonObject, JSONConfig.of());
|
||||
assertEquals(1, jsonArray.size());
|
||||
assertEquals("[{\"key1\":\"value1\"}]", jsonArray.toString());
|
||||
@@ -70,9 +70,9 @@ public class JSONArrayTest {
|
||||
final JSONArray array = JSONUtil.ofArray();
|
||||
// 方法2
|
||||
// JSONArray array = new JSONArray();
|
||||
array.set("value1");
|
||||
array.set("value2");
|
||||
array.set("value3");
|
||||
array.addObj("value1");
|
||||
array.addObj("value2");
|
||||
array.addObj("value3");
|
||||
|
||||
assertEquals(array.getObj(0), "value1");
|
||||
}
|
||||
@@ -240,12 +240,12 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void putToIndexTest() {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.setValue(3, "test");
|
||||
jsonArray.setObj(3, "test");
|
||||
// 默认忽略null值,因此空位无值,只有一个值
|
||||
assertEquals(1, jsonArray.size());
|
||||
|
||||
jsonArray = new JSONArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||
jsonArray.setValue(2, "test");
|
||||
jsonArray.setObj(2, "test");
|
||||
// 第三个位置插入值,0~2都是null
|
||||
assertEquals(3, jsonArray.size());
|
||||
}
|
||||
@@ -254,7 +254,7 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void putTest2() {
|
||||
final JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.setValue(0, 1);
|
||||
jsonArray.setObj(0, 1);
|
||||
assertEquals(1, jsonArray.size());
|
||||
assertEquals(1, jsonArray.getObj(0));
|
||||
}
|
||||
@@ -276,10 +276,10 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void filterIncludeTest() {
|
||||
final JSONArray json1 = JSONUtil.ofArray()
|
||||
.set("value1")
|
||||
.set("value2")
|
||||
.set("value3")
|
||||
.set(true);
|
||||
.addObj("value1")
|
||||
.addObj("value2")
|
||||
.addObj("value3")
|
||||
.addObj(true);
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> ((JSONPrimitive)pair.getValue()).getValue().equals("value2"));
|
||||
assertEquals("[\"value2\"]", s);
|
||||
@@ -288,10 +288,10 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void filterExcludeTest() {
|
||||
final JSONArray json1 = JSONUtil.ofArray()
|
||||
.set("value1")
|
||||
.set("value2")
|
||||
.set("value3")
|
||||
.set(true);
|
||||
.addObj("value1")
|
||||
.addObj("value2")
|
||||
.addObj("value3")
|
||||
.addObj(true);
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> !((JSONPrimitive)pair.getValue()).getValue().equals("value2"));
|
||||
assertEquals("[\"value1\",\"value3\",true]", s);
|
||||
@@ -300,7 +300,7 @@ public class JSONArrayTest {
|
||||
@Test
|
||||
public void putNullTest() {
|
||||
final JSONArray array = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false));
|
||||
array.set(null);
|
||||
array.addObj(null);
|
||||
|
||||
assertEquals("[null]", array.toString());
|
||||
}
|
||||
@@ -322,7 +322,7 @@ public class JSONArrayTest {
|
||||
if(mutable.getKey() instanceof Integer){
|
||||
final JSONObject o = (JSONObject) mutable.getValue();
|
||||
if ("111".equals(o.getStr("id"))) {
|
||||
o.set("name", "test1_edit");
|
||||
o.putObj("name", "test1_edit");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -335,9 +335,9 @@ public class JSONArrayTest {
|
||||
@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"));
|
||||
array.add(JSONUtil.ofObj().putObj("name", "aaa"));
|
||||
array.add(JSONUtil.ofObj().putObj("name", "bbb"));
|
||||
array.add(JSONUtil.ofObj().putObj("name", "ccc"));
|
||||
|
||||
final StringBuilder result = new StringBuilder();
|
||||
array.forEach(result::append);
|
||||
|
||||
@@ -51,22 +51,22 @@ public class JSONNullTest {
|
||||
@Test
|
||||
public void setNullTest(){
|
||||
// 忽略null
|
||||
String json1 = JSONUtil.ofObj().set("key1", null).toString();
|
||||
String json1 = JSONUtil.ofObj().putObj("key1", null).toString();
|
||||
Assertions.assertEquals("{}", json1);
|
||||
|
||||
// 不忽略null
|
||||
json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false)).set("key1", null).toString();
|
||||
json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false)).putObj("key1", null).toString();
|
||||
Assertions.assertEquals("{\"key1\":null}", json1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setNullOfJSONArrayTest(){
|
||||
// 忽略null
|
||||
String json1 = JSONUtil.ofArray().set(null).toString();
|
||||
String json1 = JSONUtil.ofArray().addObj(null).toString();
|
||||
Assertions.assertEquals("[]", json1);
|
||||
|
||||
// 不忽略null
|
||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).set(null).toString();
|
||||
json1 = JSONUtil.ofArray(JSONConfig.of().setIgnoreNullValue(false)).addObj(null).toString();
|
||||
Assertions.assertEquals("[null]", json1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,18 +75,18 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void toStringTest3() {
|
||||
final JSONObject json = JSONUtil.ofObj(JSONConfig.of().setDateFormat(DatePattern.NORM_DATE_PATTERN))//
|
||||
.set("dateTime", DateUtil.parse("2019-05-02 22:12:01"));
|
||||
.putObj("dateTime", DateUtil.parse("2019-05-02 22:12:01"));
|
||||
assertEquals("{\"dateTime\":\"2019-05-02\"}", json.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringWithDateTest() {
|
||||
JSONObject json = JSONUtil.ofObj().set("date", DateUtil.parse("2019-05-08 19:18:21"));
|
||||
JSONObject json = JSONUtil.ofObj().putObj("date", DateUtil.parse("2019-05-08 19:18:21"));
|
||||
assert json != null;
|
||||
assertEquals("{\"date\":1557314301000}", json.toString());
|
||||
|
||||
json = JSONUtil.ofObj(JSONConfig.of().setDateFormat(DatePattern.NORM_DATE_PATTERN))
|
||||
.set("date", DateUtil.parse("2019-05-08 19:18:21"));
|
||||
.putObj("date", DateUtil.parse("2019-05-08 19:18:21"));
|
||||
assertEquals("{\"date\":\"2019-05-08\"}", json.toString());
|
||||
}
|
||||
|
||||
@@ -94,14 +94,14 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void putAllTest() {
|
||||
final JSONObject json1 = JSONUtil.ofObj()
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
.set("d", true);
|
||||
.putObj("a", "value1")
|
||||
.putObj("b", "value2")
|
||||
.putObj("c", "value3")
|
||||
.putObj("d", true);
|
||||
|
||||
final JSONObject json2 = JSONUtil.ofObj()
|
||||
.set("a", "value21")
|
||||
.set("b", "value22");
|
||||
.putObj("a", "value21")
|
||||
.putObj("b", "value22");
|
||||
|
||||
// putAll操作会覆盖相同key的值,因此a,b两个key的值改变,c的值不变
|
||||
json1.putAll(json2);
|
||||
@@ -193,12 +193,12 @@ public class JSONObjectTest {
|
||||
|
||||
@Test
|
||||
public void toBeanTest() {
|
||||
final JSONObject subJson = JSONUtil.ofObj().set("value1", "strValue1").set("value2", "234");
|
||||
final JSONObject json = JSONUtil.ofObj(JSONConfig.of().setIgnoreError(true)).set("strValue", "strTest").set("intValue", 123)
|
||||
final JSONObject subJson = JSONUtil.ofObj().putObj("value1", "strValue1").putObj("value2", "234");
|
||||
final JSONObject json = JSONUtil.ofObj(JSONConfig.of().setIgnoreError(true)).putObj("strValue", "strTest").putObj("intValue", 123)
|
||||
// 测试空字符串转对象
|
||||
.set("doubleValue", "")
|
||||
.set("beanValue", subJson)
|
||||
.set("list", JSONUtil.ofArray().set("a").set("b")).set("testEnum", "TYPE_A");
|
||||
.putObj("doubleValue", "")
|
||||
.putObj("beanValue", subJson)
|
||||
.putObj("list", JSONUtil.ofArray().addObj("a").addObj("b")).putObj("testEnum", "TYPE_A");
|
||||
|
||||
final TestBean bean = json.toBean(TestBean.class);
|
||||
assertEquals("a", bean.getList().get(0));
|
||||
@@ -214,11 +214,11 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void toBeanNullStrTest() {
|
||||
final JSONObject json = JSONUtil.ofObj(JSONConfig.of().setIgnoreError(true))//
|
||||
.set("strValue", "null")//
|
||||
.set("intValue", 123)//
|
||||
.putObj("strValue", "null")//
|
||||
.putObj("intValue", 123)//
|
||||
// 子对象对应"null"字符串,如果忽略错误,跳过,否则抛出转换异常
|
||||
.set("beanValue", "null")//
|
||||
.set("list", JSONUtil.ofArray().set("a").set("b"));
|
||||
.putObj("beanValue", "null")//
|
||||
.putObj("list", JSONUtil.ofArray().addObj("a").addObj("b"));
|
||||
|
||||
final TestBean bean = json.toBean(TestBean.class);
|
||||
// 当JSON中为字符串"null"时应被当作字符串处理
|
||||
@@ -230,7 +230,7 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
void addListTest(){
|
||||
final JSONObject json = JSONUtil.ofObj();
|
||||
json.set("list", ListUtil.of(1, 2, 3));
|
||||
json.putObj("list", ListUtil.of(1, 2, 3));
|
||||
Assertions.assertEquals("{\"list\":[1,2,3]}", json.toString());
|
||||
}
|
||||
|
||||
@@ -288,11 +288,11 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void toBeanTest6() {
|
||||
final JSONObject json = JSONUtil.ofObj()
|
||||
.set("targetUrl", "http://test.com")
|
||||
.set("success", "true")
|
||||
.set("result", JSONUtil.ofObj()
|
||||
.set("token", "tokenTest")
|
||||
.set("userId", "测试用户1"));
|
||||
.putObj("targetUrl", "http://test.com")
|
||||
.putObj("success", "true")
|
||||
.putObj("result", JSONUtil.ofObj()
|
||||
.putObj("token", "tokenTest")
|
||||
.putObj("userId", "测试用户1"));
|
||||
|
||||
final TokenAuthWarp2 bean = json.toBean(TokenAuthWarp2.class);
|
||||
assertEquals("http://test.com", bean.getTargetUrl());
|
||||
@@ -353,8 +353,8 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void parseBeanTest3() {
|
||||
final JSONObject json = JSONUtil.ofObj()
|
||||
.set("code", 22)
|
||||
.set("data", "{\"jobId\": \"abc\", \"videoUrl\": \"http://a.com/a.mp4\"}");
|
||||
.putObj("code", 22)
|
||||
.putObj("data", "{\"jobId\": \"abc\", \"videoUrl\": \"http://a.com/a.mp4\"}");
|
||||
|
||||
final JSONBean bean = json.toBean(JSONBean.class);
|
||||
assertEquals(22, bean.getCode());
|
||||
@@ -393,9 +393,9 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void beanTransTest3() {
|
||||
final JSONObject userAJson = JSONUtil.ofObj()
|
||||
.set("a", "AValue")
|
||||
.set("name", "nameValue")
|
||||
.set("date", "08:00:00");
|
||||
.putObj("a", "AValue")
|
||||
.putObj("name", "nameValue")
|
||||
.putObj("date", "08:00:00");
|
||||
final UserA bean = JSONUtil.toBean(userAJson.toString(), UserA.class);
|
||||
assertEquals(DateUtil.formatToday() + " 08:00:00", DateUtil.date(bean.getDate()).toString());
|
||||
}
|
||||
@@ -449,8 +449,8 @@ public class JSONObjectTest {
|
||||
assertEquals(Integer.valueOf(35), jsonObject.getInt("age"));
|
||||
|
||||
final JSONObject json = JSONUtil.ofObj()
|
||||
.set("name", "张三")
|
||||
.set("age", 35);
|
||||
.putObj("name", "张三")
|
||||
.putObj("age", 35);
|
||||
final BeanWithAlias bean = JSONUtil.toBean(Objects.requireNonNull(json).toString(), BeanWithAlias.class);
|
||||
assertEquals("张三", bean.getValue1());
|
||||
assertEquals(Integer.valueOf(35), bean.getValue2());
|
||||
@@ -475,9 +475,9 @@ public class JSONObjectTest {
|
||||
|
||||
final Date date = DateUtil.parse("2020-06-05 11:16:11");
|
||||
final JSONObject json = new JSONObject(jsonConfig);
|
||||
json.set("date", date);
|
||||
json.set("bbb", "222");
|
||||
json.set("aaa", "123");
|
||||
json.putObj("date", date);
|
||||
json.putObj("bbb", "222");
|
||||
json.putObj("aaa", "123");
|
||||
|
||||
final String jsonStr = "{\"date\":\"2020#06#05\",\"bbb\":\"222\",\"aaa\":\"123\"}";
|
||||
|
||||
@@ -495,7 +495,7 @@ public class JSONObjectTest {
|
||||
|
||||
final Date date = DateUtil.parse("2020-06-05 11:16:11");
|
||||
final JSONObject json = new JSONObject(jsonConfig);
|
||||
json.set("date", date);
|
||||
json.putObj("date", date);
|
||||
|
||||
assertEquals("{\"date\":1591326971}", json.toString());
|
||||
|
||||
@@ -511,9 +511,9 @@ public class JSONObjectTest {
|
||||
|
||||
final Date date = DateUtil.parse("2020-06-05 11:16:11");
|
||||
final JSONObject json = new JSONObject(jsonConfig);
|
||||
json.set("date", date);
|
||||
json.set("bbb", "222");
|
||||
json.set("aaa", "123");
|
||||
json.putObj("date", date);
|
||||
json.putObj("bbb", "222");
|
||||
json.putObj("aaa", "123");
|
||||
|
||||
final String jsonStr = "{\"date\":1591326971,\"bbb\":\"222\",\"aaa\":\"123\"}";
|
||||
|
||||
@@ -527,7 +527,7 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void getTimestampTest() {
|
||||
final String timeStr = "1970-01-01 00:00:00";
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().set("time", timeStr);
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().putObj("time", timeStr);
|
||||
final Timestamp time = jsonObject.get("time", Timestamp.class);
|
||||
assertEquals("1970-01-01 00:00:00.0", time.toString());
|
||||
}
|
||||
@@ -670,10 +670,10 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void filterIncludeTest() {
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
.set("d", true);
|
||||
.putObj("a", "value1")
|
||||
.putObj("b", "value2")
|
||||
.putObj("c", "value3")
|
||||
.putObj("d", true);
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> pair.getKey().equals("b"));
|
||||
assertEquals("{\"b\":\"value2\"}", s);
|
||||
@@ -682,10 +682,10 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void filterExcludeTest() {
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
.set("d", true);
|
||||
.putObj("a", "value1")
|
||||
.putObj("b", "value2")
|
||||
.putObj("c", "value3")
|
||||
.putObj("d", true);
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> !pair.getKey().equals("b"));
|
||||
assertEquals("{\"a\":\"value1\",\"c\":\"value3\",\"d\":true}", s);
|
||||
@@ -694,10 +694,10 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void editTest() {
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("a", "value1")
|
||||
.set("b", "value2")
|
||||
.set("c", "value3")
|
||||
.set("d", true);
|
||||
.putObj("a", "value1")
|
||||
.putObj("b", "value2")
|
||||
.putObj("c", "value3")
|
||||
.putObj("d", true);
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> {
|
||||
if ("b".equals(pair.getKey())) {
|
||||
@@ -715,10 +715,10 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void toUnderLineCaseTest() {
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of())
|
||||
.set("aKey", "value1")
|
||||
.set("bJob", "value2")
|
||||
.set("cGood", "value3")
|
||||
.set("d", true);
|
||||
.putObj("aKey", "value1")
|
||||
.putObj("bJob", "value2")
|
||||
.putObj("cGood", "value3")
|
||||
.putObj("d", true);
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> {
|
||||
pair.setKey(StrUtil.toUnderlineCase((String)pair.getKey()));
|
||||
@@ -730,8 +730,8 @@ public class JSONObjectTest {
|
||||
@Test
|
||||
public void nullToEmptyTest() {
|
||||
final JSONObject json1 = JSONUtil.ofObj(JSONConfig.of().setIgnoreNullValue(false))
|
||||
.set("a", null)
|
||||
.set("b", "value2");
|
||||
.putObj("a", null)
|
||||
.putObj("b", "value2");
|
||||
|
||||
final String s = json1.toJSONString(0, (pair) -> {
|
||||
pair.setValue(ObjUtil.defaultIfNull(pair.getValue(), StrUtil.EMPTY));
|
||||
|
||||
@@ -186,9 +186,9 @@ public class JSONUtilTest {
|
||||
public void toJsonStrTest3() {
|
||||
// 验证某个字段为JSON字符串时转义是否规范
|
||||
final JSONObject object = new JSONObject(JSONConfig.of().setIgnoreError(true));
|
||||
object.set("name", "123123");
|
||||
object.set("value", "\\");
|
||||
object.set("value2", "</");
|
||||
object.putObj("name", "123123");
|
||||
object.putObj("value", "\\");
|
||||
object.putObj("value2", "</");
|
||||
|
||||
final HashMap<String, String> map = MapUtil.newHashMap();
|
||||
map.put("user", object.toString());
|
||||
@@ -277,12 +277,12 @@ public class JSONUtilTest {
|
||||
public void setStripTrailingZerosTest() {
|
||||
// 默认去除多余的0
|
||||
final JSONObject jsonObjectDefault = JSONUtil.ofObj()
|
||||
.set("test2", 12.00D);
|
||||
.putObj("test2", 12.00D);
|
||||
assertEquals("{\"test2\":12}", jsonObjectDefault.toString());
|
||||
|
||||
// 不去除多余的0
|
||||
final JSONObject jsonObject = JSONUtil.ofObj(JSONConfig.of().setStripTrailingZeros(false))
|
||||
.set("test2", 12.00D);
|
||||
.putObj("test2", 12.00D);
|
||||
assertEquals("{\"test2\":12.0}", jsonObject.toString());
|
||||
|
||||
// 去除多余的0
|
||||
@@ -304,7 +304,7 @@ public class JSONUtilTest {
|
||||
public void sqlExceptionTest() {
|
||||
//https://github.com/dromara/hutool/issues/1399
|
||||
// SQLException实现了Iterable接口,默认是遍历之,会栈溢出,修正后只返回string
|
||||
final JSONObject set = JSONUtil.ofObj().set("test", new SQLException("test"));
|
||||
final JSONObject set = JSONUtil.ofObj().putObj("test", new SQLException("test"));
|
||||
assertEquals("{\"test\":\"java.sql.SQLException: test\"}", set.toString());
|
||||
}
|
||||
|
||||
@@ -318,8 +318,8 @@ public class JSONUtilTest {
|
||||
@Test
|
||||
public void toXmlTest() {
|
||||
final JSONObject obj = JSONUtil.ofObj();
|
||||
obj.set("key1", "v1")
|
||||
.set("key2", ListUtil.view("a", "b", "c"));
|
||||
obj.putObj("key1", "v1")
|
||||
.putObj("key2", ListUtil.view("a", "b", "c"));
|
||||
final String xmlStr = JSONUtil.toXmlStr(obj);
|
||||
assertEquals("<key1>v1</key1><key2>a</key2><key2>b</key2><key2>c</key2>", xmlStr);
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public class JSONWriterTest {
|
||||
@Test
|
||||
public void writeDateTest() {
|
||||
final JSONObject jsonObject = JSONUtil.ofObj(JSONConfig.of().setDateFormat("yyyy-MM-dd"))
|
||||
.set("date", DateUtil.parse("2022-09-30"));
|
||||
.putObj("date", DateUtil.parse("2022-09-30"));
|
||||
|
||||
// 日期原样写入
|
||||
final Date date = jsonObject.getDate("date");
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.junit.jupiter.api.Test;
|
||||
public class Pr3507Test {
|
||||
@Test
|
||||
void writeClassTest() {
|
||||
final JSONObject set = JSONUtil.ofObj().set("name", Pr3507Test.class);
|
||||
final JSONObject set = JSONUtil.ofObj().putObj("name", Pr3507Test.class);
|
||||
Assertions.assertEquals("{\"name\":\"org.dromara.hutool.json.Pr3507Test\"}", set.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ public class XMLTest {
|
||||
@Test
|
||||
public void toXmlTest(){
|
||||
final JSONObject put = JSONUtil.ofObj()
|
||||
.set("aaa", "你好")
|
||||
.set("键2", "test");
|
||||
.putObj("aaa", "你好")
|
||||
.putObj("键2", "test");
|
||||
final String s = JSONUtil.toXmlStr(put);
|
||||
Assertions.assertEquals("<aaa>你好</aaa><键2>test</键2>", s);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ public class XMLTest {
|
||||
|
||||
@Test
|
||||
public void xmlContentTest(){
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().set("content","123456");
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().putObj("content","123456");
|
||||
|
||||
String xml = JSONXMLUtil.toXml(jsonObject);
|
||||
Assertions.assertEquals("123456", xml);
|
||||
@@ -56,7 +56,7 @@ public class XMLTest {
|
||||
|
||||
@Test
|
||||
public void xmlContentTest2(){
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().set("content","123456");
|
||||
final JSONObject jsonObject = JSONUtil.ofObj().putObj("content","123456");
|
||||
final String xml = JSONXMLUtil.toXml(jsonObject, null, new String[0]);
|
||||
Assertions.assertEquals("<content>123456</content>", xml);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user