Json系列之四 揭开JsonConfig的神秘面纱 java to json

//揭开JsonConfig的神秘面纱,for bean to json JsonConfig jsonConfig = new JsonConfig(); //忽略掉bean中含后某个注解的field,不转换成json,可以多次增加不同注解 //jsonConfig.addIgnoreFieldAnnotation(Person.class);//一定是注解的类,我这里没有例子,,大家可以自己做 //同上 //jsonConfig.addIgnoreFieldAnnotation("person"); //把某种类型的class,转换成自定义的结果 jsonConfig.registerDefaultValueProcessor(Address.class, new DefaultValueProcessor() {@Overridepublic Object getDefaultValue(Class type) {return null;} }); //返回成自己设定的jsonObject// jsonConfig.registerJsonBeanProcessor(Person.class, new JsonBeanProcessor() {////@Override//public JSONObject processBean(Object bean, JsonConfig jsonConfig) {////return null;//}//});//jsonConfig.registerPropertyNameProcessor(target, propertyNameProcessor);//被registerJsonPropertyNameProcessor取代//在到json层的时候的转换,比上面javapropertyName靠后执行 jsonConfig.registerJsonPropertyNameProcessor(Person.class, new PropertyNameProcessor() {@Overridepublic String processPropertyName(Class beanClass, String name) {if(name.equals("name")){return "nameJson";}return name;} }); //在json层转换的 jsonConfig.registerJsonValueProcessor(String.class, new JsonValueProcessor() {@Overridepublic Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {if(key.equals("emptyStr")){return "asdfa";}return value;}@Overridepublic Object processArrayValue(Object value, JsonConfig jsonConfig) {// TODO Auto-generated method stubreturn null;} }); jsonConfig.registerJsonValueProcessor("nullStr", new JsonValueProcessor() {@Overridepublic Object processObjectValue(String key, Object value,JsonConfig jsonConfig) {return "nullStr2";}@Overridepublic Object processArrayValue(Object value, JsonConfig jsonConfig) {return null;}});//jsonConfig.registerJsonValueProcessor(beanClass, propertyType, jsonValueProcessor);//更加细化//jsonConfig.registerJsonValueProcessor(beanClass, key, jsonValueProcessor);//更加细化//剔除哪个属性jsonConfig.registerPropertyExclusion(Person.class, "sameTest");//剔除哪些属性//jsonConfig.registerPropertyExclusions(target, properties);//是否允许空jsonConfig.setAllowNonStringKeys(true);//内部如果有嵌套引用时候,如何处理 DEFAULT_CYCLE_DETECTION_STRATEGY = CycleDetectionStrategy.STRICT;//jsonConfig.setCycleDetectionStrategy(cycleDetectionStrategy);//可以实现吧A class 转换成B Class//jsonConfig.setDefaultValueProcessorMatcher(new DefaultValueProcessorMatcher() {////@Override//public Object getMatch(Class target, Set set) {////return B.class//return null;//}//});//忽略掉哪些属性//jsonConfig.setExcludes(excludes);//jsonConfig.setIgnoreDefaultExcludes(ignoreDefaultExcludes);//jsonConfig.setIgnoreJPATransient(ignoreJPATransient);//jsonConfig.setIgnorePublicFields(ignorePublicFields);//jsonConfig.setIgnoreTransientFields(ignoreTransientFields);//jsonConfig.setJsonValueProcessorMatcher(jsonValueProcessorMatcher);//jsonConfig.setJsonPropertyNameProcessorMatcher(propertyNameProcessorMatcher);jsonConfig.setJsonPropertyFilter(new PropertyFilter() {@Overridepublic boolean apply(Object source, String name, Object value) {// TODO Auto-generated method stubreturn false;}});//jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);//jsonConfig.setJavascriptCompliant(javascriptCompliant);System.out.println(format(JSONObject.fromObject(p, jsonConfig).toString()));

望着它们,我睡着了。今天已经过去——我生命中所有天中的一天,

Json系列之四 揭开JsonConfig的神秘面纱 java to json

相关文章:

你感兴趣的文章:

标签云: