通过ajax请求action,hibernate采用懒加载(不立即加载),查出的数据在json序列化时,懒加载是挡不住的,,,使得json序列化的数据过多,报异常堆不足,,现在建一个pojo,用没用映射的实体类去封装数据,json序列化现在建的pojo能够搞定问题,,,但是,我不想通过这个方法搞定,由于学习ajax时间不长,对json的插件不是很熟,我该怎么操作json的插件来搞定上面的问题,让json序列化时只序列化我想要的求教!ajaxjson
调试欢乐多
* Json配置按类对象映射属性过滤器.
*
* @author MaYichao
* @since 1.0
*/
public class ClassMappingPropertyFilter extends MappingPropertyFilter { @Override
protected boolean keyMatches(Object key, Object source, String name, Object value) {
Class c = (Class) key;
return c.isInstance(source);
} @Override
public boolean apply(Object source, String name, Object value) {
// if (name.equals("imagePath")) {
// return true;
// }
if (value == null || value instanceof JSONNull) {
return true;
} return super.apply(source, name, value);
}
}
/**
* Json配置包含属性过滤器.
*
* @author MaYichao
* @since 1.0
*/
public class IncludePropertyFilter implements PropertyFilter { /**
* 需要包含的属性列表.
*/
private List<String> properties = null;
/**
* 指定需要处理的类. 如果指定了类型,则只会排除指定类型的属性.
*/
private Class clazz = null; public IncludePropertyFilter() {
} public IncludePropertyFilter(String... properties) {
this(null, properties);
} public IncludePropertyFilter(Class clazz, String... properties) {
init(Arrays.asList(properties), clazz);
} public IncludePropertyFilter(List<String> propertylist, Class clazz) {
init(propertylist, clazz);
} private void init(List<String> propertylist, Class clazz) {
properties = propertylist;
this.clazz = clazz;
} public boolean apply(Object source, String name, Object value) {
if (properties == null || properties.isEmpty()) {
return true;
}
boolean b = value == null || !((clazz == null || clazz.isInstance(source)) && properties.contains(name));
// if (b == false) {
// System.out.println("===" + source.getClass() + "." + name);
// }
return b;
} /**
* 需要排除的属性列表.
*
* @return the exclude
*/
public List<String> getExclude() {
return properties;
} /**
* 需要排除的属性列表.
*
* @param exclude the exclude to set
*/
public void setExclude(List<String> exclude) {
this.properties = exclude;
} /**
* 指定需要处理的类. 如果指定了类型,则只会排除指定类型的属性.
*
* @return the clazz
*/
public Class getClazz() {
return clazz;
} /**
* 指定需要处理的类. 如果指定了类型,则只会排除指定类型的属性.
*
* @param clazz the clazz to set
*/
public void setClazz(Class clazz) {
this.clazz = clazz;
}
} private JsonConfig getJSONConfig() {
JsonConfig jc = new JsonConfig();
ClassMappingPropertyFilter filter = new ClassMappingPropertyFilter();
filter.addPropertyFilter(ExamItem.class, new IncludePropertyFilter(
"id", "content", "answer", "optionCount", "imagePath", "type",
"waitTime", "sortNumber", "memberCount", "passedCount","pkCount"));
jc.registerJsonValueProcessor(java.util.Date.class,
new DateJsonValueProcessor("yyyy-MM-dd HH:mm:ss"));
jc.setJsonPropertyFilter(filter);
return jc;
}