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() {
			
			@Override
			public 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() {
			
			@Override
			public String processPropertyName(Class beanClass, String name) {
				if(name.equals("name")){
					return "nameJson";
				}
				return name;
			}
		 });
		 //在json层转换的
		 jsonConfig.registerJsonValueProcessor(String.class, new JsonValueProcessor() {
			
			@Override
			public Object processObjectValue(String key, Object value,
					JsonConfig jsonConfig) {
				if(key.equals("emptyStr")){
					
					return "asdfa";
				}
				return value;
			}
			
			@Override
			public Object processArrayValue(Object value, JsonConfig jsonConfig) {
				// TODO Auto-generated method stub
				return null;
			}
		 });
		 jsonConfig.registerJsonValueProcessor("nullStr", new JsonValueProcessor() {
			
			@Override
			public Object processObjectValue(String key, Object value,
					JsonConfig jsonConfig) {
				
				return "nullStr2";
			}
			
			@Override
			public 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() {
			
			@Override
			public boolean apply(Object source, String name, Object value) {
				// TODO Auto-generated method stub
				return false;
			}
		});
		//jsonConfig.setJsonBeanProcessorMatcher(jsonBeanProcessorMatcher);
		//jsonConfig.setJavascriptCompliant(javascriptCompliant);
		
		System.out.println(format(JSONObject.fromObject(p, jsonConfig).toString()));

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hassen2010

你的鼓励我能输出跟多的好文章

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值