將對象轉換成字符串,是很是經常使用的功能,尤爲在WEB應用中,使用 JSON lib 可以便捷地完成這項工做。JSON lib可以將Java對象轉成json格式的字符串,也能夠將Java對象轉換成xml格式的文檔,一樣能夠將json字符串轉換成Java對象或是將xml字符串轉換成Java對象。java
不管出於何種緣由,某些時候,咱們須要對對象轉爲字符串的過程加以控制,最多見需求如數值格式化和日期格式化。JSON lib提供了JsonConfig對象,該對象可以深入影響Java對象轉成json字符串的行爲。json
1. 第一種方式,實現JSONString接口的方法 數組
package cn.ysh.studio.test; import java.io.Serializable; import net.sf.json.JSONObject; import net.sf.json.JSONString; /** * * @author 楊勝寒 * @date 2013-6-27 * */ public class User implements JSONString, Serializable{ private static final long serialVersionUID = 1L; private long id; private String name; private String password; public User(){} public User(Long id, String name, String password){ this.id = id; this.name = name; this.password = password; } public User(String name, String password){ this.name = name; this.password = password; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toJSONString() { //return "{\"id\":" + this.id + ",\"name\":\"" + this.name + "\",\"password\":\""+ this.password +"\"}"; //忽略敏感字段password return "{\"id\":" + this.id + ",\"name\":\"" + this.name + "\"}"; } public static void main(String[] args) { User user = new User(12L, "JSON", "json"); System.out.println(JSONObject.fromObject(user).toString()); } }
2.第二種方式,經過jsonconfig實例,對包含和須要排除的屬性進行方便的添加或刪除 app
package cn.ysh.studio.test; import java.io.Serializable; import net.sf.json.JSONObject; import net.sf.json.JSONString; import net.sf.json.JsonConfig; /** * * @author 楊勝寒 * @date 2013-6-27 * */ public class User { private long id; private String name; private String password; public User(){} public User(Long id, String name, String password){ this.id = id; this.name = name; this.password = password; } public User(String name, String password){ this.name = name; this.password = password; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public static void main(String[] args) { JsonConfig config = new JsonConfig(); config.setExcludes( new String[]{"password"}); User user = new User(12L, "JSON", "json"); System.out.println(JSONObject.fromObject(user, config).toString()); } }
使用propertyFilter能夠容許同時對須要排除的屬性和類進行控制,這種控制還能夠是雙向的,也能夠應用到json字符串到java對象 ide
JsonConfig config = new JsonConfig(); config.setJsonPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object source/* 屬性的擁有者 */ , String name /*屬性名字*/ , Object value/* 屬性值 */) { return source instanceof User && "password".equalsIgnoreCase(name); } }); User user = new User(12L, "JSON", "json"); System.out.println(JSONObject.fromObject(user, config).toString());
相對於上面的何種方式,以下的方式或許更爲簡便: this
JsonConfig config = new JsonConfig(); config.registerPropertyExclusions(User.class, new String[]{"password"}); User user = new User(12L, "JSON", "json"); System.out.println(JSONObject.fromObject(user, config).toString());
JsonBeanProcessor和實現JsonString很相似,返回一個表明原來目標對象的合法JSONObjectspa
JsonConfig config = new JsonConfig(); config.registerJsonBeanProcessor(User.class, new JsonBeanProcessor() { @Override public JSONObject processBean(Object bean, JsonConfig config) { User user = (User) bean; return new JSONObject().element("id", user.getId()).element("name", user.getName()); } }); User user = new User(12L, "JSON", "json"); System.out.println(JSONObject.fromObject(user, config).toString());
好比咱們要控制JSON序列化過程當中的Date對象的格式化,以及數值的格式化,JsonValueProcessor是最好的選擇。.net
Map<String, Object> map = new HashMap<String, Object>(); map.put("date", new Date()); map.put("dates", Arrays.asList(new Date())); JsonConfig config = new JsonConfig(); config.registerJsonValueProcessor(Date.class, new JsonValueProcessor() { //自定義日期格式 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); @Override /** * 處理單個Date對象 */ public Object processObjectValue(String propertyName, Object date,JsonConfig config) { return simpleDateFormat.format(date); } @Override /** * 處理數組中的Date對象 */ public Object processArrayValue(Object date, JsonConfig config) { return simpleDateFormat.format(date); } }); System.out.println(JSONObject.fromObject(map, config).toString());
除了自定義日期格式外,還能夠如法炮製,控制數值格式化、HTML內容轉碼等。code