json-lib 常用功能
將對象轉換成字符串,是非常常用的功能,尤其在WEB應用中,使用 JSON lib 能夠便捷地完成這項工作。JSON lib能夠將Java對象轉成json格式的字符串,也可以將Java對象轉換成xml格式的文檔,同樣可以將json字符串轉換成Java對象或是將xml字符串轉換成Java對象。
無論出于何種原因,某些時候,我們需要對對象轉為字符串的過程加以控制,最常見需求如數值格式化和日期格式化。JSON lib提供了JsonConfig對象,該對象能夠深刻影響Java對象轉成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實例,對包含和需要排除的屬性進行方便的添加或刪除
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對象
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());
相對于上面的何種方式,如下的方式或許更為簡便:
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
JsonBeanProcessor和實現JsonString很類似,返回一個代表原來目標對象的合法JSONObject
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());
自定義JsonValueProcessor
比如我們要控制JSON序列化過程中的Date對象的格式化,以及數值的格式化,JsonValueProcessor是最好的選擇。
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內容轉碼等。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!