Skip to content

Commit

Permalink
json format bean 支持排序输出 fix #156
Browse files Browse the repository at this point in the history
删除 com.feilong.lib.json.JSONArray.add(int, Object) fix #266

删除 com.feilong.lib.json.JSONArray.JSONArrayListIterator.JSONArrayListIterator()
fix #262

删除 com.feilong.lib.json.JSONArray.remove(int) fix #263

删除 com.feilong.lib.json.JSONArray.set(int, Object) fix #264

删除com.feilong.lib.json.JSONArray.add(int, Object, JsonConfig) fix #265

com.feilong.json.JsonUtil.toArray(Object, JsonToJavaConfig) 参数改成 String
fix #268

com.feilong.json.JsonUtil.toBean(Object, JsonToJavaConfig) 参数改成String
fix #269

com.feilong.json.JsonUtil.toBean(String, Class<T>) 参数改成String fix #270

com.feilong.json.JsonUtil.toMap(Object, JsonToJavaConfig) 参数改成 fix #271

com.feilong.json.JsonUtil.toMap(Object) 参数改成 String fix #272

com.feilong.json.JsonUtil.toList(Object, JsonToJavaConfig) 参数改成 String
fix #273

com.feilong.json.JsonUtil.toList(Object, Class<T>)参数改成 String fix #274
  • Loading branch information
venusdrogon committed Jun 14, 2020
1 parent 806f5b1 commit 74fb8d7
Show file tree
Hide file tree
Showing 23 changed files with 520 additions and 794 deletions.
49 changes: 14 additions & 35 deletions feilong-json/src/main/java/com/feilong/json/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import static com.feilong.core.lang.ObjectUtil.defaultIfNull;
import static com.feilong.json.builder.JsonConfigBuilder.DEFAULT_JAVA_TO_JSON_CONFIG;
import static com.feilong.lib.json.ToStringUtil.ARRAY_END;
import static com.feilong.lib.json.ToStringUtil.ARRAY_START;
import static com.feilong.lib.json.ToStringUtil.OBJECT_END;
import static com.feilong.lib.json.ToStringUtil.OBJECT_START;

import java.util.Iterator;
import java.util.Map;
Expand All @@ -26,11 +30,13 @@
import com.feilong.lib.collection4.IteratorUtils;
import com.feilong.lib.json.JSON;
import com.feilong.lib.json.JSONArray;
import com.feilong.lib.json.JSONNull;
import com.feilong.lib.json.JSONArrayBuilder;
import com.feilong.lib.json.JSONObject;
import com.feilong.lib.json.JSONObjectBuilder;
import com.feilong.lib.json.JSONTokener;
import com.feilong.lib.json.JsonConfig;
import com.feilong.lib.json.ToStringUtil;
import com.feilong.lib.json.processors.JsonValueProcessor;
import com.feilong.lib.json.util.JSONTokener;
import com.feilong.lib.json.util.JSONUtils;
import com.feilong.lib.lang3.ClassUtils;

Expand All @@ -56,28 +62,6 @@ private JsonHelper(){

//---------------------------------------------------------------

/**
* 转换value的值.
*
* @param <T>
* the generic type
* @param value
* the value
* @param jsonToJavaConfig
* the json to java config
* @return 如果 value 是 {@link JSONNull#getInstance()} ,那么返回null,<br>
* 如果null == jsonToJavaConfig 或者 null == jsonToJavaConfig.getRootClass() 返回value,<br>
* 否则,使用 {@link JsonUtil#toBean(Object, JsonToJavaConfig)} 转成对应的bean
*/
@SuppressWarnings("unchecked")
static <T> T transformerValue(Object value,JsonToJavaConfig jsonToJavaConfig){
if (JSONNull.getInstance().equals(value)){
return null;
}
//如果rootClass是null,表示不需要转换
boolean noRootClass = null == jsonToJavaConfig || null == jsonToJavaConfig.getRootClass();
return noRootClass ? (T) value : JsonUtil.<T> toBean(value, jsonToJavaConfig);
}
// [start]toJSON

/**
Expand Down Expand Up @@ -113,8 +97,6 @@ static <T> T transformerValue(Object value,JsonToJavaConfig jsonToJavaConfig){
* @param jsonConfig
* the json config
* @return the json
* @see com.feilong.lib.json.JSONArray#fromObject(Object, JsonConfig)
* @see com.feilong.lib.json.JSONObject#fromObject(Object, JsonConfig)
* @see com.feilong.lib.json.util.JSONUtils#isArray(Object)
* @see java.lang.Class#isEnum()
* @see com.feilong.lib.json.JsonConfig#registerJsonValueProcessor(Class, JsonValueProcessor)
Expand Down Expand Up @@ -153,13 +135,12 @@ static JSON toJSON(Object obj,JsonConfig jsonConfig){
* @param obj
* the obj
* @return true, if is need convert to JSON array
* @see com.feilong.lib.json.JSONArray#_fromJSONTokener(com.feilong.lib.json.util.JSONTokener, JsonConfig)
* @see com.feilong.lib.json.util.JSONUtils#isArray(Object)
*/
private static boolean isNeedConvertToJSONArray(Object obj){
if (obj instanceof String){
String str = (String) obj;
if (str.startsWith("[") && str.endsWith("]")){// [] 格式的字符串
if (str.startsWith(OBJECT_START) && str.endsWith(OBJECT_END)){// [] 格式的字符串
return true;
}
}
Expand Down Expand Up @@ -196,10 +177,10 @@ public static boolean isCommonString(Object obj){

//---------------------------------------------------------------
String str = (String) obj;
if (str.startsWith("[") && str.endsWith("]")){// [] 格式的字符串
if (str.startsWith(OBJECT_START) && str.endsWith(OBJECT_END)){// [] 格式的字符串
return false;
}
if (str.startsWith("{") && str.endsWith("}")){// {} 格式的字符串
if (str.startsWith(ARRAY_START) && str.endsWith(ARRAY_END)){// {} 格式的字符串
return false;
}
return true;
Expand Down Expand Up @@ -232,7 +213,7 @@ static boolean isKeyValueJsonString(Object obj){

//---------------------------------------------------------------
String str = (String) obj;
return str.startsWith("{") && str.endsWith("}");
return str.startsWith(ToStringUtil.ARRAY_START) && str.endsWith(ToStringUtil.ARRAY_END);
}

// [end]
Expand All @@ -247,10 +228,9 @@ static boolean isKeyValueJsonString(Object obj){
* @param useJsonConfig
* 如果是null,将使用 {@link #DEFAULT_JSON_CONFIG_INSTANCE}
* @return the JSON array
* @see com.feilong.lib.json.JSONArray#fromObject(Object, JsonConfig)
*/
static JSONArray toJSONArray(Object obj,JsonConfig useJsonConfig){
return JSONArray.fromObject(obj, defaultIfNull(useJsonConfig, DEFAULT_JSON_CONFIG_INSTANCE));
return JSONArrayBuilder.fromObject(obj, defaultIfNull(useJsonConfig, DEFAULT_JSON_CONFIG_INSTANCE));
}

//---------------------------------------------------------------
Expand All @@ -265,10 +245,9 @@ static JSONArray toJSONArray(Object obj,JsonConfig useJsonConfig){
* @param useJsonConfig
* 如果是null,将使用 {@link #DEFAULT_JSON_CONFIG_INSTANCE}
* @return the JSON object
* @see com.feilong.lib.json.JSONObject#fromObject(Object, JsonConfig)
*/
static JSONObject toJSONObject(Object object,JsonConfig useJsonConfig){
return JSONObject.fromObject(object, defaultIfNull(useJsonConfig, DEFAULT_JSON_CONFIG_INSTANCE));
return JSONObjectBuilder.build(object, defaultIfNull(useJsonConfig, DEFAULT_JSON_CONFIG_INSTANCE));
}

//---------------------------------------------------------------
Expand Down
82 changes: 57 additions & 25 deletions feilong-json/src/main/java/com/feilong/json/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
import com.feilong.lib.ezmorph.object.DateMorpher;
import com.feilong.lib.json.JSON;
import com.feilong.lib.json.JSONArray;
import com.feilong.lib.json.JSONNull;
import com.feilong.lib.json.JSONObject;
import com.feilong.lib.json.JSONObjectBuilder;
import com.feilong.lib.json.JSONObjectToBeanUtil;
import com.feilong.lib.json.JsonConfig;
import com.feilong.lib.json.util.JSONUtils;
import com.feilong.lib.lang3.StringUtils;
Expand Down Expand Up @@ -816,12 +819,13 @@ public static String formatObjectFieldsNameAndValueMap(Object obj){
* @return 如果 <code>json</code> 是null,返回 null<br>
* 如果 <code>jsonToJavaConfig</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>jsonToJavaConfig.getRootClass()</code> 是null,抛出 {@link NullPointerException}<br>
* @see #toBean(Object, JsonToJavaConfig)
* @see #toBean(String, JsonToJavaConfig)
* @see java.lang.reflect.Array#newInstance(Class, int)
* @since 1.9.4
* @since 3.0.6 change param type from Object to String
*/
@SuppressWarnings("squid:S1168") //Empty arrays and collections should be returned instead of null
public static <T> T[] toArray(Object json,JsonToJavaConfig jsonToJavaConfig){
public static <T> T[] toArray(String json,JsonToJavaConfig jsonToJavaConfig){
if (null == json){
return null;
}
Expand All @@ -840,7 +844,7 @@ public static <T> T[] toArray(Object json,JsonToJavaConfig jsonToJavaConfig){
@SuppressWarnings("unchecked")
T[] t = (T[]) ArrayUtil.newArray(rootClass, size);
for (int i = 0; i < size; i++){
t[i] = toBean(jsonArray.getJSONObject(i), jsonToJavaConfig);
t[i] = toBean(jsonArray.getJSONObject(i).toString(), jsonToJavaConfig);
}
return t;
}catch (Exception e){
Expand Down Expand Up @@ -904,10 +908,11 @@ public static <T> T[] toArray(Object json,JsonToJavaConfig jsonToJavaConfig){
* the klass,see {@link com.feilong.lib.json.JsonConfig#setRootClass(Class)}
* @return 如果<code>json</code> 是null,那么返回 null<br>
* 如果 <code>rootClass()</code> 是null,抛出 {@link NullPointerException}<br>
* @see #toList(Object, JsonToJavaConfig)
* @see #toList(String, JsonToJavaConfig)
* @since 3.0.6 change param type from Object to String
*/
@SuppressWarnings("squid:S1168") //Empty arrays and collections should be returned instead of null
public static <T> List<T> toList(Object json,Class<T> rootClass){
public static <T> List<T> toList(String json,Class<T> rootClass){
if (null == json){
return null;
}
Expand Down Expand Up @@ -992,11 +997,11 @@ public static <T> List<T> toList(Object json,Class<T> rootClass){
* 如果 <code>jsonToJavaConfig.getRootClass()</code> 是null,抛出 {@link NullPointerException}<br>
*
* @see com.feilong.lib.json.JSONArray#getJSONObject(int)
* @see com.feilong.lib.json.JSONArray#fromObject(Object)
* @see #toBean(Object, JsonToJavaConfig)
* @see #toBean(String, JsonToJavaConfig)
* @since 3.0.6 change param type from Object to String
*/
@SuppressWarnings("squid:S1168") //Empty arrays and collections should be returned instead of null
public static <T> List<T> toList(Object json,JsonToJavaConfig jsonToJavaConfig){
public static <T> List<T> toList(String json,JsonToJavaConfig jsonToJavaConfig){
if (null == json){
return null;
}
Expand All @@ -1010,7 +1015,7 @@ public static <T> List<T> toList(Object json,JsonToJavaConfig jsonToJavaConfig){
JSONArray jsonArray = JsonHelper.toJSONArray(json, null);
List<T> list = newArrayList();
for (int i = 0, j = jsonArray.size(); i < j; i++){
list.add(JsonUtil.<T> toBean(jsonArray.getJSONObject(i), jsonToJavaConfig));
list.add(objectToBean(jsonArray.getJSONObject(i), jsonToJavaConfig));
}
return list;
}catch (Exception e){
Expand Down Expand Up @@ -1090,10 +1095,11 @@ public static <T> List<T> toList(Object json,JsonToJavaConfig jsonToJavaConfig){
* the json
* @return 如果 <code>json</code> 是null或者empty,返回 {@link Collections#emptyMap()}<br>
* 如果 <code>json</code> 不是Map格式的json字符串,抛出 {@link IllegalArgumentException}<br>
* @see #toMap(Object, JsonToJavaConfig)
* @see #toMap(String, JsonToJavaConfig)
* @since 1.5.0
* @since 3.0.6 change param type from Object to String
*/
public static <T> Map<String, T> toMap(Object json){
public static <T> Map<String, T> toMap(String json){
return toMap(json, null);
}

Expand Down Expand Up @@ -1151,11 +1157,11 @@ public static <T> Map<String, T> toMap(Object json){
* @return 如果 <code>json</code> 是null或者empty,返回 {@link Collections#emptyMap()}<br>
* 如果 <code>json</code> 不是Map格式的json字符串,抛出 {@link IllegalArgumentException}<br>
* 如果 <code>rootClass</code> 是null,那么直接将json里面的value 作为map 的value
* @see #toBean(Object, JsonToJavaConfig)
* @see #toBean(String, JsonToJavaConfig)
* @since 1.9.2 use LinkedHashMap instead of HashMap
* @since 1.9.4
* @since 3.0.6 change param type from Object to String
*/
public static <T> Map<String, T> toMap(Object json,JsonToJavaConfig jsonToJavaConfig){
public static <T> Map<String, T> toMap(String json,JsonToJavaConfig jsonToJavaConfig){
LOGGER.trace("input json:[{}],jsonToJavaConfig:[{}]", json, jsonToJavaConfig);
if (isNullOrEmpty(json)){
return emptyMap();
Expand All @@ -1174,7 +1180,7 @@ public static <T> Map<String, T> toMap(Object json,JsonToJavaConfig jsonToJavaCo
Object value = jsonObject.get(key);
LOGGER.trace("key:[{}],value:[{}],value type is:[{}]", key, value, value.getClass().getName());

map.put(key, JsonHelper.<T> transformerValue(value, jsonToJavaConfig));
map.put(key, transformerValue(value, jsonToJavaConfig));
}
return map;
}catch (Exception e){
Expand Down Expand Up @@ -1230,16 +1236,15 @@ public static <T> Map<String, T> toMap(Object json,JsonToJavaConfig jsonToJavaCo
* e.g. {'name':'get','dateAttr':'2009-11-12'}<br>
* 可以是 json字符串,也可以是JSONObject<br>
* Accepts JSON formatted strings, Maps, DynaBeans and JavaBeans. <br>
* 支持的格式有: {@link JSONObject#fromObject(Object, JsonConfig)}
* @param rootClass
* e.g. Person.class,see {@link com.feilong.lib.json.JsonConfig#setRootClass(Class)}
* @return 如果<code>json</code> 是null,那么返回 null <br>
* 如果 <code>rootClass</code> 是null,抛出 {@link NullPointerException}<br>
* @see JSONObject#fromObject(Object, JsonConfig)
* @see com.feilong.lib.json.JsonConfig#setRootClass(Class)
* @see #toBean(Object, JsonToJavaConfig)
* @see #toBean(String, JsonToJavaConfig)
* @since 3.0.6 change param type from Object to String
*/
public static <T> T toBean(Object json,Class<T> rootClass){
public static <T> T toBean(String json,Class<T> rootClass){
if (null == json){
return null;
}
Expand Down Expand Up @@ -1318,12 +1323,39 @@ public static <T> T toBean(Object json,Class<T> rootClass){
* @return 如果<code>json</code> 是null,那么返回 null<br>
* 如果 <code>jsonToJavaConfig</code> 是null,抛出 {@link NullPointerException}<br>
* 如果 <code>jsonToJavaConfig.getRootClass()</code> 是null,抛出 {@link NullPointerException}<br>
* @see JSONObject#fromObject(Object, JsonConfig)
* @see com.feilong.lib.json.JsonConfig#setRootClass(Class)
* @since 1.9.4
* @since 3.0.6 change param type from Object to String
*/
public static <T> T toBean(String json,JsonToJavaConfig jsonToJavaConfig){
return objectToBean(json, jsonToJavaConfig);
}

/**
* 转换value的值.
*
* @param <T>
* the generic type
* @param value
* the value
* @param jsonToJavaConfig
* the json to java config
* @return 如果 value 是 {@link JSONNull#getInstance()} ,那么返回null,<br>
* 如果null == jsonToJavaConfig 或者 null == jsonToJavaConfig.getRootClass() 返回value,<br>
* 否则,使用 {@link #objectToBean(Object, JsonToJavaConfig)} 转成对应的bean
*/
@SuppressWarnings("unchecked")
public static <T> T toBean(Object json,JsonToJavaConfig jsonToJavaConfig){
static <T> T transformerValue(Object value,JsonToJavaConfig jsonToJavaConfig){
if (JSONNull.getInstance().equals(value)){
return null;
}
//如果rootClass是null,表示不需要转换
boolean noRootClass = null == jsonToJavaConfig || null == jsonToJavaConfig.getRootClass();
return noRootClass ? (T) value : objectToBean(value, jsonToJavaConfig);
}

@SuppressWarnings("unchecked")
private static <T> T objectToBean(Object json,JsonToJavaConfig jsonToJavaConfig){
if (null == json){
return null;
}
Expand All @@ -1336,10 +1368,10 @@ public static <T> T toBean(Object json,JsonToJavaConfig jsonToJavaConfig){
//---------------------------------------------------------------
JsonConfig jsonConfig = JsonToJavaConfigBuilder.build(rootClass, jsonToJavaConfig);
try{
JSONObject jsonObject = JSONObject.fromObject(json);
return (T) JSONObject.toBean(jsonObject, jsonConfig);
JSONObject jsonObject = JSONObjectBuilder.build(json, new JsonConfig());
return (T) JSONObjectToBeanUtil.toBean(jsonObject, jsonConfig);
}catch (Exception e){
throw new JsonToJavaException(buildJsonToJavaExceptionMessage(json, jsonToJavaConfig), e);
throw new JsonToJavaException(buildJsonToJavaExceptionMessage(json.toString(), jsonToJavaConfig), e);
}
}

Expand All @@ -1355,7 +1387,7 @@ public static <T> T toBean(Object json,JsonToJavaConfig jsonToJavaConfig){
* @return the string
* @since 1.11.5
*/
private static String buildJsonToJavaExceptionMessage(Object json,JsonToJavaConfig jsonToJavaConfig){
private static String buildJsonToJavaExceptionMessage(String json,JsonToJavaConfig jsonToJavaConfig){
return Slf4jUtil.format("input json:{},jsonToJavaConfig:{}", json, format(jsonToJavaConfig, true));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import com.feilong.json.entity.MyBean;
import com.feilong.lib.json.JSON;
import com.feilong.lib.json.JSONObject;
import com.feilong.lib.json.JSONObjectBuilder;
import com.feilong.lib.json.JSONObjectToBeanUtil;
import com.feilong.lib.json.JsonConfig;
import com.feilong.store.member.Person;
import com.feilong.test.AbstractTest;
Expand All @@ -44,8 +46,8 @@ public class JsonHelperTest extends AbstractTest{
public void name(){
String json_test = "{name=\"json\",bool:true,int:1,double:2.2,array:[1,2]}";

JSONObject jsonObject = JSONObject.fromObject(json_test);
Object bean = JSONObject.toBean(jsonObject);
JSONObject jsonObject = JSONObjectBuilder.build(json_test, new JsonConfig());
Object bean = JSONObjectToBeanUtil.toBean(jsonObject, null);

assertEquals(jsonObject.get("name"), PropertyUtil.getProperty(bean, "name"));
assertEquals(jsonObject.get("bool"), PropertyUtil.getProperty(bean, "bool"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

public class FormatBeanExcludesTest extends AbstractJsonTest{

@SuppressWarnings("unchecked")
@Test
public void testExcludes(){
String format = JsonUtil.format(USER, toArray("name", "loves", "attrMap", "userInfo", "userAddresses"));
Expand All @@ -47,6 +48,7 @@ public void testExcludes(){
));
}

@SuppressWarnings("unchecked")
@Test
public void testExcludes2(){
String format = JsonUtil.format(USER, "name", "loves", "attrMap", "userInfo", "userAddresses");
Expand All @@ -68,6 +70,7 @@ public void testExcludes2(){

//---------------------------------------------------------------

@SuppressWarnings("unchecked")
@Test
public void testExcludes1(){
String format = JsonUtil.format(USER, toArray("name", "loves", "attrMap", "userInfo", "userAddresses"), 0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.feilong.json.entity.BeanIntIgnoreNullNests;
import com.feilong.json.entity.BeanIntIgnoreNullParent;
import com.feilong.lib.json.JSONObject;
import com.feilong.lib.json.JSONObjectBuilder;
import com.feilong.lib.json.JsonConfig;
import com.feilong.test.AbstractTest;

public class FormatBeanIgnoreNullTest extends AbstractTest{
Expand Down Expand Up @@ -55,13 +57,13 @@ public void testAnd(){

@Test
public void test12MAP(){
JSONObject jsonObject = JSONObject.fromObject(toMap("age", 16, "name", null));
JSONObject jsonObject = JSONObjectBuilder.build(toMap("age", 16, "name", null), new JsonConfig());
LOGGER.debug(jsonObject.toString(0, 0));
}

@Test
public void test12(){
JSONObject jsonObject = JSONObject.fromObject(beanIntIgnoreNull);
JSONObject jsonObject = JSONObjectBuilder.build(beanIntIgnoreNull, new JsonConfig());
LOGGER.debug(jsonObject.toString());
}

Expand Down
Loading

0 comments on commit 74fb8d7

Please sign in to comment.