All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.gdface.facelog.ExceptionFieldTransformer Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package net.gdface.facelog;

import java.beans.PropertyDescriptor;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Throwables;
import com.google.common.collect.Maps;

import net.gdface.facelog.ServiceSecurityException.FieldJsonTransformer;
import net.gdface.utils.BeanPropertyUtils;

/**
 * 实现{@link FieldJsonTransformer}接口,将异常类型自定义字段转为json
 * @author guyadong
 *
 * @param 
 */
public class ExceptionFieldTransformer implements FieldJsonTransformer {
	/** 指定生成的json中field不带引号 */
	private static final int NO_FIELD_QUOTE_FEATURE=SerializerFeature.config(
			JSON.DEFAULT_GENERATE_FEATURE, SerializerFeature.QuoteFieldNames, false);
	public ExceptionFieldTransformer() {
	}

	@Override
	public String jsonOfDeclaredFields(final T input) {
		Map fields = BeanPropertyUtils.getProperties(input.getClass(), 3, true);
		fields = Maps.filterValues(fields, new Predicate() {
			@Override
			public boolean apply(PropertyDescriptor descriptor) {
				return descriptor.getWriteMethod().getDeclaringClass().equals(input.getClass());
			}
		});
		Map map = Maps.transformValues(fields, new Function(){

			@Override
			public Object apply(PropertyDescriptor descriptor) {
				try {
					return descriptor.getReadMethod().invoke(input);
				} catch (Exception e) {
					Throwables.throwIfUnchecked(e);
					throw new RuntimeException(e);
				}
			}});
		return JSON.toJSONString(
				map,
				NO_FIELD_QUOTE_FEATURE);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy