diff --git a/feilong-json/src/main/java/com/feilong/json/JsonUtil.java b/feilong-json/src/main/java/com/feilong/json/JsonUtil.java index f136d1702..48f34bd3d 100755 --- a/feilong-json/src/main/java/com/feilong/json/JsonUtil.java +++ b/feilong-json/src/main/java/com/feilong/json/JsonUtil.java @@ -363,6 +363,57 @@ public static String formatSimpleMap(Map inputMap,Class...allowF //--------------------------------------------------------------- + /** + * 将对象格式化成json字符串,并且 toString(0, 0) 输出. + * + *

+ * =com.feilong.json.JsonUtil.format(Object, 0, 0) + *

+ * + *

示例:

+ * + *
+ * + *
+     * 
+     * User user = new User();
+     * 
+     * user.setPassword("123456");
+     * user.setId(8L);
+     * user.setName("feilong");
+     * user.setDate(now());
+     * user.setMoney(toBigDecimal("99999999.00"));
+     * 
+     * user.setLoves(toArray("桔子", "香蕉"));
+     * user.setUserInfo(new UserInfo(10));
+     * 
+     * UserAddress userAddress1 = new UserAddress("上海市地址1");
+     * UserAddress userAddress2 = new UserAddress("上海市地址2");
+     * 
+     * user.setUserAddresses(toArray(userAddress1, userAddress2));
+     * user.setUserAddresseList(toList(userAddress1, userAddress2));
+     * 
+     * LOGGER.debug(JsonUtil.toString(USER));
+     * 
+     * 
+ * + * 返回: + * + *
+    {"date":"2022-07-03 16:23:00","userInfo":{"age":10},"loves":["桔子","香蕉"],"userAddresses":[{"address":"上海市地址1"},{"address":"上海市地址2"}],"attrMap":null,"ageInt":0,"password":"123456","money":"99999999.00","name":"feilong","userAddresseList":[{"address":"上海市地址1"},{"address":"上海市地址2"}],"id":8,"age":null,"nickNames":[]}
+     * 
+ * + *
+ * + * @param obj + * 可以是数组,字符串,枚举,集合,map,Java bean,Iterator等类型,内部自动识别转成{@link JSONArray}还是{@link JSONObject} + * @return 如果 obj 是null,返回 {@link StringUtils#EMPTY}
+ * @since 3.1.1 + */ + public static String toString(Object obj){ + return format(obj, 0, 0); + } + /** * 将对象格式化 成json字符串(排除指定名称的属性 excludes),并且 toString(4, 4) 输出. * diff --git a/feilong-json/src/test/java/com/feilong/json/format/ToStringTest.java b/feilong-json/src/test/java/com/feilong/json/format/ToStringTest.java new file mode 100644 index 000000000..2b51b9d08 --- /dev/null +++ b/feilong-json/src/test/java/com/feilong/json/format/ToStringTest.java @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2008 feilong + * + * 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 com.feilong.json.format; + +import org.junit.Test; + +import com.feilong.json.AbstractJsonTest; +import com.feilong.json.JsonUtil; + +public class ToStringTest extends AbstractJsonTest{ + + @Test + public void test1(){ + LOGGER.debug(JsonUtil.toString(USER)); + } + +}