Jsonconfig 设置属性删除,JsonValueProcessor 自定义格式化时间格式

将对象转换成字符串,是非常常用的功能,尤其在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;

/**
 * 
 *
 */
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;

/**
 * 
 *
 */
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内容转码等。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值