mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix code
This commit is contained in:
parent
9a753e3fd6
commit
3a54cc9445
@ -18,7 +18,6 @@ package org.dromara.hutool.json;
|
|||||||
|
|
||||||
import org.dromara.hutool.core.bean.path.BeanPath;
|
import org.dromara.hutool.core.bean.path.BeanPath;
|
||||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
|
||||||
import org.dromara.hutool.json.writer.JSONWriter;
|
import org.dromara.hutool.json.writer.JSONWriter;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -261,11 +260,7 @@ public interface JSON extends Serializable {
|
|||||||
* @param type {@link Type}
|
* @param type {@link Type}
|
||||||
* @return 实体类对象
|
* @return 实体类对象
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
default <T> T toBean(final Type type) {
|
default <T> T toBean(final Type type) {
|
||||||
if(JSON.class.isAssignableFrom(TypeUtil.getClass(type))){
|
|
||||||
return (T) this;
|
|
||||||
}
|
|
||||||
return getFactory().toBean(this, type);
|
return getFactory().toBean(this, type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ import org.dromara.hutool.json.serializer.TypeAdapter;
|
|||||||
import org.dromara.hutool.json.support.JSONNodeBeanFactory;
|
import org.dromara.hutool.json.support.JSONNodeBeanFactory;
|
||||||
import org.dromara.hutool.json.writer.JSONWriter;
|
import org.dromara.hutool.json.writer.JSONWriter;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
@ -268,16 +267,12 @@ public class JSONFactory {
|
|||||||
// region ----- parse
|
// region ----- parse
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* JSON字符串转JSONObject对象
|
* 对象转JSONObject对象
|
||||||
*
|
*
|
||||||
* @param obj Bean对象或者Map
|
* @param obj Bean对象或者Map
|
||||||
* @return JSONObject
|
* @return JSONObject
|
||||||
*/
|
*/
|
||||||
public JSONObject parseObj(Object obj) {
|
public JSONObject parseObj(final Object obj) {
|
||||||
if (obj instanceof byte[]) {
|
|
||||||
obj = new ByteArrayInputStream((byte[]) obj);
|
|
||||||
}
|
|
||||||
|
|
||||||
final JSONMapper mapper = getMapper();
|
final JSONMapper mapper = getMapper();
|
||||||
if (obj instanceof CharSequence) {
|
if (obj instanceof CharSequence) {
|
||||||
return (JSONObject) mapper.map((CharSequence) obj);
|
return (JSONObject) mapper.map((CharSequence) obj);
|
||||||
@ -298,9 +293,9 @@ public class JSONFactory {
|
|||||||
*/
|
*/
|
||||||
public JSONArray parseArray(final Object obj) {
|
public JSONArray parseArray(final Object obj) {
|
||||||
final JSONMapper mapper = getMapper();
|
final JSONMapper mapper = getMapper();
|
||||||
if (obj instanceof JSONObject) {
|
// if (obj instanceof JSONObject) {
|
||||||
return mapper.mapFromJSONObject((JSONObject) obj);
|
// return mapper.mapFromJSONObject((JSONObject) obj);
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (obj instanceof CharSequence) {
|
if (obj instanceof CharSequence) {
|
||||||
return (JSONArray) mapper.map((CharSequence) obj);
|
return (JSONArray) mapper.map((CharSequence) obj);
|
||||||
|
@ -99,7 +99,7 @@ public class JSONUtil {
|
|||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public static JSONPrimitive ofPrimitive(final Object value) {
|
public static JSONPrimitive ofPrimitive(final Object value) {
|
||||||
return ofPrimitive(value, JSONConfig.of());
|
return JSONFactory.getInstance().ofPrimitive(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -132,7 +132,7 @@ public class JSONUtil {
|
|||||||
* @return JSONObject
|
* @return JSONObject
|
||||||
*/
|
*/
|
||||||
public static JSONObject parseObj(final Object obj) {
|
public static JSONObject parseObj(final Object obj) {
|
||||||
return parseObj(obj, JSONConfig.of(), null);
|
return JSONFactory.getInstance().parseObj(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -166,7 +166,7 @@ public class JSONUtil {
|
|||||||
* @return JSONArray
|
* @return JSONArray
|
||||||
*/
|
*/
|
||||||
public static JSONArray parseArray(final Object obj) {
|
public static JSONArray parseArray(final Object obj) {
|
||||||
return parseArray(obj, null);
|
return JSONFactory.getInstance().parseArray(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -207,7 +207,7 @@ public class JSONUtil {
|
|||||||
* @return JSON
|
* @return JSON
|
||||||
*/
|
*/
|
||||||
public static JSON parse(final Object obj) {
|
public static JSON parse(final Object obj) {
|
||||||
return parse(obj, null);
|
return JSONFactory.getInstance().parse(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -314,7 +314,7 @@ public class JSONUtil {
|
|||||||
* @return JSON字符串
|
* @return JSON字符串
|
||||||
*/
|
*/
|
||||||
public static String toJsonStr(final Object obj) {
|
public static String toJsonStr(final Object obj) {
|
||||||
return toJsonStr(obj, (JSONConfig) null);
|
return parse(obj).toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -341,7 +341,7 @@ public class JSONUtil {
|
|||||||
*/
|
*/
|
||||||
public static void toJsonStr(final Object obj, final Appendable appendable) {
|
public static void toJsonStr(final Object obj, final Appendable appendable) {
|
||||||
Assert.notNull(appendable);
|
Assert.notNull(appendable);
|
||||||
final JSONFactory jsonFactory = JSONFactory.of(null, null);
|
final JSONFactory jsonFactory = JSONFactory.getInstance();
|
||||||
final JSON json = jsonFactory.parse(obj);
|
final JSON json = jsonFactory.parse(obj);
|
||||||
json.write(jsonFactory.ofWriter(appendable, 0));
|
json.write(jsonFactory.ofWriter(appendable, 0));
|
||||||
}
|
}
|
||||||
@ -389,13 +389,15 @@ public class JSONUtil {
|
|||||||
* 转为实体类对象
|
* 转为实体类对象
|
||||||
*
|
*
|
||||||
* @param <T> Bean类型
|
* @param <T> Bean类型
|
||||||
* @param json JSONObject
|
* @param obj 对象
|
||||||
* @param type 实体类对象类型
|
* @param type 实体类对象类型
|
||||||
* @return 实体类对象
|
* @return 实体类对象
|
||||||
* @since 4.3.2
|
|
||||||
*/
|
*/
|
||||||
public static <T> T toBean(final Object json, final Type type) {
|
public static <T> T toBean(final Object obj, final Type type) {
|
||||||
return toBean(json, null, type);
|
if (null == obj) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return parse(obj).toBean(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,7 +28,8 @@ public interface JSONContext {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前JSON对象<br>
|
* 获取当前JSON对象<br>
|
||||||
* 此对象为在Mapper时预定义的对象,用于指定序列化的JSON类型
|
* 此对象为在Mapper时预定义的对象,用于指定序列化的JSON类型<br>
|
||||||
|
* 未指定返回{@code null}
|
||||||
*
|
*
|
||||||
* @return JSON对象
|
* @return JSON对象
|
||||||
*/
|
*/
|
||||||
|
@ -129,13 +129,6 @@ public class JSONMapper implements Serializable {
|
|||||||
type = ((TypeReference<?>) type).getType();
|
type = ((TypeReference<?>) type).getType();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null == type || Object.class == type) {
|
|
||||||
if (json instanceof JSONPrimitive) {
|
|
||||||
return (T) ((JSONPrimitive) json).getValue();
|
|
||||||
}
|
|
||||||
return (T) json;
|
|
||||||
}
|
|
||||||
|
|
||||||
JSONDeserializer<Object> deserializer = null;
|
JSONDeserializer<Object> deserializer = null;
|
||||||
// 自定义反序列化
|
// 自定义反序列化
|
||||||
if (null != this.typeAdapterManager) {
|
if (null != this.typeAdapterManager) {
|
||||||
|
@ -37,9 +37,12 @@ public class DefaultDeserializer implements JSONDeserializer<Object> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object deserialize(final JSON json, final Type deserializeType) {
|
public Object deserialize(final JSON json, final Type deserializeType) {
|
||||||
// 当目标类型不确定时,返回原JSON
|
// 当目标类型不确定时,返回原JSON或JSONPrimitive的值
|
||||||
final Class<?> rawType = TypeUtil.getClass(deserializeType);
|
final Class<?> rawType = TypeUtil.getClass(deserializeType);
|
||||||
if (null == rawType || Object.class == rawType || rawType.isAssignableFrom(json.getClass())) {
|
if (null == rawType || Object.class == rawType || rawType.isAssignableFrom(json.getClass())) {
|
||||||
|
if (json instanceof JSONPrimitive) {
|
||||||
|
return ((JSONPrimitive) json).getValue();
|
||||||
|
}
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,22 +17,24 @@
|
|||||||
package org.dromara.hutool.json.serializer.impl;
|
package org.dromara.hutool.json.serializer.impl;
|
||||||
|
|
||||||
import org.dromara.hutool.core.convert.CompositeConverter;
|
import org.dromara.hutool.core.convert.CompositeConverter;
|
||||||
import org.dromara.hutool.core.convert.ConvertUtil;
|
|
||||||
import org.dromara.hutool.core.map.MapUtil;
|
import org.dromara.hutool.core.map.MapUtil;
|
||||||
import org.dromara.hutool.core.reflect.TypeUtil;
|
import org.dromara.hutool.core.reflect.TypeUtil;
|
||||||
|
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.JSON;
|
import org.dromara.hutool.json.JSON;
|
||||||
|
import org.dromara.hutool.json.JSONArray;
|
||||||
import org.dromara.hutool.json.JSONObject;
|
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.MatcherJSONDeserializer;
|
import org.dromara.hutool.json.serializer.MatcherJSONDeserializer;
|
||||||
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
|
import org.dromara.hutool.json.serializer.MatcherJSONSerializer;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Map类型适配器器,用于将JSON对象和Map对象互转。
|
* Map类型适配器,用于将JSON对象和Map对象互转。
|
||||||
*
|
*
|
||||||
* @author looly
|
* @author looly
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
@ -60,10 +62,24 @@ public class MapTypeAdapter implements MatcherJSONSerializer<Map<?, ?>>, Matcher
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public JSON serialize(final Map<?, ?> bean, final JSONContext context) {
|
public JSON serialize(final Map<?, ?> bean, final JSONContext context) {
|
||||||
|
// 序列化为JSONArray
|
||||||
|
if(context.getContextJson() instanceof JSONArray){
|
||||||
|
final Iterator<?> iter;
|
||||||
|
if(bean instanceof Iterator){
|
||||||
|
iter = (Iterator<?>) bean;
|
||||||
|
} else if(bean instanceof Iterable){
|
||||||
|
iter = ((Iterable<?>) bean).iterator();
|
||||||
|
} else{
|
||||||
|
iter = bean.entrySet().iterator();
|
||||||
|
}
|
||||||
|
return IterTypeAdapter.INSTANCE.serialize(bean, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map to JSONObject
|
||||||
final JSONObject result = context.getOrCreateObj();
|
final JSONObject result = context.getOrCreateObj();
|
||||||
// 注入键值对
|
// 注入键值对
|
||||||
for (final Map.Entry<?, ?> e : bean.entrySet()) {
|
for (final Map.Entry<?, ?> e : bean.entrySet()) {
|
||||||
result.putObj(ConvertUtil.toStr(e.getKey()), e.getValue());
|
result.putObj(StrUtil.toStringOrNull(e.getKey()), e.getValue());
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ public class PairDeserializer implements JSONDeserializer<Pair<?, ?>> {
|
|||||||
deserializeType = ((TypeReference<?>) deserializeType).getType();
|
deserializeType = ((TypeReference<?>) deserializeType).getType();
|
||||||
}
|
}
|
||||||
final Type leftType = TypeUtil.getTypeArgument(deserializeType, 0);
|
final Type leftType = TypeUtil.getTypeArgument(deserializeType, 0);
|
||||||
final Type rightType = TypeUtil.getTypeArgument(deserializeType, 2);
|
final Type rightType = TypeUtil.getTypeArgument(deserializeType, 1);
|
||||||
|
|
||||||
final JSONObject jsonObject = json.asJSONObject();
|
final JSONObject jsonObject = json.asJSONObject();
|
||||||
final JSON left = jsonObject.get("left");
|
final JSON left = jsonObject.get("left");
|
||||||
|
@ -48,7 +48,7 @@ public class JSONArrayTest {
|
|||||||
// JSONObject实现了Iterable接口,可以转换为JSONArray
|
// JSONObject实现了Iterable接口,可以转换为JSONArray
|
||||||
final JSONObject jsonObject = new JSONObject();
|
final JSONObject jsonObject = new JSONObject();
|
||||||
|
|
||||||
JSONArray jsonArray = JSONUtil.parseArray(jsonObject, JSONConfig.of());
|
JSONArray jsonArray = JSONUtil.parseArray(jsonObject);
|
||||||
assertEquals(new JSONArray(), jsonArray);
|
assertEquals(new JSONArray(), jsonArray);
|
||||||
|
|
||||||
jsonObject.putObj("key1", "value1");
|
jsonObject.putObj("key1", "value1");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user