mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
fix code
This commit is contained in:
parent
185fb1a72e
commit
9223811716
@ -130,7 +130,7 @@ Each module can be introduced individually, or all modules can be introduced by
|
||||
|
||||
### 🍐Gradle
|
||||
```
|
||||
compile 'cn.hutool:hutool-all:5.7.12'
|
||||
implementation 'cn.hutool:hutool-all:5.7.12'
|
||||
```
|
||||
|
||||
## 📥Download
|
||||
|
@ -128,7 +128,7 @@ Hutool的存在就是为了减少代码搜索成本,避免网络上参差不
|
||||
|
||||
### 🍐Gradle
|
||||
```
|
||||
compile 'cn.hutool:hutool-all:5.7.12'
|
||||
implementation 'cn.hutool:hutool-all:5.7.12'
|
||||
```
|
||||
|
||||
### 📥下载jar
|
||||
|
@ -5,6 +5,7 @@ import cn.hutool.core.collection.ArrayIter;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.text.StrJoiner;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.core.util.TypeUtil;
|
||||
import cn.hutool.json.serialize.GlobalSerializeMapping;
|
||||
@ -91,7 +92,7 @@ public class JSONArray implements JSON, JSONGetter<Integer>, List<Object>, Rando
|
||||
*/
|
||||
public JSONArray(int initialCapacity, JSONConfig config) {
|
||||
this.rawList = new ArrayList<>(initialCapacity);
|
||||
this.config = config;
|
||||
this.config = ObjectUtil.defaultIfNull(config, JSONConfig.create());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -12,7 +12,7 @@ public class JSONConfig implements Serializable {
|
||||
private static final long serialVersionUID = 119730355204738278L;
|
||||
|
||||
/**
|
||||
* 是否有序,顺序按照加入顺序排序
|
||||
* 是否有序,顺序按照加入顺序排序,只针对JSONObject有效
|
||||
*/
|
||||
private boolean order;
|
||||
/**
|
||||
@ -51,7 +51,7 @@ public class JSONConfig implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否有序,顺序按照加入顺序排序
|
||||
* 是否有序,顺序按照加入顺序排序,只针对JSONObject有效
|
||||
*
|
||||
* @return 是否有序
|
||||
*/
|
||||
@ -60,7 +60,7 @@ public class JSONConfig implements Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置是否有序,顺序按照加入顺序排序
|
||||
* 设置是否有序,顺序按照加入顺序排序,只针对JSONObject有效
|
||||
*
|
||||
* @param order 是否有序
|
||||
* @return this
|
||||
|
@ -110,7 +110,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
* 构造
|
||||
*
|
||||
* @param capacity 初始大小
|
||||
* @param config JSON配置项,null表示默认配置
|
||||
* @param config JSON配置项,{@code null}则使用默认配置
|
||||
* @since 4.1.19
|
||||
*/
|
||||
public JSONObject(int capacity, JSONConfig config) {
|
||||
@ -192,7 +192,7 @@ public class JSONObject implements JSON, JSONGetter<String>, Map<String, Object>
|
||||
* 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"
|
||||
*
|
||||
* @param source JavaBean或者Map对象或者String
|
||||
* @param config JSON配置文件
|
||||
* @param config JSON配置文件,{@code null}则使用默认配置
|
||||
* @since 4.2.2
|
||||
*/
|
||||
public JSONObject(Object source, JSONConfig config) {
|
||||
|
@ -96,7 +96,7 @@ public class JSONUtil {
|
||||
* @return JSONObject
|
||||
*/
|
||||
public static JSONObject parseObj(Object obj) {
|
||||
return new JSONObject(obj);
|
||||
return parseObj(obj, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,6 +109,13 @@ public class JSONUtil {
|
||||
* @since 5.3.1
|
||||
*/
|
||||
public static JSONObject parseObj(Object obj, JSONConfig config) {
|
||||
// 默认配置,根据对象类型决定是否有序
|
||||
if(null == config){
|
||||
config = JSONConfig.create();
|
||||
if(InternalJSONUtil.isOrder(obj)){
|
||||
config.setOrder(true);
|
||||
}
|
||||
}
|
||||
return new JSONObject(obj, config);
|
||||
}
|
||||
|
||||
@ -121,7 +128,7 @@ public class JSONUtil {
|
||||
* @since 3.0.9
|
||||
*/
|
||||
public static JSONObject parseObj(Object obj, boolean ignoreNullValue) {
|
||||
return new JSONObject(obj, ignoreNullValue);
|
||||
return parseObj(obj, ignoreNullValue, InternalJSONUtil.isOrder(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -155,7 +162,7 @@ public class JSONUtil {
|
||||
* @since 3.0.8
|
||||
*/
|
||||
public static JSONArray parseArray(Object arrayOrCollection) {
|
||||
return new JSONArray(arrayOrCollection);
|
||||
return parseArray(arrayOrCollection, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -183,36 +190,32 @@ public class JSONUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换对象为JSON<br>
|
||||
* 支持的对象:<br>
|
||||
* String: 转换为相应的对象<br>
|
||||
* Array Collection:转换为JSONArray<br>
|
||||
* Bean对象:转为JSONObject
|
||||
* 转换对象为JSON,如果用户不配置JSONConfig,则JSON的有序与否与传入对象有关。<br>
|
||||
* 支持的对象:
|
||||
* <ul>
|
||||
* <li>String: 转换为相应的对象</li>
|
||||
* <li>Array、Iterable、Iterator:转换为JSONArray</li>
|
||||
* <li>Bean对象:转为JSONObject</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param obj 对象
|
||||
* @return JSON
|
||||
*/
|
||||
public static JSON parse(Object obj) {
|
||||
if(obj instanceof JSON){
|
||||
return (JSON) obj;
|
||||
}
|
||||
|
||||
final JSONConfig config = JSONConfig.create();
|
||||
if(InternalJSONUtil.isOrder(obj)){
|
||||
config.setOrder(true);
|
||||
}
|
||||
return parse(obj, config);
|
||||
return parse(obj, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换对象为JSON<br>
|
||||
* 支持的对象:<br>
|
||||
* String: 转换为相应的对象<br>
|
||||
* Array、Iterable、Iterator:转换为JSONArray<br>
|
||||
* Bean对象:转为JSONObject
|
||||
* 转换对象为JSON,如果用户不配置JSONConfig,则JSON的有序与否与传入对象有关。<br>
|
||||
* 支持的对象:
|
||||
* <ul>
|
||||
* <li>String: 转换为相应的对象</li>
|
||||
* <li>Array、Iterable、Iterator:转换为JSONArray</li>
|
||||
* <li>Bean对象:转为JSONObject</li>
|
||||
* </ul>
|
||||
*
|
||||
* @param obj 对象
|
||||
* @param config JSON配置
|
||||
* @param config JSON配置,{@code null}使用默认配置
|
||||
* @return JSON
|
||||
* @since 5.3.1
|
||||
*/
|
||||
@ -220,7 +223,6 @@ public class JSONUtil {
|
||||
if (null == obj) {
|
||||
return null;
|
||||
}
|
||||
|
||||
JSON json;
|
||||
if (obj instanceof JSON) {
|
||||
json = (JSON) obj;
|
||||
@ -228,9 +230,9 @@ public class JSONUtil {
|
||||
final String jsonStr = StrUtil.trim((CharSequence) obj);
|
||||
json = isJsonArray(jsonStr) ? parseArray(jsonStr, config) : parseObj(jsonStr, config);
|
||||
} else if (obj instanceof Iterable || obj instanceof Iterator || ArrayUtil.isArray(obj)) {// 列表
|
||||
json = new JSONArray(obj, config);
|
||||
json = parseArray(obj, config);
|
||||
} else {// 对象
|
||||
json = new JSONObject(obj, config);
|
||||
json = parseObj(obj, config);
|
||||
}
|
||||
|
||||
return json;
|
||||
|
Loading…
Reference in New Issue
Block a user