This commit is contained in:
Looly 2024-09-13 12:58:56 +08:00
parent ffe1adbe46
commit 98de15b455
9 changed files with 55 additions and 44 deletions

View File

@ -151,11 +151,9 @@ public class TemporalAccessorConverter extends AbstractConverter {
instant = formatter.parse(value, Instant::from); instant = formatter.parse(value, Instant::from);
zoneId = formatter.getZone(); zoneId = formatter.getZone();
} else { } else {
final Date date = DateUtil.parse(value); final DateTime date = DateUtil.parse(value);
instant = Objects.requireNonNull(date).toInstant(); instant = Objects.requireNonNull(date).toInstant();
if(date instanceof DateTime){ zoneId = date.getZoneId();
zoneId = ((DateTime) date).getZoneId();
}
} }
return parseFromInstant(targetClass, instant, zoneId); return parseFromInstant(targetClass, instant, zoneId);
} }

View File

@ -203,6 +203,15 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
return getOrDefault(key, defaultValue); return getOrDefault(key, defaultValue);
} }
@Override
public Object get(final Object key) {
Object value = super.get(key);
if(value instanceof JSONPrimitive){
value = ((JSONPrimitive) value).getValue();
}
return value;
}
@Override @Override
public Object getOrDefault(final Object key, final Object defaultValue) { public Object getOrDefault(final Object key, final Object defaultValue) {
Object value = super.getOrDefault(key, defaultValue); Object value = super.getOrDefault(key, defaultValue);

View File

@ -18,10 +18,7 @@ package org.dromara.hutool.json.mapper;
import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.JSON; import org.dromara.hutool.json.*;
import org.dromara.hutool.json.JSONArray;
import org.dromara.hutool.json.JSONConfig;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.serializer.JSONStringer; import org.dromara.hutool.json.serializer.JSONStringer;
import org.dromara.hutool.json.writer.GlobalValueWriters; import org.dromara.hutool.json.writer.GlobalValueWriters;
@ -91,6 +88,10 @@ public class JSONValueMapper implements Serializable {
|| object instanceof CharSequence // || object instanceof CharSequence //
|| ObjUtil.isBasicType(object) // || ObjUtil.isBasicType(object) //
) { ) {
if(object instanceof JSONPrimitive){
return ((JSONPrimitive) object).getValue();
}
return object; return object;
} }

View File

@ -183,14 +183,14 @@ public class JSONParser {
tokener.nextColon(); tokener.nextColon();
// 过滤并设置键值对 // 过滤并设置键值对
Object value = parse(); JSON value = parse();
// 添加前置过滤通过MutablePair实现过滤修改键值对等 // 添加前置过滤通过MutablePair实现过滤修改键值对等
if (null != predicate) { if (null != predicate) {
final MutableEntry<Object, Object> pair = new MutableEntry<>(key, value); final MutableEntry<Object, Object> entry = new MutableEntry<>(key, value);
if (predicate.test(pair)) { if (predicate.test(entry)) {
// 使用修改后的键值对 // 使用修改后的键值对
key = (String) pair.getKey(); key = (String) entry.getKey();
value = pair.getValue(); value = (JSON) entry.getValue();
jsonObject.set(key, value); jsonObject.set(key, value);
} }
}else { }else {

View File

@ -16,11 +16,11 @@
package org.dromara.hutool.json.serializer.impl; package org.dromara.hutool.json.serializer.impl;
import org.dromara.hutool.core.convert.impl.TemporalAccessorConverter;
import org.dromara.hutool.core.lang.Assert; import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.Opt;
import org.dromara.hutool.core.math.NumberUtil; import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.json.JSON; import org.dromara.hutool.json.*;
import org.dromara.hutool.json.JSONException;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.serializer.JSONContext; import org.dromara.hutool.json.serializer.JSONContext;
import org.dromara.hutool.json.serializer.JSONDeserializer; import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.JSONSerializer; import org.dromara.hutool.json.serializer.JSONSerializer;
@ -101,6 +101,17 @@ public class TemporalAccessorSerializer implements JSONSerializer<TemporalAccess
@Override @Override
public TemporalAccessor deserialize(final JSON json, final Type deserializeType) { public TemporalAccessor deserialize(final JSON json, final Type deserializeType) {
// JSONPrimitive
if(json instanceof JSONPrimitive){
final Object value = ((JSONPrimitive) json).getValue();
final TemporalAccessorConverter converter = new TemporalAccessorConverter(
Opt.ofNullable(json.config()).map(JSONConfig::getDateFormat).getOrNull());
return (TemporalAccessor) converter.convert(deserializeType, value);
}
// TODO JSONArray
// JSONObject
final JSONObject jsonObject = (JSONObject) json; final JSONObject jsonObject = (JSONObject) json;
if (LocalDate.class.equals(this.temporalAccessorClass) || LocalDateTime.class.equals(this.temporalAccessorClass)) { if (LocalDate.class.equals(this.temporalAccessorClass) || LocalDateTime.class.equals(this.temporalAccessorClass)) {
// //

View File

@ -34,9 +34,7 @@ public class Issue2746Test {
@Test @Test
public void parseTest() { public void parseTest() {
Assertions.assertThrows(JSONException.class, ()->{
final String str = StrUtil.repeat("[", 1500) + StrUtil.repeat("]", 1500); final String str = StrUtil.repeat("[", 1500) + StrUtil.repeat("]", 1500);
JSONUtil.parseArray(str); JSONUtil.parseArray(str);
});
} }
} }

View File

@ -29,7 +29,10 @@ import org.dromara.hutool.json.test.bean.KeyBean;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.*; import java.util.ArrayList;
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.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@ -315,11 +318,12 @@ public class JSONArrayTest {
final String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]"; final String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
//noinspection MismatchedQueryAndUpdateOfCollection //noinspection MismatchedQueryAndUpdateOfCollection
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> { final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> {
final JSONObject o = new JSONObject(mutable.get()); if(mutable.getKey() instanceof Integer){
final JSONObject o = (JSONObject) mutable.getValue();
if ("111".equals(o.getStr("id"))) { if ("111".equals(o.getStr("id"))) {
o.set("name", "test1_edit"); o.set("name", "test1_edit");
} }
mutable.set(new AbstractMap.SimpleEntry<>(1, o)); }
return true; return true;
}); });
assertEquals(2, array.size()); assertEquals(2, array.size());

View File

@ -16,27 +16,21 @@
package org.dromara.hutool.json; package org.dromara.hutool.json;
import lombok.Data;
import org.dromara.hutool.core.annotation.Alias; import org.dromara.hutool.core.annotation.Alias;
import org.dromara.hutool.core.annotation.PropIgnore; import org.dromara.hutool.core.annotation.PropIgnore;
import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DatePattern; import org.dromara.hutool.core.date.DatePattern;
import org.dromara.hutool.core.date.DateUtil; import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.io.resource.ResourceUtil; import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.map.MapUtil; import org.dromara.hutool.core.map.MapUtil;
import org.dromara.hutool.core.text.StrUtil; import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.test.bean.JSONBean; import org.dromara.hutool.json.test.bean.*;
import org.dromara.hutool.json.test.bean.ResultDto;
import org.dromara.hutool.json.test.bean.Seq;
import org.dromara.hutool.json.test.bean.TokenAuthResponse;
import org.dromara.hutool.json.test.bean.TokenAuthWarp2;
import org.dromara.hutool.json.test.bean.UserA;
import org.dromara.hutool.json.test.bean.UserB;
import org.dromara.hutool.json.test.bean.UserWithMap;
import org.dromara.hutool.json.test.bean.report.CaseReport; import org.dromara.hutool.json.test.bean.report.CaseReport;
import org.dromara.hutool.json.test.bean.report.StepReport; import org.dromara.hutool.json.test.bean.report.StepReport;
import org.dromara.hutool.json.test.bean.report.SuiteReport; import org.dromara.hutool.json.test.bean.report.SuiteReport;
import lombok.Data;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -45,12 +39,7 @@ import java.io.StringReader;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.Date; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/** /**
* JSONObject单元测试 * JSONObject单元测试
@ -195,7 +184,7 @@ public class JSONObjectTest {
final String jsonStr = "{\"a\":\"<div>aaa</div>\"}"; final String jsonStr = "{\"a\":\"<div>aaa</div>\"}";
//noinspection MismatchedQueryAndUpdateOfCollection //noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject json = new JSONObject(jsonStr); final JSONObject json = new JSONObject(jsonStr);
Assertions.assertEquals("<div>aaa</div>", json.get("a")); Assertions.assertEquals("<div>aaa</div>", json.getObj("a"));
Assertions.assertEquals(jsonStr, json.toString()); Assertions.assertEquals(jsonStr, json.toString());
} }
@ -752,7 +741,8 @@ public class JSONObjectTest {
//noinspection MismatchedQueryAndUpdateOfCollection //noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = new JSONObject(jsonStr, null, (pair)-> { final JSONObject jsonObject = new JSONObject(jsonStr, null, (pair)-> {
if("b".equals(pair.getKey())){ if("b".equals(pair.getKey())){
pair.setValue(pair.getValue() + "_edit"); final JSONPrimitive primitive = (JSONPrimitive) pair.getValue();
pair.setValue(primitive.getValue() + "_edit");
} }
return true; return true;
}); });

View File

@ -16,10 +16,10 @@
package org.dromara.hutool.json.writer; package org.dromara.hutool.json.writer;
import lombok.Data;
import org.dromara.hutool.core.convert.Converter; import org.dromara.hutool.core.convert.Converter;
import org.dromara.hutool.json.JSONConfig; import org.dromara.hutool.json.JSONConfig;
import org.dromara.hutool.json.JSONUtil; import org.dromara.hutool.json.JSONUtil;
import lombok.Data;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;