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()));