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]; final String segment = path[i];
JSONObject nextTarget = target.getJSONObject(segment); JSONObject nextTarget = target.getJSONObject(segment);
if (nextTarget == null) { if (nextTarget == null) {
nextTarget = new JSONObject(target.getConfig()); nextTarget = new JSONObject(target.config());
target.set(segment, nextTarget, predicate); target.set(segment, nextTarget, predicate);
} }
target = nextTarget; target = nextTarget;

View File

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

View File

@ -32,7 +32,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
* @return {@link JSONConfig} * @return {@link JSONConfig}
* @since 5.3.0 * @since 5.3.0
*/ */
JSONConfig getConfig(); JSONConfig config();
/** /**
* key对应值是否为{@code null}或无此key * key对应值是否为{@code null}或无此key
@ -83,7 +83,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
if (object instanceof JSON) { if (object instanceof JSON) {
return (JSONArray) object; 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) { if (object instanceof JSON) {
return (JSONObject) object; 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 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 // -------------------------------------------------------------------------------------------------------------------- Constructor end
@Override @Override
public JSONConfig getConfig() { public JSONConfig config() {
return this.config; return this.config;
} }
@ -215,7 +215,7 @@ public class JSONObject extends MapWrapper<String, Object> implements JSON, JSON
*/ */
@Override @Override
public Object put(final String key, final Object value) throws JSONException { 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 * @since 5.8.0
*/ */
public JSONObject set(final String key, final Object value, final Predicate<MutableEntry<String, Object>> predicate) throws JSONException { 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; return this;
} }

View File

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

View File

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

View File

@ -96,13 +96,13 @@ public class JSONArrayMapper {
// JSON字符串 // JSON字符串
mapFromStr((CharSequence) source, jsonArray); mapFromStr((CharSequence) source, jsonArray);
} else if (source instanceof Reader) { } 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) { } 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[]) { } else if (source instanceof byte[]) {
final byte[] bytesSource = (byte[]) source; final byte[] bytesSource = (byte[]) source;
if ('[' == bytesSource[0] && ']' == bytesSource[bytesSource.length - 1]) { 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 { } else {
// https://github.com/dromara/hutool/issues/2369 // https://github.com/dromara/hutool/issues/2369
// 非标准的二进制流则按照普通数组对待 // 非标准的二进制流则按照普通数组对待
@ -119,7 +119,7 @@ public class JSONArrayMapper {
} else if (source instanceof Iterable<?>) {// Iterable } else if (source instanceof Iterable<?>) {// Iterable
iter = ((Iterable<?>) source).iterator(); iter = ((Iterable<?>) source).iterator();
} else { } 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."); 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) { private void mapFromStr(final CharSequence source, final JSONArray jsonArray) {
if (null != source) { 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字符串 // 可能为JSON字符串
mapFromStr((CharSequence) source, jsonObject); mapFromStr((CharSequence) source, jsonObject);
} else if (source instanceof Reader) { } 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) { } 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[]) { } 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) { } else if (source instanceof ResourceBundle) {
// ResourceBundle // ResourceBundle
mapFromResourceBundle((ResourceBundle) source, jsonObject); mapFromResourceBundle((ResourceBundle) source, jsonObject);
@ -129,7 +129,7 @@ public class JSONObjectMapper {
// 普通Bean // 普通Bean
mapFromBean(source, jsonObject); mapFromBean(source, jsonObject);
} else { } else {
if(false == jsonObject.getConfig().isIgnoreError()){ if(false == jsonObject.config().isIgnoreError()){
// 不支持对象类型转换为JSONObject // 不支持对象类型转换为JSONObject
throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass()); throw new JSONException("Unsupported type [{}] to JSONObject!", source.getClass());
} }
@ -167,7 +167,7 @@ public class JSONObjectMapper {
JSONXMLUtil.toJSONObject(jsonObject, jsonStr, false); JSONXMLUtil.toJSONObject(jsonObject, jsonStr, false);
return; 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} * @param jsonObject {@link JSONObject}
*/ */
private void mapFromBean(final Object bean, final JSONObject 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) { if (null != this.predicate) {
copyOptions.setFieldEditor((entry -> this.predicate.test(entry) ? entry : null)); copyOptions.setFieldEditor((entry -> this.predicate.test(entry) ? entry : null));
} }

View File

@ -35,7 +35,7 @@ public class JSONXMLParser {
* @throws JSONException 解析异常 * @throws JSONException 解析异常
*/ */
public static void parseJSONObject(final JSONObject jo, final String xmlStr, final boolean keepStrings) 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("<")) { while (x.more() && x.skipPast("<")) {
parse(x, jo, null, keepStrings); 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("device_status_date"));
Assertions.assertNull(bodyjson.get("imsi")); 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()); 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 //noinspection MismatchedQueryAndUpdateOfCollection
final JSONObject jsonObject = new JSONObject(str); final JSONObject jsonObject = new JSONObject(str);
Assertions.assertEquals("{\"code\":500,\"data\":null}", jsonObject.toString()); Assertions.assertEquals("{\"code\":500,\"data\":null}", jsonObject.toString());
jsonObject.getConfig().setIgnoreNullValue(true); jsonObject.config().setIgnoreNullValue(true);
Assertions.assertEquals("{\"code\":500}", jsonObject.toString()); Assertions.assertEquals("{\"code\":500}", jsonObject.toString());
} }

View File

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