This commit is contained in:
Looly 2023-04-11 22:05:28 +08:00
parent 19f6ea76ca
commit eac3e0ae27
13 changed files with 30 additions and 30 deletions

View File

@ -230,7 +230,7 @@ public final class InternalJSONUtil {
final String segment = path[i];
JSONObject nextTarget = target.getJSONObject(segment);
if (nextTarget == null) {
nextTarget = new JSONObject(target.getConfig());
nextTarget = new JSONObject(target.config());
target.set(segment, nextTarget, predicate);
}
target = nextTarget;

View File

@ -37,7 +37,7 @@ public interface JSON extends Converter, Cloneable, Serializable {
* @return {@link JSONConfig}
* @since 5.3.0
*/
JSONConfig getConfig();
JSONConfig config();
/**
* JSON大小对于JSONObject是键值对的多少JSONArray则是元素的个数
@ -123,7 +123,7 @@ public interface JSON extends Converter, Cloneable, Serializable {
*/
@SuppressWarnings("unchecked")
default <T> T getByPath(final String expression, final Class<T> resultType){
return (T) getConfig().getConverter().convert(resultType, getByPath(expression));
return (T) config().getConverter().convert(resultType, getByPath(expression));
}
/**
@ -191,6 +191,6 @@ public interface JSON extends Converter, Cloneable, Serializable {
@Override
default Object convert(final Type targetType, final Object value) throws ConvertException {
return JSONConverter.of(getConfig()).convert(targetType, value);
return JSONConverter.of(config()).convert(targetType, value);
}
}

View File

@ -165,7 +165,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
// endregion
@Override
public JSONConfig getConfig() {
public JSONConfig config() {
return this.config;
}

View File

@ -32,7 +32,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
* @return {@link JSONConfig}
* @since 5.3.0
*/
JSONConfig getConfig();
JSONConfig config();
/**
* key对应值是否为{@code null}或无此key
@ -83,7 +83,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
if (object instanceof JSON) {
return (JSONArray) object;
}
return new JSONArray(object, getConfig());
return new JSONArray(object, config());
}
/**
@ -102,7 +102,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
if (object instanceof JSON) {
return (JSONObject) object;
}
return new JSONObject(object, getConfig());
return new JSONObject(object, config());
}
/**
@ -142,6 +142,6 @@ public interface JSONGetter<K> extends TypeGetter<K> {
return defaultValue;
}
return get(key, type, getConfig().getConverter(), defaultValue);
return get(key, type, config().getConverter(), defaultValue);
}
}

View File

@ -149,7 +149,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
// -------------------------------------------------------------------------------------------------------------------- Constructor end
@Override
public JSONConfig getConfig() {
public JSONConfig config() {
return this.config;
}
@ -215,7 +215,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
*/
@Override
public Object put(final String key, final Object value) throws JSONException {
return put(key, value, null, getConfig().isCheckDuplicate());
return put(key, value, null, config().isCheckDuplicate());
}
/**
@ -258,7 +258,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
* @since 5.8.0
*/
public JSONObject set(final String key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) throws JSONException {
put(key, value, predicate, getConfig().isCheckDuplicate());
put(key, value, predicate, config().isCheckDuplicate());
return this;
}

View File

@ -42,7 +42,7 @@ public class JSONSupport implements JSONStringer, JSONDeserializer<Object> {
public Object deserialize(final JSON json) {
BeanCopier.of(json,
this, this.getClass(),
InternalJSONUtil.toCopyOptions(json.getConfig())).copy();
InternalJSONUtil.toCopyOptions(json.config())).copy();
return this;
}

View File

@ -212,11 +212,11 @@ public class JSONConverter implements Converter {
if (BeanUtil.isBean(rawType)) {
return BeanCopier.of(json,
ConstructorUtil.newInstanceIfPossible(rawType), targetType,
InternalJSONUtil.toCopyOptions(json.getConfig())).copy();
InternalJSONUtil.toCopyOptions(json.config())).copy();
}
// 跳过异常时返回null
if (json.getConfig().isIgnoreError()) {
if (json.config().isIgnoreError()) {
return null;
}

View File

@ -96,13 +96,13 @@ public class JSONArrayMapper {
// JSON字符串
mapFromStr((CharSequence) source, jsonArray);
} else if (source instanceof Reader) {
mapFromTokener(new JSONTokener((Reader) source, jsonArray.getConfig()), jsonArray);
mapFromTokener(new JSONTokener((Reader) source, jsonArray.config()), jsonArray);
} else if (source instanceof InputStream) {
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.getConfig()), jsonArray);
mapFromTokener(new JSONTokener((InputStream) source, jsonArray.config()), jsonArray);
} else if (source instanceof byte[]) {
final byte[] bytesSource = (byte[]) source;
if ('[' == bytesSource[0] && ']' == bytesSource[bytesSource.length - 1]) {
mapFromTokener(new JSONTokener(IoUtil.toStream(bytesSource), jsonArray.getConfig()), jsonArray);
mapFromTokener(new JSONTokener(IoUtil.toStream(bytesSource), jsonArray.config()), jsonArray);
} else {
// https://github.com/dromara/hutool/issues/2369
// 非标准的二进制流则按照普通数组对待
@ -119,7 +119,7 @@ public class JSONArrayMapper {
} else if (source instanceof Iterable<?>) {// Iterable
iter = ((Iterable<?>) source).iterator();
} else {
if(false == jsonArray.getConfig().isIgnoreError()){
if(false == jsonArray.config().isIgnoreError()){
throw new JSONException("JSONArray initial value should be a string or collection or array.");
}
// 如果用户选择跳过异常则跳过此值转换
@ -145,7 +145,7 @@ public class JSONArrayMapper {
*/
private void mapFromStr(final CharSequence source, final JSONArray jsonArray) {
if (null != source) {
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonArray.getConfig()), jsonArray);
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonArray.config()), jsonArray);
}
}

View File

@ -117,11 +117,11 @@ public class JSONObjectMapper {
// 可能为JSON字符串
mapFromStr((CharSequence) source, jsonObject);
} else if (source instanceof Reader) {
mapFromTokener(new JSONTokener((Reader) source, jsonObject.getConfig()), jsonObject);
mapFromTokener(new JSONTokener((Reader) source, jsonObject.config()), jsonObject);
} else if (source instanceof InputStream) {
mapFromTokener(new JSONTokener((InputStream) source, jsonObject.getConfig()), jsonObject);
mapFromTokener(new JSONTokener((InputStream) source, jsonObject.config()), jsonObject);
} else if (source instanceof byte[]) {
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonObject.getConfig()), jsonObject);
mapFromTokener(new JSONTokener(IoUtil.toStream((byte[]) source), jsonObject.config()), jsonObject);
} else if (source instanceof ResourceBundle) {
// ResourceBundle
mapFromResourceBundle((ResourceBundle) source, jsonObject);
@ -129,7 +129,7 @@ public class JSONObjectMapper {
// 普通Bean
mapFromBean(source, jsonObject);
} else {
if(false == jsonObject.getConfig().isIgnoreError()){
if(false == jsonObject.config().isIgnoreError()){
// 不支持对象类型转换为JSONObject
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
}
@ -167,7 +167,7 @@ public class JSONObjectMapper {
JSONXMLUtil.toJSONObject(jsonObject, jsonStr, false);
return;
}
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonObject.getConfig()), jsonObject);
mapFromTokener(new JSONTokener(StrUtil.trim(source), jsonObject.config()), jsonObject);
}
/**
@ -187,7 +187,7 @@ public class JSONObjectMapper {
* @param jsonObject {@link JSONObject}
*/
private void mapFromBean(final Object bean, final JSONObject jsonObject) {
final CopyOptions copyOptions = InternalJSONUtil.toCopyOptions(jsonObject.getConfig());
final CopyOptions copyOptions = InternalJSONUtil.toCopyOptions(jsonObject.config());
if (null != this.predicate) {
copyOptions.setFieldEditor((entry -> this.predicate.test(entry) ? entry : null));
}

View File

@ -35,7 +35,7 @@ public class JSONXMLParser {
* @throws JSONException 解析异常
*/
public static void parseJSONObject(final JSONObject jo, final String xmlStr, final boolean keepStrings) throws JSONException {
final XMLTokener x = new XMLTokener(xmlStr, jo.getConfig());
final XMLTokener x = new XMLTokener(xmlStr, jo.config());
while (x.more() && x.skipPast("<")) {
parse(x, jo, null, keepStrings);
}

View File

@ -16,7 +16,7 @@ public class JSONNullTest {
Assertions.assertNull(bodyjson.get("device_status_date"));
Assertions.assertNull(bodyjson.get("imsi"));
bodyjson.getConfig().setIgnoreNullValue(true);
bodyjson.config().setIgnoreNullValue(true);
Assertions.assertEquals("{\"act_date\":\"2021-07-23T06:23:26.000+00:00\"}", bodyjson.toString());
}

View File

@ -49,7 +49,7 @@ public class JSONObjectTest {
//noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = new JSONObject(str);
Assertions.assertEquals("{\"code\":500,\"data\":null}", jsonObject.toString());
jsonObject.getConfig().setIgnoreNullValue(true);
jsonObject.config().setIgnoreNullValue(true);
Assertions.assertEquals("{\"code\":500}", jsonObject.toString());
}

View File

@ -260,7 +260,7 @@ public class JSONUtilTest {
Assertions.assertEquals("{\"test2\":12.0}", jsonObject.toString());
// 去除多余的0
jsonObject.getConfig().setStripTrailingZeros(true);
jsonObject.config().setStripTrailingZeros(true);
Assertions.assertEquals("{\"test2\":12}", jsonObject.toString());
}