mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
fix code
This commit is contained in:
@@ -21,7 +21,6 @@ import org.dromara.hutool.core.bean.copier.CopyOptions;
|
||||
import org.dromara.hutool.core.codec.binary.HexUtil;
|
||||
import org.dromara.hutool.core.convert.ConvertUtil;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.map.CaseInsensitiveLinkedMap;
|
||||
import org.dromara.hutool.core.map.CaseInsensitiveTreeMap;
|
||||
import org.dromara.hutool.core.math.NumberUtil;
|
||||
@@ -34,7 +33,6 @@ import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import java.io.IOException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 内部JSON工具类,仅用于JSON内部使用
|
||||
@@ -93,7 +91,7 @@ public final class InternalJSONUtil {
|
||||
* 值转为String,用于JSON中。规则为:
|
||||
* <ul>
|
||||
* <li>对象如果是数组或Collection,包装为{@link JSONArray}</li>
|
||||
* <li>对象如果是Map,包装为{@link OldJSONObject}</li>
|
||||
* <li>对象如果是Map,包装为{@link JSONObject}</li>
|
||||
* <li>对象如果是数字,使用{@link NumberUtil#toStr(Number)}转换为字符串</li>
|
||||
* <li>其他情况调用toString并使用双引号包装</li>
|
||||
* </ul>
|
||||
@@ -108,11 +106,11 @@ public final class InternalJSONUtil {
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return NumberUtil.toStr((Number) value);
|
||||
} else if (value instanceof Boolean || value instanceof OldJSONObject || value instanceof JSONArray) {
|
||||
} else if (value instanceof Boolean || value instanceof JSONObject || value instanceof JSONArray) {
|
||||
return value.toString();
|
||||
} else if (value instanceof Map) {
|
||||
final Map<?, ?> map = (Map<?, ?>) value;
|
||||
return new OldJSONObject(map).toString();
|
||||
return JSONUtil.parseObj(map).toString();
|
||||
} else if (value instanceof Collection) {
|
||||
final Collection<?> coll = (Collection<?>) value;
|
||||
return new JSONArray(coll).toString();
|
||||
@@ -131,22 +129,21 @@ public final class InternalJSONUtil {
|
||||
* @param jsonObject JSONObject
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param predicate 属性过滤器,{@link Predicate#test(Object)}为{@code true}保留
|
||||
*/
|
||||
public static void propertyPut(final OldJSONObject jsonObject, final Object key, final Object value, final Predicate<MutableEntry<Object, Object>> predicate) {
|
||||
public static void propertyPut(final JSONObject jsonObject, final Object key, final Object value) {
|
||||
final String[] path = SplitUtil.splitToArray(ConvertUtil.toStr(key), StrUtil.DOT);
|
||||
final int last = path.length - 1;
|
||||
OldJSONObject target = jsonObject;
|
||||
JSONObject target = jsonObject;
|
||||
for (int i = 0; i < last; i += 1) {
|
||||
final String segment = path[i];
|
||||
OldJSONObject nextTarget = target.getJSONObject(segment);
|
||||
JSONObject nextTarget = target.getJSONObject(segment);
|
||||
if (nextTarget == null) {
|
||||
nextTarget = new OldJSONObject(target.config());
|
||||
target.set(segment, nextTarget, predicate);
|
||||
nextTarget = new JSONObject(target.config());
|
||||
target.set(segment, nextTarget);
|
||||
}
|
||||
target = nextTarget;
|
||||
}
|
||||
target.set(path[last], value, predicate);
|
||||
target.set(path[last], value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -265,36 +262,6 @@ public final class InternalJSONUtil {
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置创建对应的原始Map
|
||||
*
|
||||
* @param capacity 初始大小
|
||||
* @param config JSON配置项,{@code null}则使用默认配置
|
||||
* @return Map
|
||||
*/
|
||||
@Deprecated
|
||||
static Map<String, Object> createRawMapOld(final int capacity, JSONConfig config) {
|
||||
final Map<String, Object> rawHashMap;
|
||||
if (null == config) {
|
||||
config = JSONConfig.of();
|
||||
}
|
||||
final Comparator<String> keyComparator = config.getKeyComparator();
|
||||
if (config.isIgnoreCase()) {
|
||||
if (null != keyComparator) {
|
||||
rawHashMap = new CaseInsensitiveTreeMap<>(keyComparator);
|
||||
} else {
|
||||
rawHashMap = new CaseInsensitiveLinkedMap<>(capacity);
|
||||
}
|
||||
} else {
|
||||
if (null != keyComparator) {
|
||||
rawHashMap = new TreeMap<>(keyComparator);
|
||||
} else {
|
||||
rawHashMap = new LinkedHashMap<>(capacity);
|
||||
}
|
||||
}
|
||||
return rawHashMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据配置创建对应的原始Map
|
||||
*
|
||||
|
@@ -19,16 +19,18 @@ package org.dromara.hutool.json;
|
||||
import org.dromara.hutool.core.bean.path.BeanPath;
|
||||
import org.dromara.hutool.core.convert.ConvertException;
|
||||
import org.dromara.hutool.core.convert.Converter;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.writer.JSONWriter;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* JSON树模型接口,表示树中的一个节点。实现包括:
|
||||
* <ul>
|
||||
* <li>{@link OldJSONObject}表示键值对形式的节点</li>
|
||||
* <li>{@link JSONObject}表示键值对形式的节点</li>
|
||||
* <li>{@link JSONArray}表示列表形式的节点</li>
|
||||
* <li>{@link JSONPrimitive}表示数字、Boolean、字符串形式的节点</li>
|
||||
* <li>{@code null}表示空节点</li>
|
||||
@@ -53,6 +55,15 @@ public interface JSON extends Converter, Cloneable, Serializable {
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* 判断JSON是否为空,即大小为0
|
||||
*
|
||||
* @return 是否为空
|
||||
*/
|
||||
default boolean isEmpty() {
|
||||
return 0 == size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过表达式获取JSON中嵌套的对象<br>
|
||||
* <ol>
|
||||
@@ -152,7 +163,19 @@ public interface JSON extends Converter, Cloneable, Serializable {
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
*/
|
||||
default String toJSONString(final int indentFactor) throws JSONException {
|
||||
final JSONWriter jsonWriter = JSONWriter.of(new StringBuilder(), indentFactor, 0, config());
|
||||
return toJSONString(indentFactor, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化输出JSON字符串
|
||||
*
|
||||
* @param indentFactor 每层缩进空格数
|
||||
* @param predicate 过滤器,用于过滤不需要的键值对
|
||||
* @return JSON字符串
|
||||
* @throws JSONException 包含非法数抛出此异常
|
||||
*/
|
||||
default String toJSONString(final int indentFactor, final Predicate<MutableEntry<Object, Object>> predicate) throws JSONException {
|
||||
final JSONWriter jsonWriter = JSONWriter.of(new StringBuilder(), indentFactor, 0, config()).setPredicate(predicate);
|
||||
this.write(jsonWriter);
|
||||
return jsonWriter.toString();
|
||||
}
|
||||
@@ -161,7 +184,7 @@ public interface JSON extends Converter, Cloneable, Serializable {
|
||||
* 将JSON内容写入Writer<br>
|
||||
* Warning: This method assumes that the data structure is acyclical.
|
||||
*
|
||||
* @param writer writer
|
||||
* @param writer writer
|
||||
* @throws JSONException JSON相关异常
|
||||
*/
|
||||
void write(JSONWriter writer) throws JSONException;
|
||||
|
@@ -257,11 +257,11 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
* @return A JSONObject,无名或值返回null
|
||||
* @throws JSONException 如果任何一个名为null
|
||||
*/
|
||||
public OldJSONObject toJSONObject(final JSONArray names) throws JSONException {
|
||||
public JSONObject toJSONObject(final JSONArray names) throws JSONException {
|
||||
if (names == null || names.size() == 0 || this.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
final OldJSONObject jo = new OldJSONObject(this.config);
|
||||
final JSONObject jo = new JSONObject(this.config);
|
||||
for (int i = 0; i < names.size(); i += 1) {
|
||||
jo.set(names.getStr(i), this.getObj(i));
|
||||
}
|
||||
@@ -569,7 +569,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
|
||||
@Override
|
||||
public void write(final JSONWriter writer) throws JSONException {
|
||||
final JSONWriter copyWriter = writer.copyOf();
|
||||
final JSONWriter copyWriter = writer.copyOfSub();
|
||||
copyWriter.beginArray();
|
||||
CollUtil.forEach(this, (value, index) -> copyWriter.writeField(new MutableEntry<>(index, value)));
|
||||
copyWriter.end();
|
||||
|
@@ -92,21 +92,22 @@ public interface JSONGetter<K> extends TypeGetter<K> {
|
||||
|
||||
/**
|
||||
* 获得JSONObject对象<br>
|
||||
* 如果值为其它类型对象,尝试转换为{@link OldJSONObject}返回,否则抛出异常
|
||||
* 如果值为其它类型对象,尝试转换为{@link JSONObject}返回,否则抛出异常
|
||||
*
|
||||
* @param key KEY
|
||||
* @return JSONObject对象,如果值为{@code null},返回{@code null},非JSONObject类型,尝试转换,转换失败抛出异常
|
||||
*/
|
||||
default OldJSONObject getJSONObject(final K key) {
|
||||
final Object object = this.getObj(key);
|
||||
if (ObjUtil.isNull(object)) {
|
||||
default JSONObject getJSONObject(final K key) {
|
||||
final Object obj = getObj(key);
|
||||
if(null == obj){
|
||||
return null;
|
||||
}
|
||||
|
||||
if (object instanceof JSON) {
|
||||
return (OldJSONObject) object;
|
||||
if(obj instanceof JSONObject){
|
||||
return (JSONObject) obj;
|
||||
}
|
||||
return new OldJSONObject(object, config());
|
||||
|
||||
throw new JSONException("JSONObject expected, but " + obj.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,9 +120,13 @@ public interface JSONGetter<K> extends TypeGetter<K> {
|
||||
* @return Bean对象,如果值为null或者非JSONObject类型,返回null
|
||||
* @since 3.1.1
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
default <T> T getBean(final K key, final Class<T> beanType) {
|
||||
final OldJSONObject obj = getJSONObject(key);
|
||||
return (null == obj) ? null : obj.toBean(beanType);
|
||||
final Object obj = getObj(key);
|
||||
if(null == obj || beanType.isInstance(obj)){
|
||||
return (T) obj;
|
||||
}
|
||||
return ((JSON)obj).toBean(beanType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -16,13 +16,33 @@
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.func.LambdaInfo;
|
||||
import org.dromara.hutool.core.func.LambdaUtil;
|
||||
import org.dromara.hutool.core.func.SerFunction;
|
||||
import org.dromara.hutool.core.func.SerSupplier;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.map.MapWrapper;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.mapper.JSONValueMapper;
|
||||
import org.dromara.hutool.json.writer.JSONWriter;
|
||||
|
||||
public class JSONObject extends MapWrapper<String, JSON> implements JSON{
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* JSON对象<br>
|
||||
* 对象是 JSON 中的映射类型。他们将“键”映射到“值”。在 JSON 中,“键”必须始终是字符串。这些对中的每一组通常被称为“属性”。<br>
|
||||
* 例:
|
||||
* <pre>{@code
|
||||
* json = new JSONObject().put("JSON", "Hello, World!").toString();
|
||||
* }</pre>
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class JSONObject extends MapWrapper<String, JSON> implements JSON, JSONGetter<String>{
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 默认初始大小
|
||||
@@ -32,7 +52,11 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON{
|
||||
/**
|
||||
* 配置项
|
||||
*/
|
||||
private JSONConfig config;
|
||||
private final JSONConfig config;
|
||||
/**
|
||||
* 对象转换和包装,用于将Java对象和值转换为JSON值
|
||||
*/
|
||||
private final JSONValueMapper valueMapper;
|
||||
|
||||
/**
|
||||
* 构造,初始容量为 {@link #DEFAULT_CAPACITY},KEY有序
|
||||
@@ -60,6 +84,7 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON{
|
||||
public JSONObject(final int capacity, final JSONConfig config) {
|
||||
super(InternalJSONUtil.createRawMap(capacity, config));
|
||||
this.config = ObjUtil.defaultIfNull(config, JSONConfig::of);
|
||||
this.valueMapper = JSONValueMapper.of(this.config);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -69,8 +94,169 @@ public class JSONObject extends MapWrapper<String, JSON> implements JSON{
|
||||
|
||||
@Override
|
||||
public void write(final JSONWriter writer) throws JSONException {
|
||||
writer.beginObj();
|
||||
this.forEach((key, value) -> writer.writeField(new MutableEntry<>(key, value)));
|
||||
writer.end();
|
||||
final JSONWriter jsonWriter = writer.copyOfSub();
|
||||
jsonWriter.beginObj();
|
||||
this.forEach((key, value) -> jsonWriter.writeField(new MutableEntry<>(key, value)));
|
||||
jsonWriter.end();
|
||||
}
|
||||
|
||||
// region ----- get
|
||||
/**
|
||||
* 根据lambda的方法引用,获取
|
||||
*
|
||||
* @param func 方法引用
|
||||
* @param <P> 参数类型
|
||||
* @param <T> 返回值类型
|
||||
* @return 获取表达式对应属性和返回的对象
|
||||
*/
|
||||
public <P, T> T get(final SerFunction<P, T> func) {
|
||||
final LambdaInfo lambdaInfo = LambdaUtil.resolve(func);
|
||||
return get(lambdaInfo.getFieldName(), lambdaInfo.getReturnType());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObj(final String key, final Object defaultValue) {
|
||||
final Object value;
|
||||
final JSON json = get(key);
|
||||
if(json instanceof JSONPrimitive){
|
||||
value = ((JSONPrimitive) json).getValue();
|
||||
}else {
|
||||
value = json;
|
||||
}
|
||||
return ObjUtil.defaultIfNull(value, defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSON get(final Object key) {
|
||||
return super.get(key);
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region ----- set
|
||||
/**
|
||||
* 对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return this.
|
||||
* @throws JSONException 如果存在值非Integer, Long, Double, 或 Float.
|
||||
*/
|
||||
public JSONObject increment(final String key) throws JSONException {
|
||||
final JSON json = this.get(key);
|
||||
if(null == json){
|
||||
return set(key, 1);
|
||||
}
|
||||
|
||||
if(json instanceof JSONPrimitive){
|
||||
final JSONPrimitive jsonPrimitive = (JSONPrimitive) json;
|
||||
if(jsonPrimitive.isNumber()){
|
||||
jsonPrimitive.increment();
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
throw new JSONException("Unable to increment key: {} type: {}", key, json.getClass());
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加值.
|
||||
* <ul>
|
||||
* <li>如果键值对不存在或对应值为{@code null},则value为单独值</li>
|
||||
* <li>如果值是一个{@link JSONArray},追加之</li>
|
||||
* <li>如果值是一个其他值,则和旧值共同组合为一个{@link JSONArray}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return this.
|
||||
* @throws JSONException 如果给定键为{@code null}或者键对应的值存在且为非JSONArray
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public JSONObject append(final String key, final Object value) throws JSONException {
|
||||
final Object object = this.getObj(key);
|
||||
if (object == null) {
|
||||
this.set(key, value);
|
||||
} else if (object instanceof JSONArray) {
|
||||
((JSONArray) object).set(value);
|
||||
} else {
|
||||
this.set(key, JSONUtil.ofArray(this.config).set(object).set(value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过lambda批量设置值<br>
|
||||
* 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
|
||||
* <pre>{@code
|
||||
* User user = GenericBuilder.of(User::new).with(User::setUsername, "hutool").build();
|
||||
* (new JSONObject()).setFields(user::getNickname, user::getUsername);
|
||||
* }</pre>
|
||||
*
|
||||
* @param fields lambda,不能为空
|
||||
* @return this
|
||||
*/
|
||||
public JSONObject setFields(final SerSupplier<?>... fields) {
|
||||
Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.get()));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置所有键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param map 键值对
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject setAll(final Map<?, ?> map) {
|
||||
if(MapUtil.isNotEmpty(map)){
|
||||
for (final Entry<?, ?> entry : map.entrySet()) {
|
||||
this.set(StrUtil.toStringOrNull(entry.getKey()), entry.getValue());
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public JSONObject set(final String key, final Object value) throws JSONException {
|
||||
this.put(key, valueMapper.map(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @return 旧值
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
@Override
|
||||
public JSON put(final String key, final JSON value) throws JSONException {
|
||||
if (null == key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final boolean ignoreNullValue = this.config.isIgnoreNullValue();
|
||||
if (null == value && ignoreNullValue) {
|
||||
// 忽略值模式下如果值为空清除key
|
||||
return this.remove(key);
|
||||
} else if (this.config.isCheckDuplicate() && containsKey(key)) {
|
||||
throw new JSONException("Duplicate key \"{}\"", key);
|
||||
}
|
||||
return super.put(key, value);
|
||||
}
|
||||
// endregion
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return toJSONString(0);
|
||||
}
|
||||
}
|
||||
|
@@ -17,11 +17,14 @@
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.lang.wrapper.Wrapper;
|
||||
import org.dromara.hutool.json.writer.JSONWriter;
|
||||
import org.dromara.hutool.json.writer.ValueWriter;
|
||||
import org.dromara.hutool.json.writer.ValueWriterManager;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
/**
|
||||
* JSON原始类型数据封装,根据RFC8259规范,JSONPrimitive只包含以下类型:
|
||||
@@ -34,7 +37,7 @@ import java.lang.reflect.Type;
|
||||
* @author Looly
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public class JSONPrimitive implements JSON {
|
||||
public class JSONPrimitive implements Wrapper<Object>, JSON {
|
||||
private static final long serialVersionUID = -2026215279191790345L;
|
||||
|
||||
private Object value;
|
||||
@@ -72,6 +75,11 @@ public class JSONPrimitive implements JSON {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRaw() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置值
|
||||
*
|
||||
@@ -99,6 +107,46 @@ public class JSONPrimitive implements JSON {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为数字类型
|
||||
*
|
||||
* @return 是否为数字类型
|
||||
*/
|
||||
public boolean isNumber() {
|
||||
return value instanceof Number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自增,仅支持数字类型,包括:
|
||||
* <ul>
|
||||
* <li>{@link Integer}</li>
|
||||
* <li>{@link Long}</li>
|
||||
* <li>{@link Double}</li>
|
||||
* <li>{@link Float}</li>
|
||||
* <li>{@link BigInteger}</li>
|
||||
* <li>{@link BigDecimal}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @throws JSONException 非数字类型
|
||||
*/
|
||||
public void increment(){
|
||||
if (value instanceof BigInteger) {
|
||||
value = ((BigInteger) value).add(BigInteger.ONE);
|
||||
} else if (value instanceof BigDecimal) {
|
||||
value = ((BigDecimal) value).add(BigDecimal.ONE);
|
||||
} else if (value instanceof Integer) {
|
||||
value = (Integer) value + 1;
|
||||
} else if (value instanceof Long) {
|
||||
value = (Long) value + 1;
|
||||
} else if (value instanceof Double) {
|
||||
value = (Double) value + 1;
|
||||
} else if (value instanceof Float) {
|
||||
value = (Float) value + 1;
|
||||
} else {
|
||||
throw new JSONException("Unable to increment {} type: {}", value, value.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return 1;
|
||||
|
@@ -19,10 +19,12 @@ package org.dromara.hutool.json;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.io.file.FileUtil;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.reflect.TypeReference;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.convert.JSONConverter;
|
||||
import org.dromara.hutool.json.mapper.JSONObjectMapper;
|
||||
import org.dromara.hutool.json.writer.JSONWriter;
|
||||
import org.dromara.hutool.json.writer.ValueWriter;
|
||||
import org.dromara.hutool.json.writer.ValueWriterManager;
|
||||
@@ -34,6 +36,7 @@ import java.io.Writer;
|
||||
import java.lang.reflect.Type;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* JSON工具类
|
||||
@@ -42,15 +45,14 @@ import java.util.List;
|
||||
*/
|
||||
public class JSONUtil {
|
||||
|
||||
// -------------------------------------------------------------------- Pause start
|
||||
|
||||
// region ----- of
|
||||
/**
|
||||
* 创建JSONObject
|
||||
*
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static OldJSONObject ofObj() {
|
||||
return new OldJSONObject();
|
||||
public static JSONObject ofObj() {
|
||||
return new JSONObject();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,8 +62,8 @@ public class JSONUtil {
|
||||
* @return JSONObject
|
||||
* @since 5.2.5
|
||||
*/
|
||||
public static OldJSONObject ofObj(final JSONConfig config) {
|
||||
return new OldJSONObject(config);
|
||||
public static JSONObject ofObj(final JSONConfig config) {
|
||||
return new JSONObject(config);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +85,9 @@ public class JSONUtil {
|
||||
public static JSONArray ofArray(final JSONConfig config) {
|
||||
return new JSONArray(config);
|
||||
}
|
||||
// endregion
|
||||
|
||||
// region ----- parse
|
||||
/**
|
||||
* JSON字符串转JSONObject对象<br>
|
||||
* 此方法会忽略空值,但是对JSON字符串不影响
|
||||
@@ -91,21 +95,8 @@ public class JSONUtil {
|
||||
* @param obj Bean对象或者Map
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static OldJSONObject parseObj(final Object obj) {
|
||||
return new OldJSONObject(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转JSONObject对象<br>
|
||||
* 此方法会忽略空值,但是对JSON字符串不影响
|
||||
*
|
||||
* @param obj Bean对象或者Map
|
||||
* @param config JSON配置
|
||||
* @return JSONObject
|
||||
* @since 5.3.1
|
||||
*/
|
||||
public static OldJSONObject parseObj(final Object obj, final JSONConfig config) {
|
||||
return new OldJSONObject(obj, config);
|
||||
public static JSONObject parseObj(final Object obj) {
|
||||
return parseObj(obj, JSONConfig.of(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -116,8 +107,35 @@ public class JSONUtil {
|
||||
* @return JSONObject
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static OldJSONObject parseObj(final Object obj, final boolean ignoreNullValue) {
|
||||
return new OldJSONObject(obj, JSONConfig.of().setIgnoreNullValue(ignoreNullValue));
|
||||
public static JSONObject parseObj(final Object obj, final boolean ignoreNullValue) {
|
||||
return parseObj(obj, JSONConfig.of().setIgnoreNullValue(ignoreNullValue));
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转JSONObject对象<br>
|
||||
* 此方法会忽略空值,但是对JSON字符串不影响
|
||||
*
|
||||
* @param obj Bean对象或者Map
|
||||
* @param config JSON配置
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static JSONObject parseObj(final Object obj, final JSONConfig config) {
|
||||
return parseObj(obj, config, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON字符串转JSONObject对象<br>
|
||||
* 此方法会忽略空值,但是对JSON字符串不影响
|
||||
*
|
||||
* @param obj Bean对象或者Map
|
||||
* @param config JSON配置
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static JSONObject parseObj(final Object obj, final JSONConfig config, final Predicate<MutableEntry<Object, Object>> predicate) {
|
||||
final JSONObject jsonObject = new JSONObject(config);
|
||||
JSONObjectMapper.of(obj, predicate).mapTo(jsonObject);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,10 +203,10 @@ public class JSONUtil {
|
||||
* @param xmlStr XML字符串
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static OldJSONObject parseFromXml(final String xmlStr) {
|
||||
public static JSONObject parseFromXml(final String xmlStr) {
|
||||
return JSONXMLUtil.toJSONObject(xmlStr);
|
||||
}
|
||||
// -------------------------------------------------------------------- Parse end
|
||||
// endregion
|
||||
|
||||
// -------------------------------------------------------------------- Read start
|
||||
|
||||
@@ -212,7 +230,7 @@ public class JSONUtil {
|
||||
* @return JSONObject
|
||||
* @throws IORuntimeException IO异常
|
||||
*/
|
||||
public static OldJSONObject readJSONObject(final File file, final Charset charset) throws IORuntimeException {
|
||||
public static JSONObject readJSONObject(final File file, final Charset charset) throws IORuntimeException {
|
||||
return FileUtil.read(file, charset, JSONUtil::parseObj);
|
||||
}
|
||||
|
||||
@@ -307,12 +325,13 @@ public class JSONUtil {
|
||||
* @return JSONObject
|
||||
* @since 4.0.8
|
||||
*/
|
||||
public static OldJSONObject xmlToJson(final String xml) {
|
||||
public static JSONObject xmlToJson(final String xml) {
|
||||
return JSONXMLUtil.toJSONObject(xml);
|
||||
}
|
||||
// -------------------------------------------------------------------- toString end
|
||||
|
||||
// -------------------------------------------------------------------- toBean start
|
||||
|
||||
/**
|
||||
* 转为实体类对象
|
||||
*
|
||||
@@ -324,7 +343,7 @@ public class JSONUtil {
|
||||
*/
|
||||
public static <T> T toBean(final Object json, final Class<T> clazz) {
|
||||
Assert.notNull(clazz);
|
||||
return toBean(json, (Type)clazz);
|
||||
return toBean(json, (Type) clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -358,7 +377,7 @@ public class JSONUtil {
|
||||
* 转为实体类对象
|
||||
*
|
||||
* @param <T> Bean类型
|
||||
* @param obj JSONObject
|
||||
* @param obj JSONObject
|
||||
* @param config JSON配置
|
||||
* @param type 实体类对象类型
|
||||
* @return 实体类对象
|
||||
@@ -479,8 +498,7 @@ public class JSONUtil {
|
||||
* JSON对象是否为空,以下情况返回true<br>
|
||||
* <ul>
|
||||
* <li>null</li>
|
||||
* <li>{@link JSONArray#isEmpty()}</li>
|
||||
* <li>{@link OldJSONObject#isEmpty()}</li>
|
||||
* <li>{@link JSON#isEmpty()}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param json JSONObject或JSONArray
|
||||
@@ -490,12 +508,7 @@ public class JSONUtil {
|
||||
if (null == json) {
|
||||
return true;
|
||||
}
|
||||
if (json instanceof OldJSONObject) {
|
||||
return ((OldJSONObject) json).isEmpty();
|
||||
} else if (json instanceof JSONArray) {
|
||||
return ((JSONArray) json).isEmpty();
|
||||
}
|
||||
return false;
|
||||
return json.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -1,496 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2013-2024 Hutool Team and hutool.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.dromara.hutool.json;
|
||||
|
||||
import org.dromara.hutool.core.collection.CollUtil;
|
||||
import org.dromara.hutool.core.func.LambdaInfo;
|
||||
import org.dromara.hutool.core.func.LambdaUtil;
|
||||
import org.dromara.hutool.core.func.SerFunction;
|
||||
import org.dromara.hutool.core.func.SerSupplier;
|
||||
import org.dromara.hutool.core.lang.mutable.MutableEntry;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.core.map.MapWrapper;
|
||||
import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.mapper.JSONObjectMapper;
|
||||
import org.dromara.hutool.json.mapper.JSONValueMapper;
|
||||
import org.dromara.hutool.json.writer.JSONWriter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* JSON对象<br>
|
||||
* 例:<br>
|
||||
*
|
||||
* <pre>
|
||||
* json = new JSONObject().put("JSON", "Hello, World!").toString();
|
||||
* </pre>
|
||||
*
|
||||
* @author looly
|
||||
*/
|
||||
public class OldJSONObject extends MapWrapper<String, Object> implements JSON, JSONGetter<String> {
|
||||
private static final long serialVersionUID = -330220388580734346L;
|
||||
|
||||
/**
|
||||
* 默认初始大小
|
||||
*/
|
||||
public static final int DEFAULT_CAPACITY = MapUtil.DEFAULT_INITIAL_CAPACITY;
|
||||
|
||||
/**
|
||||
* 配置项
|
||||
*/
|
||||
private JSONConfig config;
|
||||
/**
|
||||
* 对象转换和包装,用于将Java对象和值转换为JSON值
|
||||
*/
|
||||
private JSONValueMapper valueMapper;
|
||||
|
||||
// -------------------------------------------------------------------------------------------------------------------- Constructor start
|
||||
|
||||
/**
|
||||
* 构造,初始容量为 {@link #DEFAULT_CAPACITY},KEY有序
|
||||
*/
|
||||
public OldJSONObject() {
|
||||
this(JSONConfig.of());
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param config JSON配置项
|
||||
* @since 4.6.5
|
||||
*/
|
||||
public OldJSONObject(final JSONConfig config) {
|
||||
this(DEFAULT_CAPACITY, config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造
|
||||
*
|
||||
* @param capacity 初始大小
|
||||
* @param config JSON配置项,{@code null}则使用默认配置
|
||||
* @since 4.1.19
|
||||
*/
|
||||
public OldJSONObject(final int capacity, final JSONConfig config) {
|
||||
super(InternalJSONUtil.createRawMapOld(capacity, ObjUtil.defaultIfNull(config, JSONConfig.of())));
|
||||
this.config = ObjUtil.defaultIfNull(config, JSONConfig.of());
|
||||
this.valueMapper = JSONValueMapper.of(this.config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建JSONObject,JavaBean默认忽略null值,其它对象不忽略,规则如下:
|
||||
* <ol>
|
||||
* <li>value为Map,将键值对加入JSON对象</li>
|
||||
* <li>value为JSON字符串(CharSequence),使用JSONTokener解析</li>
|
||||
* <li>value为JSONTokener,直接解析</li>
|
||||
* <li>value为普通JavaBean,如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。
|
||||
* 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param source JavaBean或者Map对象或者String
|
||||
*/
|
||||
public OldJSONObject(final Object source) {
|
||||
this(source, JSONConfig.of().setIgnoreNullValue(InternalJSONUtil.defaultIgnoreNullValue(source)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建JSONObject,规则如下:
|
||||
* <ol>
|
||||
* <li>value为Map,将键值对加入JSON对象</li>
|
||||
* <li>value为JSON字符串(CharSequence),使用JSONTokener解析</li>
|
||||
* <li>value为JSONTokener,直接解析</li>
|
||||
* <li>value为普通JavaBean,如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
||||
* </ol>
|
||||
* <p>
|
||||
* 如果给定值为Map,将键值对加入JSON对象;<br>
|
||||
* 如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象<br>
|
||||
* 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"
|
||||
*
|
||||
* @param source JavaBean或者Map对象或者String
|
||||
* @param config JSON配置文件,{@code null}则使用默认配置
|
||||
* @since 4.2.2
|
||||
*/
|
||||
public OldJSONObject(final Object source, final JSONConfig config) {
|
||||
this(source, config, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建JSONObject,规则如下:
|
||||
* <ol>
|
||||
* <li>value为Map,将键值对加入JSON对象</li>
|
||||
* <li>value为JSON字符串(CharSequence),使用JSONTokener解析</li>
|
||||
* <li>value为JSONTokener,直接解析</li>
|
||||
* <li>value为普通JavaBean,如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象。例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"</li>
|
||||
* </ol>
|
||||
* <p>
|
||||
* 如果给定值为Map,将键值对加入JSON对象;<br>
|
||||
* 如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象<br>
|
||||
* 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"
|
||||
*
|
||||
* @param source JavaBean或者Map对象或者String
|
||||
* @param config JSON配置文件,{@code null}则使用默认配置
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public OldJSONObject(final Object source, final JSONConfig config, final Predicate<MutableEntry<Object, Object>> predicate) {
|
||||
this(DEFAULT_CAPACITY, config);
|
||||
JSONObjectMapper.of(source, predicate).mapTo(this);
|
||||
}
|
||||
// -------------------------------------------------------------------------------------------------------------------- Constructor end
|
||||
|
||||
@Override
|
||||
public JSONConfig config() {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置转为字符串时的日期格式,默认为时间戳(null值)<br>
|
||||
* 此方法设置的日期格式仅对转换为JSON字符串有效,对解析JSON为bean无效。
|
||||
*
|
||||
* @param format 格式,null表示使用时间戳
|
||||
* @return this
|
||||
* @since 4.1.19
|
||||
*/
|
||||
public OldJSONObject setDateFormat(final String format) {
|
||||
this.config.setDateFormat(format);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将指定KEY列表的值组成新的JSONArray
|
||||
*
|
||||
* @param names KEY列表
|
||||
* @return A JSONArray of values.
|
||||
* @throws JSONException If any of the values are non-finite numbers.
|
||||
*/
|
||||
public JSONArray toJSONArray(final Collection<String> names) throws JSONException {
|
||||
if (CollUtil.isEmpty(names)) {
|
||||
return null;
|
||||
}
|
||||
final JSONArray ja = new JSONArray(this.config);
|
||||
Object value;
|
||||
for (final String name : names) {
|
||||
value = this.get(name);
|
||||
if (null != value) {
|
||||
ja.set(value);
|
||||
}
|
||||
}
|
||||
return ja;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getObj(final String key, final Object defaultValue) {
|
||||
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);
|
||||
if(value instanceof JSONPrimitive){
|
||||
value = ((JSONPrimitive) value).getValue();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据lambda的方法引用,获取
|
||||
*
|
||||
* @param func 方法引用
|
||||
* @param <P> 参数类型
|
||||
* @param <T> 返回值类型
|
||||
* @return 获取表达式对应属性和返回的对象
|
||||
*/
|
||||
public <P, T> T get(final SerFunction<P, T> func) {
|
||||
final LambdaInfo lambdaInfo = LambdaUtil.resolve(func);
|
||||
return get(lambdaInfo.getFieldName(), lambdaInfo.getReturnType());
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT 键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
@Override
|
||||
public Object put(final String key, final Object value) throws JSONException {
|
||||
return put(key, value, null, config().isCheckDuplicate());
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
*/
|
||||
public OldJSONObject set(final String key, final Object value) throws JSONException {
|
||||
set(key, value, null);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过lambda批量设置值<br>
|
||||
* 实际使用时,可以使用getXXX的方法引用来完成键值对的赋值:
|
||||
* <pre>
|
||||
* User user = GenericBuilder.of(User::new).with(User::setUsername, "hutool").build();
|
||||
* (new JSONObject()).setFields(user::getNickname, user::getUsername);
|
||||
* </pre>
|
||||
*
|
||||
* @param fields lambda,不能为空
|
||||
* @return this
|
||||
*/
|
||||
public OldJSONObject setFields(final SerSupplier<?>... fields) {
|
||||
Arrays.stream(fields).forEach(f -> set(LambdaUtil.getFieldName(f), f.get()));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 一次性Put 键值对,如果key已经存在抛出异常,如果键值中有null值,忽略
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return this
|
||||
* @throws JSONException 值是无穷数字、键重复抛出异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public OldJSONObject set(final String key, final Object value, final Predicate<MutableEntry<Object, Object>> predicate) throws JSONException {
|
||||
put(key, value, predicate, config().isCheckDuplicate());
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @param checkDuplicate 是否检查重复键,如果为{@code true},则出现重复键时抛出{@link JSONException}异常
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
public OldJSONObject set(final String key, final Object value, final Predicate<MutableEntry<Object, Object>> predicate, final boolean checkDuplicate) throws JSONException {
|
||||
put(key, value, predicate, checkDuplicate);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在键和值都为非空的情况下put到JSONObject中
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象,可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @return this.
|
||||
* @throws JSONException 值是无穷数字
|
||||
*/
|
||||
public OldJSONObject setOpt(final String key, final Object value) throws JSONException {
|
||||
if (key != null && value != null) {
|
||||
this.set(key, value);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(final Map<? extends String, ?> m) {
|
||||
for (final Entry<? extends String, ?> entry : m.entrySet()) {
|
||||
this.set(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加值.
|
||||
* <ul>
|
||||
* <li>如果键值对不存在或对应值为{@code null},则value为单独值</li>
|
||||
* <li>如果值是一个{@link JSONArray},追加之</li>
|
||||
* <li>如果值是一个其他值,则和旧值共同组合为一个{@link JSONArray}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @return this.
|
||||
* @throws JSONException 如果给定键为{@code null}或者键对应的值存在且为非JSONArray
|
||||
*/
|
||||
public OldJSONObject append(final String key, final Object value) throws JSONException {
|
||||
return append(key, value, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 追加值.
|
||||
* <ul>
|
||||
* <li>如果键值对不存在或对应值为{@code null},则value为单独值</li>
|
||||
* <li>如果值是一个{@link JSONArray},追加之</li>
|
||||
* <li>如果值是一个其他值,则和旧值共同组合为一个{@link JSONArray}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return this.
|
||||
* @throws JSONException 如果给定键为{@code null}或者键对应的值存在且为非JSONArray
|
||||
* @since 6.0.0
|
||||
*/
|
||||
public OldJSONObject append(String key, Object value, final Predicate<MutableEntry<Object, Object>> predicate) throws JSONException {
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
if (null != predicate) {
|
||||
final MutableEntry<Object, Object> pair = new MutableEntry<>(key, value);
|
||||
if (predicate.test(pair)) {
|
||||
// 使用修改后的键值对
|
||||
key = (String) pair.getKey();
|
||||
value = pair.getValue();
|
||||
} else {
|
||||
// 键值对被过滤
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
final Object object = this.getObj(key);
|
||||
if (object == null) {
|
||||
this.set(key, value);
|
||||
} else if (object instanceof JSONArray) {
|
||||
((JSONArray) object).set(value);
|
||||
} else {
|
||||
this.set(key, JSONUtil.ofArray(this.config).set(object).set(value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 对值加一,如果值不存在,赋值1,如果为数字类型,做加一操作
|
||||
*
|
||||
* @param key A key string.
|
||||
* @return this.
|
||||
* @throws JSONException 如果存在值非Integer, Long, Double, 或 Float.
|
||||
*/
|
||||
public OldJSONObject increment(final String key) throws JSONException {
|
||||
final Object value = this.getObj(key);
|
||||
if (value == null) {
|
||||
this.set(key, 1);
|
||||
} else if (value instanceof BigInteger) {
|
||||
this.set(key, ((BigInteger) value).add(BigInteger.ONE));
|
||||
} else if (value instanceof BigDecimal) {
|
||||
this.set(key, ((BigDecimal) value).add(BigDecimal.ONE));
|
||||
} else if (value instanceof Integer) {
|
||||
this.set(key, (Integer) value + 1);
|
||||
} else if (value instanceof Long) {
|
||||
this.set(key, (Long) value + 1);
|
||||
} else if (value instanceof Double) {
|
||||
this.set(key, (Double) value + 1);
|
||||
} else if (value instanceof Float) {
|
||||
this.set(key, (Float) value + 1);
|
||||
} else {
|
||||
throw new JSONException("Unable to increment [" + InternalJSONUtil.quote(key) + "].");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回JSON字符串<br>
|
||||
* 如果解析错误,返回{@code null}
|
||||
*
|
||||
* @return JSON字符串
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.toJSONString(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回JSON字符串<br>
|
||||
* 支持过滤器,即选择哪些字段或值不写出
|
||||
*
|
||||
* @param indentFactor 每层缩进空格数
|
||||
* @param predicate 过滤器,同时可以修改编辑键和值,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @return JSON字符串
|
||||
* @since 5.7.15
|
||||
*/
|
||||
public String toJSONString(final int indentFactor, final Predicate<MutableEntry<Object, Object>> predicate) {
|
||||
final JSONWriter jsonWriter = JSONWriter.of(new StringBuilder(), indentFactor, 0, config).setPredicate(predicate);
|
||||
write(jsonWriter);
|
||||
return jsonWriter.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(final JSONWriter writer) throws JSONException {
|
||||
final JSONWriter copyWriter = writer.copyOf();
|
||||
copyWriter.beginObj();
|
||||
this.forEach((key, value) -> copyWriter.writeField(new MutableEntry<>(key, value)));
|
||||
copyWriter.end();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OldJSONObject clone() throws CloneNotSupportedException {
|
||||
final OldJSONObject clone = (OldJSONObject) super.clone();
|
||||
clone.config = this.config;
|
||||
clone.valueMapper = this.valueMapper;
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置键值对到JSONObject中,在忽略null模式下,如果值为{@code null},将此键移除
|
||||
*
|
||||
* @param key 键
|
||||
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
|
||||
* @param predicate 键值对过滤编辑器,可以通过实现此接口,完成解析前对键值对的过滤和修改操作,{@code null}表示不过滤,{@link Predicate#test(Object)}为{@code true}保留
|
||||
* @param checkDuplicate 是否检查重复键,如果为{@code true},则出现重复键时抛出{@link JSONException}异常
|
||||
* @return 旧值
|
||||
* @throws JSONException 值是无穷数字抛出此异常
|
||||
* @since 5.8.0
|
||||
*/
|
||||
private Object put(String key, Object value, final Predicate<MutableEntry<Object, Object>> predicate, final boolean checkDuplicate) throws JSONException {
|
||||
if (null == key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 添加前置过滤,通过MutablePair实现过滤、修改键值对等
|
||||
if (null != predicate) {
|
||||
final MutableEntry<Object, Object> pair = new MutableEntry<>(key, value);
|
||||
if (predicate.test(pair)) {
|
||||
// 使用修改后的键值对
|
||||
key = (String) pair.getKey();
|
||||
value = pair.getValue();
|
||||
} else {
|
||||
// 键值对被过滤
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
final boolean ignoreNullValue = this.config.isIgnoreNullValue();
|
||||
if (null == value && ignoreNullValue) {
|
||||
// 忽略值模式下如果值为空清除key
|
||||
return this.remove(key);
|
||||
} else if (checkDuplicate && containsKey(key)) {
|
||||
throw new JSONException("Duplicate key \"{}\"", key);
|
||||
}
|
||||
return super.put(key, this.valueMapper.map(value));
|
||||
}
|
||||
}
|
@@ -33,7 +33,10 @@ import org.dromara.hutool.core.util.ObjUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.reader.JSONParser;
|
||||
import org.dromara.hutool.json.reader.JSONTokener;
|
||||
import org.dromara.hutool.json.serializer.*;
|
||||
import org.dromara.hutool.json.serializer.JSONDeserializer;
|
||||
import org.dromara.hutool.json.serializer.JSONSerializer;
|
||||
import org.dromara.hutool.json.serializer.SerializerManager;
|
||||
import org.dromara.hutool.json.serializer.SimpleJSONContext;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
@@ -69,7 +72,7 @@ public class JSONConverter implements Converter, Serializable {
|
||||
public static JSONConverter of(final JSONConfig config) {
|
||||
final JSONConverter jsonConverter = new JSONConverter(config);
|
||||
jsonConverter.registerConverter = new RegisterConverter(jsonConverter)
|
||||
.register(OldJSONObject.class, INSTANCE)
|
||||
.register(JSONObject.class, INSTANCE)
|
||||
.register(JSONArray.class, INSTANCE)
|
||||
.register(JSONPrimitive.class, INSTANCE);
|
||||
jsonConverter.specialConverter = new SpecialConverter(jsonConverter);
|
||||
@@ -90,7 +93,7 @@ public class JSONConverter implements Converter, Serializable {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object convert(Type targetType, Object value) throws ConvertException {
|
||||
public Object convert(Type targetType, final Object value) throws ConvertException {
|
||||
if (null == value) {
|
||||
return null;
|
||||
}
|
||||
@@ -169,11 +172,11 @@ public class JSONConverter implements Converter, Serializable {
|
||||
return toJSON((CharSequence) obj);
|
||||
} else if (obj instanceof MapWrapper) {
|
||||
// MapWrapper实现了Iterable会被当作JSONArray,此处做修正
|
||||
json = new OldJSONObject(obj, config);
|
||||
json = JSONUtil.parseObj(obj, config);
|
||||
} else if (obj instanceof Iterable || obj instanceof Iterator || ArrayUtil.isArray(obj)) {// 列表
|
||||
json = new JSONArray(obj, config);
|
||||
json = JSONUtil.parseArray(obj, config);
|
||||
} else {// 对象
|
||||
json = new OldJSONObject(obj, config);
|
||||
json = JSONUtil.parseObj(obj, config);
|
||||
}
|
||||
|
||||
return json;
|
||||
|
@@ -21,7 +21,7 @@ import org.dromara.hutool.core.date.format.GlobalCustomFormat;
|
||||
import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.map.MapUtil;
|
||||
import org.dromara.hutool.json.JSONConfig;
|
||||
import org.dromara.hutool.json.OldJSONObject;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.JSONUtil;
|
||||
|
||||
import java.io.Serializable;
|
||||
@@ -40,7 +40,7 @@ public class Claims implements Serializable {
|
||||
// 时间使用秒级时间戳表示
|
||||
private final JSONConfig CONFIG = JSONConfig.of().setDateFormat(GlobalCustomFormat.FORMAT_SECONDS);
|
||||
|
||||
private OldJSONObject claimJSON;
|
||||
private JSONObject claimJSON;
|
||||
|
||||
/**
|
||||
* 增加Claims属性,如果属性值为{@code null},则移除这个属性
|
||||
@@ -86,7 +86,7 @@ public class Claims implements Serializable {
|
||||
*
|
||||
* @return JSON字符串
|
||||
*/
|
||||
public OldJSONObject getClaimsJson() {
|
||||
public JSONObject getClaimsJson() {
|
||||
init();
|
||||
return this.claimJSON;
|
||||
}
|
||||
@@ -109,7 +109,7 @@ public class Claims implements Serializable {
|
||||
|
||||
private void init(){
|
||||
if(null == this.claimJSON){
|
||||
this.claimJSON = new OldJSONObject(CONFIG);
|
||||
this.claimJSON = new JSONObject(CONFIG);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -24,7 +24,7 @@ import org.dromara.hutool.core.lang.Assert;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.text.split.SplitUtil;
|
||||
import org.dromara.hutool.core.util.CharsetUtil;
|
||||
import org.dromara.hutool.json.OldJSONObject;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
import org.dromara.hutool.json.jwt.signers.AlgorithmUtil;
|
||||
import org.dromara.hutool.json.jwt.signers.JWTSigner;
|
||||
import org.dromara.hutool.json.jwt.signers.JWTSignerUtil;
|
||||
@@ -203,7 +203,7 @@ public class JWT implements RegisteredPayload<JWT> {
|
||||
*
|
||||
* @return 头信息
|
||||
*/
|
||||
public OldJSONObject getHeaders() {
|
||||
public JSONObject getHeaders() {
|
||||
return this.header.getClaimsJson();
|
||||
}
|
||||
|
||||
@@ -265,7 +265,7 @@ public class JWT implements RegisteredPayload<JWT> {
|
||||
*
|
||||
* @return 载荷信息
|
||||
*/
|
||||
public OldJSONObject getPayloads() {
|
||||
public JSONObject getPayloads() {
|
||||
return this.payload.getClaimsJson();
|
||||
}
|
||||
|
||||
|
@@ -33,7 +33,7 @@ public class JWTUtil {
|
||||
* @param key HS256(HmacSHA256)密钥
|
||||
* @return JWT Token
|
||||
*/
|
||||
public static String createToken(final Map<String, Object> payload, final byte[] key) {
|
||||
public static String createToken(final Map<String, ?> payload, final byte[] key) {
|
||||
return createToken(MapUtil.of(JWTHeader.TYPE, "JWT"), payload, key);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ public class JWTUtil {
|
||||
* @param key HS256(HmacSHA256)密钥
|
||||
* @return JWT Token
|
||||
*/
|
||||
public static String createToken(final Map<String, Object> headers, final Map<String, Object> payload, final byte[] key) {
|
||||
public static String createToken(final Map<String, ?> headers, final Map<String, ?> payload, final byte[] key) {
|
||||
return JWT.of()
|
||||
.addHeaders(headers)
|
||||
.addPayloads(payload)
|
||||
|
@@ -85,12 +85,12 @@ public class JSONObjectMapper {
|
||||
}
|
||||
|
||||
/**
|
||||
* 将给定对象转换为{@link OldJSONObject}
|
||||
* 将给定对象转换为{@link JSONObject}
|
||||
*
|
||||
* @param jsonObject 目标{@link OldJSONObject}
|
||||
* @param jsonObject 目标{@link JSONObject}
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void mapTo(final OldJSONObject jsonObject) {
|
||||
public void mapTo(final JSONObject jsonObject) {
|
||||
final Object source = this.source;
|
||||
if (null == source) {
|
||||
return;
|
||||
@@ -117,11 +117,11 @@ public class JSONObjectMapper {
|
||||
} else if (source instanceof Map) {
|
||||
// Map
|
||||
for (final Map.Entry<?, ?> e : ((Map<?, ?>) source).entrySet()) {
|
||||
jsonObject.set(ConvertUtil.toStr(e.getKey()), e.getValue(), predicate, false);
|
||||
jsonObject.set(ConvertUtil.toStr(e.getKey()), e.getValue());
|
||||
}
|
||||
} else if (source instanceof Map.Entry) {
|
||||
final Map.Entry entry = (Map.Entry) source;
|
||||
jsonObject.set(ConvertUtil.toStr(entry.getKey()), entry.getValue(), predicate, false);
|
||||
jsonObject.set(ConvertUtil.toStr(entry.getKey()), entry.getValue());
|
||||
} else if (source instanceof CharSequence) {
|
||||
// 可能为JSON字符串
|
||||
mapFromStr((CharSequence) source, jsonObject);
|
||||
@@ -153,15 +153,14 @@ public class JSONObjectMapper {
|
||||
* 从{@link ResourceBundle}转换
|
||||
*
|
||||
* @param bundle ResourceBundle
|
||||
* @param jsonObject {@link OldJSONObject}
|
||||
* @since 5.3.1
|
||||
* @param jsonObject {@link JSONObject}
|
||||
*/
|
||||
private void mapFromResourceBundle(final ResourceBundle bundle, final OldJSONObject jsonObject) {
|
||||
private void mapFromResourceBundle(final ResourceBundle bundle, final JSONObject jsonObject) {
|
||||
final Enumeration<String> keys = bundle.getKeys();
|
||||
while (keys.hasMoreElements()) {
|
||||
final String key = keys.nextElement();
|
||||
if (key != null) {
|
||||
InternalJSONUtil.propertyPut(jsonObject, key, bundle.getString(key), this.predicate);
|
||||
InternalJSONUtil.propertyPut(jsonObject, key, bundle.getString(key));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,9 +169,9 @@ public class JSONObjectMapper {
|
||||
* 从字符串转换
|
||||
*
|
||||
* @param source JSON字符串
|
||||
* @param jsonObject {@link OldJSONObject}
|
||||
* @param jsonObject {@link JSONObject}
|
||||
*/
|
||||
private void mapFromStr(final CharSequence source, final OldJSONObject jsonObject) {
|
||||
private void mapFromStr(final CharSequence source, final JSONObject jsonObject) {
|
||||
final String jsonStr = StrUtil.trim(source);
|
||||
if (StrUtil.startWith(jsonStr, '<')) {
|
||||
// 可能为XML
|
||||
@@ -188,9 +187,9 @@ public class JSONObjectMapper {
|
||||
*
|
||||
* @param x JSONTokener
|
||||
* @param config JSON配置
|
||||
* @param jsonObject {@link OldJSONObject}
|
||||
* @param jsonObject {@link JSONObject}
|
||||
*/
|
||||
private void mapFromTokener(final JSONTokener x, final JSONConfig config, final OldJSONObject jsonObject) {
|
||||
private void mapFromTokener(final JSONTokener x, final JSONConfig config, final JSONObject jsonObject) {
|
||||
JSONParser.of(x, config).setPredicate(this.predicate).parseTo(jsonObject);
|
||||
}
|
||||
|
||||
@@ -198,9 +197,9 @@ public class JSONObjectMapper {
|
||||
* 从Record转换
|
||||
*
|
||||
* @param record Record对象
|
||||
* @param jsonObject {@link OldJSONObject}
|
||||
* @param jsonObject {@link JSONObject}
|
||||
*/
|
||||
private void mapFromRecord(final Object record, final OldJSONObject jsonObject) {
|
||||
private void mapFromRecord(final Object record, final JSONObject jsonObject) {
|
||||
final Map.Entry<String, Type>[] components = RecordUtil.getRecordComponents(record.getClass());
|
||||
|
||||
String key;
|
||||
@@ -214,9 +213,9 @@ public class JSONObjectMapper {
|
||||
* 从Bean转换
|
||||
*
|
||||
* @param bean Bean对象
|
||||
* @param jsonObject {@link OldJSONObject}
|
||||
* @param jsonObject {@link JSONObject}
|
||||
*/
|
||||
private void mapFromBean(final Object bean, final OldJSONObject jsonObject) {
|
||||
private void mapFromBean(final Object bean, final JSONObject jsonObject) {
|
||||
final CopyOptions copyOptions = InternalJSONUtil.toCopyOptions(jsonObject.config());
|
||||
if (null != this.predicate) {
|
||||
copyOptions.setFieldEditor((entry -> this.predicate.test(
|
||||
|
@@ -17,7 +17,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.core.exception.ExceptionUtil;
|
||||
import org.dromara.hutool.json.*;
|
||||
import org.dromara.hutool.json.writer.ValueWriterManager;
|
||||
|
||||
@@ -77,33 +77,38 @@ public class JSONValueMapper implements Serializable {
|
||||
* @param object 被映射的对象
|
||||
* @return 映射后的值,null表示此值需被忽略
|
||||
*/
|
||||
public Object map(final Object object) {
|
||||
// null、JSON、字符串和自定义对象原样存储
|
||||
if (null == object
|
||||
// 当用户自定义了对象的字符串表示形式,则保留这个对象
|
||||
|| null != ValueWriterManager.getInstance().get(object)
|
||||
|| object instanceof JSON //
|
||||
|| object instanceof CharSequence //
|
||||
|| ObjUtil.isBasicType(object) //
|
||||
) {
|
||||
if(object instanceof JSONPrimitive){
|
||||
return ((JSONPrimitive) object).getValue();
|
||||
}
|
||||
|
||||
return object;
|
||||
public JSON map(final Object object) {
|
||||
if(null == object || object instanceof JSON){
|
||||
return (JSON) object;
|
||||
}
|
||||
|
||||
if(null != ValueWriterManager.getInstance().get(object)){
|
||||
return new JSONPrimitive(object, jsonConfig);
|
||||
}
|
||||
|
||||
// if (object instanceof CharSequence
|
||||
// || ObjUtil.isBasicType(object)) {
|
||||
// return new JSONPrimitive(object, jsonConfig);
|
||||
// }
|
||||
|
||||
// 特定对象转换
|
||||
try {
|
||||
// JSONArray
|
||||
if (object instanceof Iterable || ArrayUtil.isArray(object)) {
|
||||
return new JSONArray(object, jsonConfig);
|
||||
final JSONArray jsonArray = new JSONArray(jsonConfig);
|
||||
JSONArrayMapper.of(object, null).mapTo(jsonArray);
|
||||
return jsonArray;
|
||||
}
|
||||
|
||||
// 默认按照JSONObject对待
|
||||
return new OldJSONObject(object, jsonConfig);
|
||||
final JSONObject jsonObject = new JSONObject(jsonConfig);
|
||||
JSONObjectMapper.of(object, null).mapTo(jsonObject);
|
||||
return jsonObject;
|
||||
} catch (final Exception exception) {
|
||||
return null;
|
||||
if(jsonConfig.isIgnoreError()){
|
||||
return null;
|
||||
}
|
||||
throw ExceptionUtil.wrap(exception, JSONException.class);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -121,7 +121,7 @@ public class JSONParser {
|
||||
}
|
||||
switch (tokener.nextClean()) {
|
||||
case CharUtil.DELIM_START:
|
||||
nextTo((OldJSONObject) json);
|
||||
nextTo((JSONObject) json);
|
||||
break;
|
||||
case CharUtil.BRACKET_START:
|
||||
nextTo((JSONArray) json);
|
||||
@@ -146,7 +146,7 @@ public class JSONParser {
|
||||
final JSON result;
|
||||
switch (firstChar) {
|
||||
case CharUtil.DELIM_START:
|
||||
final OldJSONObject jsonObject = new OldJSONObject(config);
|
||||
final JSONObject jsonObject = new JSONObject(config);
|
||||
nextTo(jsonObject);
|
||||
result = jsonObject;
|
||||
break;
|
||||
@@ -167,7 +167,7 @@ public class JSONParser {
|
||||
*
|
||||
* @param jsonObject JSON对象
|
||||
*/
|
||||
private void nextTo(final OldJSONObject jsonObject) {
|
||||
private void nextTo(final JSONObject jsonObject) {
|
||||
final JSONTokener tokener = this.tokener;
|
||||
|
||||
char c;
|
||||
|
@@ -17,7 +17,7 @@
|
||||
package org.dromara.hutool.json.serializer;
|
||||
|
||||
import org.dromara.hutool.json.JSON;
|
||||
import org.dromara.hutool.json.OldJSONObject;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
|
||||
/**
|
||||
* 序列化接口,通过实现此接口,实现自定义的对象转换为JSON的操作
|
||||
@@ -33,7 +33,7 @@ public interface JSONSerializer<V> {
|
||||
* <ul>
|
||||
* <li>如果为原始类型,可以转为{@link org.dromara.hutool.json.JSONPrimitive}</li>
|
||||
* <li>如果是集合或数组类,可以转为{@link org.dromara.hutool.json.JSONArray}</li>
|
||||
* <li>如果是Bean或键值对类型,可以转为{@link OldJSONObject}</li>
|
||||
* <li>如果是Bean或键值对类型,可以转为{@link JSONObject}</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param bean 指定类型对象
|
||||
|
@@ -66,12 +66,12 @@ public class TemporalAccessorSerializer implements JSONSerializer<TemporalAccess
|
||||
|
||||
@Override
|
||||
public JSON serialize(final TemporalAccessor bean, final JSONContext context) {
|
||||
final OldJSONObject json;
|
||||
final JSONObject json;
|
||||
final JSON contextJson = context.getContextJson();
|
||||
if(contextJson instanceof OldJSONObject){
|
||||
json = (OldJSONObject) contextJson;
|
||||
if(contextJson instanceof JSONObject){
|
||||
json = (JSONObject) contextJson;
|
||||
}else {
|
||||
json = new OldJSONObject(7F / 0.75F + 1F, context.config());
|
||||
json = new JSONObject((int) (7F / 0.75F + 1F), context.config());
|
||||
}
|
||||
if (bean instanceof LocalDate) {
|
||||
final LocalDate localDate = (LocalDate) bean;
|
||||
@@ -112,7 +112,7 @@ public class TemporalAccessorSerializer implements JSONSerializer<TemporalAccess
|
||||
// TODO JSONArray
|
||||
|
||||
// JSONObject
|
||||
final OldJSONObject jsonObject = (OldJSONObject) json;
|
||||
final JSONObject jsonObject = (JSONObject) json;
|
||||
if (LocalDate.class.equals(this.temporalAccessorClass) || LocalDateTime.class.equals(this.temporalAccessorClass)) {
|
||||
// 年
|
||||
final Integer year = jsonObject.getInt(YEAR_KEY);
|
||||
|
@@ -126,8 +126,8 @@ public class JSONWriter implements Appendable, Flushable, Closeable {
|
||||
* @return JSONWriter
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public JSONWriter copyOf() {
|
||||
return new JSONWriter(this.appendable, this.indentFactor, this.indent, this.config)
|
||||
public JSONWriter copyOfSub() {
|
||||
return new JSONWriter(this.appendable, this.indentFactor, this.indent + indentFactor, this.config)
|
||||
.setPredicate(this.predicate);
|
||||
}
|
||||
|
||||
|
@@ -22,7 +22,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.xml.XmlConstants;
|
||||
import org.dromara.hutool.json.InternalJSONUtil;
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.OldJSONObject;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@@ -67,7 +67,7 @@ public class JSONXMLParser {
|
||||
* @param jo JSONObject
|
||||
* @throws JSONException 解析异常
|
||||
*/
|
||||
public void parseJSONObject(final String xmlStr, final OldJSONObject jo) throws JSONException {
|
||||
public void parseJSONObject(final String xmlStr, final JSONObject jo) throws JSONException {
|
||||
final XMLTokener x = new XMLTokener(xmlStr);
|
||||
while (x.more() && x.skipPast("<")) {
|
||||
parse(x, jo, null, 0);
|
||||
@@ -78,16 +78,16 @@ public class JSONXMLParser {
|
||||
* 扫描XML内容,并解析到JSONObject中。
|
||||
*
|
||||
* @param x {@link XMLTokener}
|
||||
* @param context {@link OldJSONObject}
|
||||
* @param context {@link JSONObject}
|
||||
* @param name 标签名,null表示从根标签开始解析
|
||||
* @param currentNestingDepth 当前层级
|
||||
* @return {@code true}表示解析完成
|
||||
* @throws JSONException JSON异常
|
||||
*/
|
||||
private boolean parse(final XMLTokener x, final OldJSONObject context, final String name, final int currentNestingDepth) throws JSONException {
|
||||
private boolean parse(final XMLTokener x, final JSONObject context, final String name, final int currentNestingDepth) throws JSONException {
|
||||
final char c;
|
||||
int i;
|
||||
final OldJSONObject jsonobject;
|
||||
final JSONObject jsonobject;
|
||||
String string;
|
||||
final String tagName;
|
||||
Object token;
|
||||
@@ -108,7 +108,7 @@ public class JSONXMLParser {
|
||||
if (x.next() == '[') {
|
||||
string = x.nextCDATA();
|
||||
if (!string.isEmpty()) {
|
||||
context.append("content", string);
|
||||
append(context, "content", string);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -156,7 +156,7 @@ public class JSONXMLParser {
|
||||
} else {
|
||||
tagName = (String) token;
|
||||
token = null;
|
||||
jsonobject = new OldJSONObject();
|
||||
jsonobject = new JSONObject();
|
||||
final boolean keepStrings = parseConfig.isKeepStrings();
|
||||
for (; ; ) {
|
||||
if (token == null) {
|
||||
@@ -184,9 +184,9 @@ public class JSONXMLParser {
|
||||
throw x.syntaxError("Misshaped tag");
|
||||
}
|
||||
if (!jsonobject.isEmpty()) {
|
||||
context.append(tagName, jsonobject, this.predicate);
|
||||
append(context, tagName, jsonobject);
|
||||
} else {
|
||||
context.append(tagName, StrUtil.EMPTY, this.predicate);
|
||||
append(context, tagName, StrUtil.EMPTY);
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -216,11 +216,11 @@ public class JSONXMLParser {
|
||||
// Nested element
|
||||
if (parse(x, jsonobject, tagName, currentNestingDepth + 1)) {
|
||||
if (jsonobject.isEmpty()) {
|
||||
context.append(tagName, StrUtil.EMPTY, this.predicate);
|
||||
append(context, tagName, StrUtil.EMPTY);
|
||||
} else if (jsonobject.size() == 1 && jsonobject.get("content") != null) {
|
||||
context.append(tagName, jsonobject.get("content"), this.predicate);
|
||||
append(context, tagName, jsonobject.get("content"));
|
||||
} else {
|
||||
context.append(tagName, jsonobject, this.predicate);
|
||||
append(context, tagName, jsonobject);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -232,4 +232,24 @@ public class JSONXMLParser {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加键值对,通过MutableEntry实现过滤、修改键值对等
|
||||
*
|
||||
* @param jsonObject JSONObject
|
||||
* @param key 键
|
||||
* @param value 值
|
||||
*/
|
||||
private void append(final JSONObject jsonObject, String key, final Object value){
|
||||
if (null != predicate) {
|
||||
final MutableEntry<Object, Object> entry = new MutableEntry<>(key, value);
|
||||
if (predicate.test(entry)) {
|
||||
// 使用修改后的键值对
|
||||
key = (String) entry.getKey();
|
||||
jsonObject.append(key, entry.getValue());
|
||||
}
|
||||
}else {
|
||||
jsonObject.append(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -18,11 +18,11 @@ package org.dromara.hutool.json.xml;
|
||||
|
||||
import org.dromara.hutool.core.array.ArrayUtil;
|
||||
import org.dromara.hutool.core.text.CharUtil;
|
||||
import org.dromara.hutool.core.text.escape.EscapeUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.dromara.hutool.core.text.escape.EscapeUtil;
|
||||
import org.dromara.hutool.json.JSONArray;
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.OldJSONObject;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
|
||||
/**
|
||||
* JSON转XML字符串工具
|
||||
@@ -70,13 +70,13 @@ public class JSONXMLSerializer {
|
||||
}
|
||||
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
if (object instanceof OldJSONObject) {
|
||||
if (object instanceof JSONObject) {
|
||||
|
||||
// Emit <tagName>
|
||||
appendTag(sb, tagName, false);
|
||||
|
||||
// Loop thru the keys.
|
||||
((OldJSONObject) object).forEach((key, value) -> {
|
||||
((JSONObject) object).forEach((key, value) -> {
|
||||
if (ArrayUtil.isArray(value)) {
|
||||
value = new JSONArray(value);
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@
|
||||
package org.dromara.hutool.json.xml;
|
||||
|
||||
import org.dromara.hutool.json.JSONException;
|
||||
import org.dromara.hutool.json.OldJSONObject;
|
||||
import org.dromara.hutool.json.JSONObject;
|
||||
|
||||
/**
|
||||
* 提供静态方法在XML和JSONObject之间转换
|
||||
@@ -37,7 +37,7 @@ public class JSONXMLUtil {
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown if there is an errors while parsing the string
|
||||
*/
|
||||
public static OldJSONObject toJSONObject(final String string) throws JSONException {
|
||||
public static JSONObject toJSONObject(final String string) throws JSONException {
|
||||
return toJSONObject(string, ParseConfig.of());
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ public class JSONXMLUtil {
|
||||
* @return A JSONObject containing the structured data from the XML string.
|
||||
* @throws JSONException Thrown if there is an errors while parsing the string
|
||||
*/
|
||||
public static OldJSONObject toJSONObject(final String string, final ParseConfig parseConfig) throws JSONException {
|
||||
return toJSONObject(string, new OldJSONObject(), parseConfig);
|
||||
public static JSONObject toJSONObject(final String string, final ParseConfig parseConfig) throws JSONException {
|
||||
return toJSONObject(string, new JSONObject(), parseConfig);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,7 +67,7 @@ public class JSONXMLUtil {
|
||||
* @throws JSONException 解析异常
|
||||
* @since 5.3.1
|
||||
*/
|
||||
public static OldJSONObject toJSONObject(final String xmlStr, final OldJSONObject jo, final ParseConfig parseConfig) throws JSONException {
|
||||
public static JSONObject toJSONObject(final String xmlStr, final JSONObject jo, final ParseConfig parseConfig) throws JSONException {
|
||||
JSONXMLParser.of(parseConfig, null).parseJSONObject(xmlStr, jo);
|
||||
return jo;
|
||||
}
|
||||
|
Reference in New Issue
Block a user