json-lib的普及率应该还是比较广的,不过在Spring中倒是倾向于jackson,无奈新项目做到一般才知道jackson,所以还是依旧使用了json-lib~
在序列化的时候,比较常见的问题是数据库为null的int型字段,被序列化后就赋值成0了,这在有些时候不是我们想看到的结果,那怎么办呢?
json-lib提供了JsonConfig对象供我们来自定义一些序列化时的操作:
jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultValueProcessor() { @Override public Object getDefaultValue(Class type) { return null; } });
也就是说,对于需要 序列化的对象的Integer的字段来说,只要值为null(也就是Integer的默认值),那么在序列化成json的时候,该key的value则也会为null
我们可以通过以下代码测试一下:
public static void main(String[] args) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerDefaultValueProcessor(Integer.class, new DefaultValueProcessor() { @SuppressWarnings("unchecked") @Override public Object getDefaultValue(Class type) { return ""; } }); Employee employee = new Employee(); employee.setName("zhu"); employee.setRole(1); System.out.println(JSONObject.fromObject(employee)); System.out.println(JSONObject.fromObject(employee, jsonConfig)); }
输出为:
{"depno":"","empno":"","groupno":0,"name":"zhu","online":0,"phone":"","role":1} {"depno":"","empno":"","groupno":null,"name":"zhu","online":null,"phone":"","role":1}
就拿Date类型来说吧,如果说按照默认的序列化策略,看看会是什么结果:
{"birthday":{"date":24,"day":3,"hours":11,"minutes":33,"month":11,"seconds":4,"time":1419391984356,"timezoneOffset":-480,"year":114}}
我想大多数情况下你应该不会是想要这种结果吧?格式化成“yyyy-MM-dd”这种格式也许是最常见的
那么我们可以通过为Date类型的字段配置一个我们自己的Processor:
首先,我们需要实现JsonValueProcessor接口,这个接口有两个方法需要实现:
<pre name="code" class="java">public Object processObjectValue(String paramString,Object paramObject, JsonConfig paramJsonConfig); public Object processArrayValue(Object paramObject,JsonConfig paramJsonConfig);
当对象的某个属性是该类型时,那么调用processObjectValue(...)方法来序列化该属性
而当对象的某个属性是该类型的数组或List时,那么调用processArrayValue(...)方法来序列化该属性
知道了这个,下面我们就来实现序列化中Date类型的格式化吧,这里对于Date的数组类型就不处理了,直接返回null:
public static void main(String[] args) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonValueProcessor(Date.class, new JsonValueProcessor() { /** * paramString -> 参数名 paramObject -> 参数值 */ @Override public Object processObjectValue(String paramString, Object paramObject, JsonConfig paramJsonConfig) { if (paramObject == null) { return null; } String ret = null; try { SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss"); ret = format.format((Date) paramObject); } catch (Exception e) { SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd"); ret = format.format((Date) paramObject); } return ret; } @Override public Object processArrayValue(Object paramObject, JsonConfig paramJsonConfig) { return null; } }); Employee employee = new Employee(); employee.setName("zhu"); employee.setRole(1); employee.setBirthday(new Date()); System.out.println("no jsonConfig:" + JSONObject.fromObject(employee)); System.out.println("with jsonConfig:" + JSONObject.fromObject(employee, jsonConfig)); }
no jsonConfig:{"birthday":{"date":24,"day":3,"hours":13,"minutes":46,"month":11,"seconds":31,"time":1419399991491,"timezoneOffset":-480,"year":114},"depno":"","empno":"","groupno":0,"name":"zhu","online":0,"phone":"","role":1} with jsonConfig:{"birthday":"2014-12-24 13:46:31","depno":"","empno":"","groupno":0,"name":"zhu","online":0,"phone":"","role":1}
public static void main(String[] args) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter(new PropertyFilter() { @Override public boolean apply(Object source, String name, Object value) { return value == null; } }); Employee employee = new Employee(); employee.setName("zhu"); employee.setRole(1); System.out.println("no jsonConfig:" + JSONObject.fromObject(employee)); System.out.println("with jsonConfig:" + JSONObject.fromObject(employee, jsonConfig)); }
输出结果是:
no jsonConfig:{"birthday":null,"depno":"","empno":"","groupno":0,"name":"zhu","online":0,"phone":"","role":1} with jsonConfig:{"name":"zhu","role":1}
第二种过滤方式,直接根据属性的名称:
public static void main(String[] args) { JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[] { "depno", "empno", "groupno", "online" }); Employee employee = new Employee(); employee.setName("zhu"); employee.setRole(1); System.out.println("no jsonConfig:" + JSONObject.fromObject(employee)); System.out.println("with jsonConfig:" + JSONObject.fromObject(employee, jsonConfig)); }
可以看到,这里通过jsonConfig.setExcludes(new String[]{"double", "boolean"});将double和boolean这两个属性
输出结果:
no jsonConfig:{"birthday":null,"depno":"","empno":"","groupno":0,"name":"zhu","online":0,"phone":"","role":1} with jsonConfig:{"birthday":null,"name":"zhu","phone":"","role":1}