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);
zoneId = formatter.getZone();
} else {
final Date date = DateUtil.parse(value);
final DateTime date = DateUtil.parse(value);
instant = Objects.requireNonNull(date).toInstant();
if(date instanceof DateTime){
zoneId = ((DateTime) date).getZoneId();
}
zoneId = date.getZoneId();
}
return parseFromInstant(targetClass, instant, zoneId);
}

View File

@ -203,9 +203,18 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
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
public Object getOrDefault(final Object key, final Object defaultValue) {
Object value = super.getOrDefault(key, defaultValue);
Object value = super.getOrDefault(key, defaultValue);
if(value instanceof JSONPrimitive){
value = ((JSONPrimitive) value).getValue();
}

View File

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

View File

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

View File

@ -16,11 +16,11 @@
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.Opt;
import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.json.JSON;
import org.dromara.hutool.json.JSONException;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.*;
import org.dromara.hutool.json.serializer.JSONContext;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.JSONSerializer;
@ -101,6 +101,17 @@ public class TemporalAccessorSerializer implements JSONSerializer<TemporalAccess
@Override
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;
if (LocalDate.class.equals(this.temporalAccessorClass) || LocalDateTime.class.equals(this.temporalAccessorClass)) {
//

View File

@ -34,9 +34,7 @@ public class Issue2746Test {
@Test
public void parseTest() {
Assertions.assertThrows(JSONException.class, ()->{
final String str = StrUtil.repeat("[", 1500) + StrUtil.repeat("]", 1500);
JSONUtil.parseArray(str);
});
final String str = StrUtil.repeat("[", 1500) + StrUtil.repeat("]", 1500);
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.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.assertTrue;
@ -315,11 +318,12 @@ public class JSONArrayTest {
final String jsonArr = "[{\"id\":111,\"name\":\"test1\"},{\"id\":112,\"name\":\"test2\"}]";
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONArray array = new JSONArray(jsonArr, null, (mutable) -> {
final JSONObject o = new JSONObject(mutable.get());
if ("111".equals(o.getStr("id"))) {
o.set("name", "test1_edit");
if(mutable.getKey() instanceof Integer){
final JSONObject o = (JSONObject) mutable.getValue();
if ("111".equals(o.getStr("id"))) {
o.set("name", "test1_edit");
}
}
mutable.set(new AbstractMap.SimpleEntry<>(1, o));
return true;
});
assertEquals(2, array.size());

View File

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

View File

@ -16,10 +16,10 @@
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 lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;