Json-lib 与 hibernate _lazy共同使用的问题
原文http://ericyou.iteye.com/blog/325246
hibernate使用CGLIB把POJO的domain对象动态代理,实现它的魔法,但是给JSON的序列化带来了麻烦,因为JSON无法对lazy的属性进行序列化。有以下的四个方法可以解决hibernate的序列化问题
domain类实现JSONString接口
建立JsonConfig实例,并配置属性排除列表
用属性过滤器
写一个自定义的JsonBeanProcessor
1.实现JSONString接口是侵入性最强的方法
public class Person implements JSONString { private String name; private String lastname; private Address address; // getters & setters public String toJSONString() { return "{name:'"+name+"',lastname:'"+lastname+"'}"; } }
2.第二种方法通过jsonconfig实例,对包含和需要排除的属性进行方便添加删除
public class Person { private String name; private String lastname; private Address address; // getters & setters } JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExclusions( new String[]{ "address" } ); Person bean = /* initialize */; JSON json = JSONSerializer.toJSON( bean, jsonConfig );
注意:这种方法不区分目标类,就是说如果有2个bean当中都存在“address”属性,那么采用这种方法,这两个bean中的address属性都将被排除
3.使用propertyFilter可以允许同时对需要排除的属性和类进行控制,这种控制还可以是双向的,也可以应用到json字符串到java对象
public class Person { private String name; private String lastname; private Address address; // getters & setters } JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setJsonPropertyFilter( new PropertyFilter(){ public boolean apply( Object source, String name, Object value ){ // return true to skip name return source instanceof Person && name.equals("address"); } }); Person bean = /* initialize */; JSON json = JSONSerializer.toJSON( bean, jsonConfig )
4.最后来看JsonBeanProcessor,这种方式和实现JsonString很类似,返回一个代表原来的domain类的合法JSONOBJECT
public class Person { private String name; private String lastname; private Address address; // getters & setters } JsonConfig jsonConfig = new JsonConfig(); jsonConfig.registerJsonBeanProcessor( Person.class, new JsonBeanProcessor(){ public JSONObject processBean( Object bean, JsonConfig jsonConfig ){ if( !(bean instanceof Person) ){ return new JSONObject(true); } Person person = (Person) bean; return new JSONObject() .element( "name", person.getName() ) .element( "lastname", person.getLastname() ); } }); Person bean = /* initialize */; JSON json = JSONSerializer.toJSON( bean, jsonConfig );
AnditisherewithJsonBeanProcessorswhereweencounterthesecondgotcharelatedtoCGlib.AsyounoticedweregisteredtheprocessorusingPersonastargetclass,butHibernatereturnsaProxythatisnotexactlyaPersonbutasubclass(ifpossible)ofPerson.ThedefaultmatchingalgorithmusedbyJson-libwilltrytomatchtheexactclass(usingequals),meaningthatyourprocessorwillnotbecalledwhenyouexpectittobe,thisiswhyJsonBeanProcessorMatcherwasintroduced,nowyouarebeabletowritethefollowingcode
<textareaclass="java:nocontrols:nogutter"cols="80"rows="34"name="srccode">publicclassPerson{privateStringname;privateStringlastname;privateAddressaddress;//getters&setters}JsonConfigjsonConfig=newJsonConfig();jsonConfig.registerJsonBeanProcessor(Person.class,newJsonBeanProcessor(){publicJSONObjectprocessBean(Objectbean,JsonConfigjsonConfig){if(!(beaninstanceofPerson)){returnnewJSONObject(true);}Personperson=(Person)bean;returnnewJSONObject().element("name",person.getName()).element("lastname",person.getLastname());}});jsonConfig.registerJsonBeanProcessorMatcher(newJsonBeanProcessorMatcher(){publicObjectgetMatch(Classtarget,Set/*<Class>*/matches){for(Objectmatch:matches){if(((Class)match).isAssignableFrom(target)){returnmatch;}}returnnull;}});Personbean=/*initialize*/;JSONjson=JSONSerializer.toJSON(bean,jsonConfig);</textarea>
Ihopetheseexampleshelpdispelsomedoubts,inanycasepleasefeelfreetodropbyJson-lib'sforumandmailinglists.