This commit is contained in:
Looly 2024-09-22 11:22:18 +08:00
parent 2fdbb46259
commit 43a0201a20
7 changed files with 47 additions and 100 deletions

View File

@ -107,20 +107,8 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
} }
@Override @Override
public JSON get(final int index) { public JSON getJSON(final Integer key) {
return this.raw.get(index); return this.raw.get(key);
}
@Override
public Object getObj(final Integer index, final Object defaultValue) {
final Object value;
final JSON json = get(index);
if(json instanceof JSONPrimitive){
value = ((JSONPrimitive) json).getValue();
}else {
value = json;
}
return ObjUtil.defaultIfNull(value, defaultValue);
} }
/** /**
@ -160,39 +148,6 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
return raw.iterator(); return raw.iterator();
} }
/**
* 当此JSON列表的每个元素都是一个JSONObject时可以调用此方法返回一个Iterable便于使用foreach语法遍历
*
* @return Iterable
* @since 4.0.12
* @param <T> JSON类型
* @param type JSON类型
*/
public <T extends JSON> Iterable<T> jsonIter(final Class<T> type) {
final Iterator<JSON> iterator = iterator();
return () -> new Iterator<T>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public T next() {
return type.cast(iterator.next());
}
@Override
public void remove() {
iterator.remove();
}
};
}
@Override
@SuppressWarnings({"unchecked"})
public <T> T[] toArray(final T[] a) {
return (T[]) ArrayConverter.INSTANCE.convert(a.getClass().getComponentType(), this);
}
@Override @Override
public boolean addAll(final int index, final Collection<? extends JSON> c) { public boolean addAll(final int index, final Collection<? extends JSON> c) {
if (CollUtil.isEmpty(c)) { if (CollUtil.isEmpty(c)) {
@ -264,6 +219,12 @@ public class JSONArray extends ListWrapper<JSON> implements JSON, JSONGetter<Int
} }
@Override
@SuppressWarnings({"unchecked"})
public <T> T[] toArray(final T[] a) {
return (T[]) ArrayConverter.INSTANCE.convert(a.getClass().getComponentType(), this);
}
/** /**
* 转为Bean数组 * 转为Bean数组
* *

View File

@ -16,11 +16,8 @@
package org.dromara.hutool.json; package org.dromara.hutool.json;
import org.dromara.hutool.core.convert.CompositeConverter;
import org.dromara.hutool.core.lang.getter.TypeGetter; import org.dromara.hutool.core.lang.getter.TypeGetter;
import org.dromara.hutool.core.util.ObjUtil; import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.serializer.JSONDeserializer;
import org.dromara.hutool.json.serializer.TypeAdapterManager;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.List; import java.util.List;
@ -48,7 +45,7 @@ public interface JSONGetter<K> extends TypeGetter<K> {
* @return true 无此key或值为{@code null}返回{@code false}其它返回{@code true} * @return true 无此key或值为{@code null}返回{@code false}其它返回{@code true}
*/ */
default boolean isNull(final K key) { default boolean isNull(final K key) {
return ObjUtil.isNull(this.getObj(key)); return ObjUtil.isNull(this.getJSON(key));
} }
/** /**
@ -82,15 +79,16 @@ public interface JSONGetter<K> extends TypeGetter<K> {
* @return JSONArray对象如果值为{@code null}返回{@code null}非JSONArray类型尝试转换转换失败抛出异常 * @return JSONArray对象如果值为{@code null}返回{@code null}非JSONArray类型尝试转换转换失败抛出异常
*/ */
default JSONArray getJSONArray(final K key) { default JSONArray getJSONArray(final K key) {
final Object object = this.getObj(key); final JSON json = getJSON(key);
if (ObjUtil.isNull(object)) { if (null == json) {
return null; return null;
} }
if (object instanceof JSON) { if (json instanceof JSONObject) {
return (JSONArray) object; return JSONUtil.parseArray(json, config());
} }
return JSONUtil.parseArray(object, config());
return json.asJSONArray();
} }
/** /**
@ -101,35 +99,12 @@ public interface JSONGetter<K> extends TypeGetter<K> {
* @return JSONObject对象如果值为{@code null}返回{@code null}非JSONObject类型尝试转换转换失败抛出异常 * @return JSONObject对象如果值为{@code null}返回{@code null}非JSONObject类型尝试转换转换失败抛出异常
*/ */
default JSONObject getJSONObject(final K key) { default JSONObject getJSONObject(final K key) {
final Object obj = getObj(key); final JSON json = getJSON(key);
if(null == obj){ if (null == json) {
return null; return null;
} }
if(obj instanceof JSONObject){ return json.asJSONObject();
return (JSONObject) obj;
}
throw new JSONException("JSONObject expected, but " + obj.getClass());
}
/**
* 从JSON中直接获取Bean对象<br>
* 先获取JSONObject对象然后转为Bean对象
*
* @param <T> Bean类型
* @param key KEY
* @param beanType Bean类型
* @return Bean对象如果值为null或者非JSONObject类型返回null
* @since 3.1.1
*/
@SuppressWarnings("unchecked")
default <T> T getBean(final K key, final Class<T> beanType) {
final Object obj = getObj(key);
if(null == obj || beanType.isInstance(obj)){
return (T) obj;
}
return ((JSON)obj).toBean(beanType);
} }
/** /**
@ -147,24 +122,34 @@ public interface JSONGetter<K> extends TypeGetter<K> {
return (null == jsonArray) ? null : jsonArray.toList(beanType); return (null == jsonArray) ? null : jsonArray.toList(beanType);
} }
@SuppressWarnings("unchecked") @Override
default Object getObj(final K key, final Object defaultValue) {
final Object value;
final JSON json = getJSON(key);
if (json instanceof JSONPrimitive) {
value = ((JSONPrimitive) json).getValue();
} else {
value = json;
}
return ObjUtil.defaultIfNull(value, defaultValue);
}
@Override @Override
default <T> T get(final K key, final Type type, final T defaultValue) { default <T> T get(final K key, final Type type, final T defaultValue) {
Object value = this.getObj(key); final JSON value = getJSON(key);
if (ObjUtil.isNull(value)) { if (ObjUtil.isNull(value)) {
return defaultValue; return defaultValue;
} }
if(value instanceof JSON){ return ObjUtil.defaultIfNull(value.toBean(type), defaultValue);
final JSONDeserializer<Object> deserializer = TypeAdapterManager.getInstance().getDeserializer((JSON) value, type);
if(null == deserializer){
throw new JSONException("No deserializer for type: " + type);
}
value = deserializer.deserialize((JSON) value, type);
return null == value ? defaultValue : (T) value;
} }
// JSONPrimitive中的值 /**
return CompositeConverter.getInstance().convert(type, value, defaultValue); * 获取JSON对象<br>
} * 在JSON树模型中JSON的节点都以JSON格式存储所有get方法都基于此方法
*
* @param key KEY
* @return JSON对象
*/
JSON getJSON(final K key);
} }

View File

@ -123,10 +123,9 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGe
} }
@Override @Override
public JSON get(final Object key) { public JSON getJSON(final String key) {
return super.get(key); return get(key);
} }
// endregion // endregion
// region ----- set // region ----- set

View File

@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package org.dromara.hutool.json.serializer.impl; package org.dromara.hutool.json.convert;
import org.dromara.hutool.core.bean.copier.ValueProvider; import org.dromara.hutool.core.bean.copier.ValueProvider;
import org.dromara.hutool.json.JSON; import org.dromara.hutool.json.JSON;

View File

@ -26,6 +26,7 @@ import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.InternalJSONUtil; import org.dromara.hutool.json.InternalJSONUtil;
import org.dromara.hutool.json.JSON; import org.dromara.hutool.json.JSON;
import org.dromara.hutool.json.JSONObject; import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.convert.JSONObjectValueProvider;
import org.dromara.hutool.json.serializer.JSONContext; import org.dromara.hutool.json.serializer.JSONContext;
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer; import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
import org.dromara.hutool.json.serializer.MatcherJSONSerializer; import org.dromara.hutool.json.serializer.MatcherJSONSerializer;

View File

@ -20,6 +20,7 @@ import org.dromara.hutool.core.bean.RecordUtil;
import org.dromara.hutool.core.reflect.TypeUtil; import org.dromara.hutool.core.reflect.TypeUtil;
import org.dromara.hutool.json.JSON; import org.dromara.hutool.json.JSON;
import org.dromara.hutool.json.JSONObject; import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.convert.JSONObjectValueProvider;
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer; import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
import java.lang.reflect.Type; import java.lang.reflect.Type;

View File

@ -340,7 +340,7 @@ public class JSONArrayTest {
array.add(JSONUtil.ofObj().set("name", "ccc")); array.add(JSONUtil.ofObj().set("name", "ccc"));
final StringBuilder result = new StringBuilder(); final StringBuilder result = new StringBuilder();
array.jsonIter(JSONObject.class).forEach(result::append); array.forEach(result::append);
assertEquals("{\"name\":\"aaa\"}{\"name\":\"bbb\"}{\"name\":\"ccc\"}", result.toString()); assertEquals("{\"name\":\"aaa\"}{\"name\":\"bbb\"}{\"name\":\"ccc\"}", result.toString());
} }
} }