Skip to content

Commit

Permalink
新建个方法来简化 JsonUtil.format(xqlConfig, 0, 0) fix #367
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed Jul 3, 2022
1 parent b541032 commit 2e36ff5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
51 changes: 51 additions & 0 deletions feilong-json/src/main/java/com/feilong/json/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,57 @@ public static <K, V> String formatSimpleMap(Map<K, V> inputMap,Class<?>...allowF

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

/**
* 将对象格式化成json字符串,并且 toString(0, 0) 输出.
*
* <p>
* =com.feilong.json.JsonUtil.format(Object, 0, 0)
* </p>
*
* <h3>示例:</h3>
*
* <blockquote>
*
* <pre class="code">
*
* 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));
*
* </pre>
*
* <b>返回:</b>
*
* <pre class="code">
{"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":[]}
* </pre>
*
* </blockquote>
*
* @param obj
* 可以是数组,字符串,枚举,集合,map,Java bean,Iterator等类型,内部自动识别转成{@link JSONArray}还是{@link JSONObject}
* @return 如果 <code>obj</code> 是null,返回 {@link StringUtils#EMPTY}<br>
* @since 3.1.1
*/
public static String toString(Object obj){
return format(obj, 0, 0);
}

/**
* 将对象格式化 成json字符串(<b>排除</b>指定名称的属性 <code>excludes</code>),并且 toString(4, 4) 输出.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}

}

0 comments on commit 2e36ff5

Please sign in to comment.