diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3d19d7b27..ae65a09bb 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@
* 【core 】 增加XmlUtil.setNamespaceAware,getByPath支持UniversalNamespaceCache
* 【core 】 增加XmlUtil.setNamespaceAware,getByPath支持UniversalNamespaceCache
* 【aop 】 增加Spring-cglib支持,改为SPI实现
+* 【json 】 增加JSONUtil.parseXXX增加JSONConfig参数
### Bug修复
* 【json 】 修复解析JSON字符串时配置无法传递问题
diff --git a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java
index 0d5501c24..264a11dac 100644
--- a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java
+++ b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java
@@ -188,8 +188,7 @@ final class InternalJSONUtil {
* @return JSONObject
*/
protected static JSONObject propertyPut(JSONObject jsonObject, Object key, Object value) {
- String keyStr = Convert.toStr(key);
- String[] path = StrUtil.split(keyStr, StrUtil.DOT);
+ final String[] path = StrUtil.split(Convert.toStr(key), StrUtil.DOT);
int last = path.length - 1;
JSONObject target = jsonObject;
for (int i = 0; i < last; i += 1) {
diff --git a/hutool-json/src/main/java/cn/hutool/json/JSON.java b/hutool-json/src/main/java/cn/hutool/json/JSON.java
index cdbe26e6f..dbc518002 100644
--- a/hutool-json/src/main/java/cn/hutool/json/JSON.java
+++ b/hutool-json/src/main/java/cn/hutool/json/JSON.java
@@ -1,13 +1,13 @@
package cn.hutool.json;
+import cn.hutool.core.bean.BeanPath;
+import cn.hutool.core.lang.TypeReference;
+
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Type;
-import cn.hutool.core.bean.BeanPath;
-import cn.hutool.core.lang.TypeReference;
-
/**
* JSON接口
*
diff --git a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java
index ea5f6c000..da07be15a 100644
--- a/hutool-json/src/main/java/cn/hutool/json/JSONObject.java
+++ b/hutool-json/src/main/java/cn/hutool/json/JSONObject.java
@@ -23,32 +23,41 @@ import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
+import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
+import java.util.ResourceBundle;
import java.util.Set;
/**
* JSON对象
* 例:
- *
+ *
*
* json = new JSONObject().put("JSON", "Hello, World!").toString(); *- * + * * @author looly */ public class JSONObject implements JSON, JSONGetter
* 如果给定值为Map,将键值对加入JSON对象;
* KEY或VALUE任意一个为null则不加入,字段不存在也不加入
* 如果为普通的JavaBean,调用其getters方法(getXXX或者isXXX)获得值,加入到JSON对象
* 例如:如果JavaBean对象中有个方法getName(),值为"张三",获得的键值对为:name: "张三"
- *
+ *
* @param source JavaBean或者Map对象或者String
* @param config JSON配置文件
* @since 4.2.2
@@ -195,16 +204,16 @@ public class JSONObject implements JSON, JSONGetter
* 1. 若obj为Map,则获取name列表对应键值对
* 2. 若obj为普通Bean,使用反射方式获取字段名和字段值
*
- *
+ *
* 若names列表为空,则字段全部加入
*
- * @param obj 包含需要字段的Bean对象或者Map对象
+ * @param obj 包含需要字段的Bean对象或者Map对象
* @param names 需要构建JSONObject的字段名列表
*/
public JSONObject(Object obj, String... names) {
@@ -234,9 +243,9 @@ public class JSONObject implements JSON, JSONGetternull
,将此键移除
*
- * @param key 键
+ * @param key 键
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
* @return this.
* @throws JSONException 值是无穷数字抛出此异常
@@ -360,7 +369,7 @@ public class JSONObject implements JSON, JSONGetternull
,将此键移除
*
- * @param key 键
+ * @param key 键
* @param value 值对象. 可以是以下类型: Boolean, Double, Integer, JSONArray, JSONObject, Long, String, or the JSONNull.NULL.
* @return this.
* @throws JSONException 值是无穷数字抛出此异常
@@ -373,7 +382,7 @@ public class JSONObject implements JSON, JSONGetter
* 如果只有一个值,此值就是value,如果多个值,则是添加到新的JSONArray中
*
- * @param key 键
+ * @param key 键
* @param value 被积累的值
* @return this.
* @throws JSONException 如果给定键为null
或者键对应的值存在且为非JSONArray
@@ -435,7 +444,7 @@ public class JSONObject implements JSON, JSONGetternull
或者键对应的值存在且为非JSONArray
@@ -559,12 +568,13 @@ public class JSONObject implements JSON, JSONGetter
+ * 此方法会忽略空值,但是对JSON字符串不影响
+ *
+ * @param obj Bean对象或者Map
+ * @param config JSON配置
+ * @return JSONObject
+ * @since 5.3.1
+ */
+ public static JSONObject parseObj(Object obj, JSONConfig config) {
+ return new JSONObject(obj, config);
+ }
+
/**
* JSON字符串转JSONObject对象
*
@@ -146,6 +158,18 @@ public final class JSONUtil {
return new JSONArray(arrayOrCollection);
}
+ /**
+ * JSON字符串转JSONArray
+ *
+ * @param arrayOrCollection 数组或集合对象
+ * @param config JSON配置
+ * @return JSONArray
+ * @since 5.3.1
+ */
+ public static JSONArray parseArray(Object arrayOrCollection, JSONConfig config) {
+ return new JSONArray(arrayOrCollection, config);
+ }
+
/**
* JSON字符串转JSONArray
*
@@ -169,6 +193,22 @@ public final class JSONUtil {
* @return JSON
*/
public static JSON parse(Object obj) {
+ return parse(obj, JSONConfig.create());
+ }
+
+ /**
+ * 转换对象为JSON
+ * 支持的对象:
+ * String: 转换为相应的对象
+ * Array、Iterable、Iterator:转换为JSONArray
+ * Bean对象:转为JSONObject
+ *
+ * @param obj 对象
+ * @param config JSON配置
+ * @return JSON
+ * @since 5.3.1
+ */
+ public static JSON parse(Object obj, JSONConfig config) {
if (null == obj) {
return null;
}
@@ -176,17 +216,13 @@ public final class JSONUtil {
JSON json;
if (obj instanceof JSON) {
json = (JSON) obj;
- } else if (obj instanceof String) {
- String jsonStr = ((String) obj).trim();
- if (jsonStr.startsWith("[")) {
- json = parseArray(jsonStr);
- } else {
- json = parseObj(jsonStr);
- }
- } else if (obj instanceof Collection || obj.getClass().isArray()) {// 列表
- json = new JSONArray(obj);
+ } else if (obj instanceof CharSequence) {
+ final String jsonStr = StrUtil.trim((CharSequence) obj);
+ json = StrUtil.startWith(jsonStr, '[') ? parseArray(jsonStr) : parseObj(jsonStr);
+ } else if (obj instanceof Iterable || obj instanceof Iterator || ArrayUtil.isArray(obj)) {// 列表
+ json = new JSONArray(obj, config);
} else {// 对象
- json = new JSONObject(obj);
+ json = new JSONObject(obj, config);
}
return json;
@@ -206,8 +242,10 @@ public final class JSONUtil {
* Map转化为JSONObject
*
* @param map {@link Map}
- * @return JSONObject
+ * @return JSONObjec
+ * @deprecated 请直接使用 {@link #parseObj(Object)}
*/
+ @Deprecated
public static JSONObject parseFromMap(Map, ?> map) {
return new JSONObject(map);
}
@@ -217,17 +255,11 @@ public final class JSONUtil {
*
* @param bundle ResourceBundle文件
* @return JSONObject
+ * @deprecated 请直接使用 {@link #parseObj(Object)}
*/
+ @Deprecated
public static JSONObject parseFromResourceBundle(ResourceBundle bundle) {
- JSONObject jsonObject = new JSONObject();
- Enumeration<[ [ ]]>
are ignored.
+ *
+ * @param string The source string.
+ * @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 JSONObject toJSONObject(String string) throws JSONException {
+ return toJSONObject(string, false);
+ }
+
+ /**
+ * 转换XML为JSONObject
+ * 转换过程中一些信息可能会丢失,JSON中无法区分节点和属性,相同的节点将被处理为JSONArray。
+ * Content text may be placed in a "content" member. Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
+ * All values are converted as strings, for 1, 01, 29.0 will not be coerced to numbers but will instead be the exact value as seen in the XML document.
+ *
+ * @param string The source string.
+ * @param keepStrings If true, then values will not be coerced into boolean or numeric values and will instead be left as strings
+ * @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 JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
+ return toJSONObject(new JSONObject(), string, keepStrings);
+ }
+
+ /**
+ * 转换XML为JSONObject
+ * 转换过程中一些信息可能会丢失,JSON中无法区分节点和属性,相同的节点将被处理为JSONArray。
+ *
+ * @param jo JSONObject
+ * @param string XML字符串
+ * @param keepStrings If true, then values will not be coerced into boolean or numeric values and will instead be left as strings
+ * @return A JSONObject containing the structured data from the XML string.
+ * @throws JSONException Thrown if there is an errors while parsing the string
+ * @since 5.3.1
+ */
+ public static JSONObject toJSONObject(JSONObject jo, String string, boolean keepStrings) throws JSONException {
+ XMLTokener x = new XMLTokener(string, jo.getConfig());
+ while (x.more() && x.skipPast("<")) {
+ parse(x, jo, null, keepStrings);
+ }
+ return jo;
+ }
+
/**
* Scan the content following the named tag, attaching it to the context.
- *
- * @param x The XMLTokener containing the source string.
+ *
+ * @param x The XMLTokener containing the source string.
* @param context The JSONObject that will include the new material.
- * @param name The tag name.
+ * @param name The tag name.
* @return true if the close tag is processed.
* @throws JSONException JSON异常
*/
@@ -135,7 +200,7 @@ public class XML {
tagName = (String) token;
token = null;
jsonobject = new JSONObject();
- for (;;) {
+ for (; ; ) {
if (token == null) {
token = x.nextToken();
}
@@ -169,7 +234,7 @@ public class XML {
} else if (token == GT) {
// Content, between <...> and
- for (;;) {
+ for (; ; ) {
token = x.nextContent();
if (token == null) {
if (tagName != null) {
@@ -203,43 +268,10 @@ public class XML {
}
}
- /**
- * 转换XML为JSONObject
- * 转换过程中一些信息可能会丢失,JSON中无法区分节点和属性,相同的节点将被处理为JSONArray。
- * Content text may be placed in a "content" member. Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
- *
- * @param string The source string.
- * @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 JSONObject toJSONObject(String string) throws JSONException {
- return toJSONObject(string, false);
- }
-
- /**
- * 转换XML为JSONObject
- * 转换过程中一些信息可能会丢失,JSON中无法区分节点和属性,相同的节点将被处理为JSONArray。
- * Content text may be placed in a "content" member. Comments, prologs, DTDs, and <[ [ ]]>
are ignored.
- * All values are converted as strings, for 1, 01, 29.0 will not be coerced to numbers but will instead be the exact value as seen in the XML document.
- *
- * @param string The source string.
- * @param keepStrings If true, then values will not be coerced into boolean or numeric values and will instead be left as strings
- * @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 JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
- JSONObject jo = new JSONObject();
- XMLTokener x = new XMLTokener(string, jo.getConfig());
- while (x.more() && x.skipPast("<")) {
- parse(x, jo, null, keepStrings);
- }
- return jo;
- }
-
/**
* 转换JSONObject为XML
* Convert a JSONObject into a well-formed, element-normal XML string.
- *
+ *
* @param object A JSONObject.
* @return A string.
* @throws JSONException Thrown if there is an error parsing the string
@@ -251,17 +283,17 @@ public class XML {
/**
* 转换JSONObject为XML
* Convert a JSONObject into a well-formed, element-normal XML string.
- *
- * @param object A JSONObject.
+ *
+ * @param object A JSONObject.
* @param tagName The optional name of the enclosing tag.
* @return A string.
* @throws JSONException Thrown if there is an error parsing the string
*/
public static String toXml(Object object, String tagName) throws JSONException {
- if(null == object) {
+ if (null == object) {
return null;
}
-
+
StringBuilder sb = new StringBuilder();
JSONArray ja;
JSONObject jo;