Json系列之四 揭開JsonConfig的神秘面紗 java to json

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.