json-lib的普及率应该还是比较广的,不过在spring中倒是倾向于jackson,无奈新项目做到一般才知道jackson,所以还是依旧使用了json-lib~
改变某种类型默认值的序列化值
在序列化的时候,比较常见的问题是数据库为null的int型字段,被序列化后就赋值成0了,这在有些时候不是我们想看到的结果,那怎么办呢?
json-lib提供了JsonConfig对象供我们来自定义一些序列化时的操作:
- jsonConfig.registerDefaultValueProcessor(Integer.class,
- newDefaultValueProcessor(){
- @Override
- publicObjectgetDefaultValue(Classtype){
- returnnull;
- }
- });
也就是说,对于需要 序列化的对象的Integer的字段来说,只要值为null(也就是Integer的默认值),那么在序列化成json的时候,该key的value则也会为null
- publicstaticvoidmain(String[]args){
- JsonConfigjsonConfig=newJsonConfig();
- jsonConfig.registerDefaultValueProcessor(Integer.class,
- newDefaultValueProcessor(){
- @SuppressWarnings("unchecked")
- @Override
- publicObjectgetDefaultValue(Classtype){
- return"";
- }
- });
- Employeeemployee=newEmployee();
- 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":"","groupno":null,"online":null,"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接口,这个接口有两个方法需要实现:
- <prename="code"class="java">publicObjectprocessObjectValue(StringparamString,ObjectparamObject,JsonConfigparamJsonConfig);
- publicObjectprocessArrayValue(ObjectparamObject,JsonConfigparamJsonConfig);
而当对象的某个属性是该类型的数组或List时,那么调用processArrayValue(...)方法来序列化该属性
知道了这个,下面我们就来实现序列化中Date类型的格式化吧,这里对于Date的数组类型就不处理了,直接返回null:
- publicstaticvoidmain(String[]args){
- JsonConfigjsonConfig=newJsonConfig();
- jsonConfig.registerJsonValueProcessor(Date.class,
- newJsonValueProcessor(){
- /**
- *paramString->参数名paramObject->参数值
- */
- @Override
- publicObjectprocessObjectValue(StringparamString,
- ObjectparamObject,JsonConfigparamJsonConfig){
- if(paramObject==null){
- returnnull;
- }
- Stringret=null;
- try{
- SimpleDateFormatformat=newSimpleDateFormat(
- "yyyy-MM-ddHH:mm:ss");
- ret=format.format((Date)paramObject);
- }catch(Exceptione){
- SimpleDateFormatformat=newSimpleDateFormat(
- "yyyy-MM-dd");
- ret=format.format((Date)paramObject);
- }
- returnret;
- }
- @Override
- publicObjectprocessArrayValue(ObjectparamObject,
- JsonConfigparamJsonConfig){
- returnnull;
- }
- });
- Employeeemployee=newEmployee();
- employee.setName("zhu");
- employee.setRole(1);
- employee.setBirthday(newDate());
- System.out.println("nojsonConfig:"+JSONObject.fromObject(employee));
- System.out.println("withjsonConfig:"+JSONObject.fromObject(employee,jsonConfig));
- }
输出结果是:
- nojsonConfig:{"birthday":{"date":24,"hours":13,"minutes":46,"seconds":31,"time":1419399991491,"year":114},"depno":"","role":1}
- withjsonConfig:{"birthday":"2014-12-2413:46:31","role":1}
过滤需要序列化的属性
- publicstaticvoidmain(String[]args){
- JsonConfigjsonConfig=newJsonConfig();
- jsonConfig.setJsonPropertyFilter(newPropertyFilter(){
- @Override
- publicbooleanapply(Objectsource,Stringname,Objectvalue){
- returnvalue==null;
- }
- });
- Employeeemployee=newEmployee();
- employee.setName("zhu");
- employee.setRole(1);
- System.out.println("nojsonConfig:"+JSONObject.fromObject(employee));
- System.out.println("withjsonConfig:"+JSONObject.fromObject(employee,jsonConfig));
- }
输出结果是:
- nojsonConfig:{"birthday":null,"role":1}
- withjsonConfig:{"name":"zhu","role":1}
- publicstaticvoidmain(String[]args){
- JsonConfigjsonConfig=newJsonConfig();
- jsonConfig.setExcludes(newString[]{"depno","empno","groupno",
- "online"});
- Employeeemployee=newEmployee();
- employee.setName("zhu");
- employee.setRole(1);
- System.out.println("nojsonConfig:"+JSONObject.fromObject(employee));
- System.out.println("withjsonConfig:"
- +JSONObject.fromObject(employee,jsonConfig));
- }
可以看到,这里通过jsonConfig.setExcludes(new String[]{"double","boolean"});将double和boolean这两个属性
输出结果:
- nojsonConfig:{"birthday":null,"role":1}
- withjsonConfig:{"birthday":null,"role":1}