com.alibaba.fastjson.serializer.ValueFilter Java Examples

The following examples show how to use com.alibaba.fastjson.serializer.ValueFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FastjsonConfig.java    From spring-boot-demo-all with Apache License 2.0 6 votes vote down vote up
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteClassName,
            SerializerFeature.WriteMapNullValue
    );
    List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);

    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    ValueFilter valueFilter = new ValueFilter() {
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);
    converter.setSupportedMediaTypes(fastMediaTypes);
    converter.setFastJsonConfig(fastJsonConfig);
    return converter;
}
 
Example #2
Source File: HttpMessageConverterAutoConfiguration.java    From api-boot with Apache License 2.0 6 votes vote down vote up
/**
 * 获取项目中定义的ValueFilter实现类列表
 * 通过BeanFactory读取本项目的Base Package List
 *
 * @return ValueFilter数组
 */
ValueFilter[] getDefineFilters(List<String> packages) {
    Set<Class> filterClass = new HashSet<>();
    if (ObjectUtils.isEmpty(packages)) {
        return new ValueFilter[]{};
    }
    // 读取所有package下的ValueFilter实现类
    packages.stream().forEach(pack -> filterClass.addAll((Collection<? extends Class>) ClassTools.getSubClassList(pack, ValueFilter.class)));
    List<ValueFilter> filters = new LinkedList<>();
    filterClass.stream().forEach(filter -> {
        try {
            filters.add((ValueFilter) filter.newInstance());
        } catch (Exception e) {
            logger.error("ValueFilter new instance have error.", e);
        }
    });
    logger.info("Loaded ValueFilter : {}", filterClass.toString());
    return filters.toArray(new ValueFilter[]{});
}
 
Example #3
Source File: MetaUtils.java    From hermes with Apache License 2.0 6 votes vote down vote up
public static String filterSensitiveField(Meta meta) {
	final Storage storage = meta.findStorage(Storage.MYSQL);

	return JSON.toJSONString(meta, JSON_CFG, new ValueFilter() {
		@Override
		@SuppressWarnings("unchecked")
		public Object process(Object owner, String fieldName, Object fieldValue) {
			if (storage != null && owner instanceof Datasource && fieldValue instanceof Map) {
				for (Datasource d : storage.getDatasources()) {
					if (d.getId().equals(((Datasource) owner).getId())) {
						Map<String, Property> newFieldValue = new HashMap<String, Property>();
						for (Entry<String, Property> entry : ((Map<String, Property>) fieldValue).entrySet()) {
							newFieldValue.put(entry.getKey(), new Property(entry.getKey()).setValue("**********"));
						}
						return newFieldValue;
					}
				}
			}
			return fieldValue;
		}
	});
}
 
Example #4
Source File: WebConfig.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteDateUseDateFormat);
    fastJsonConfig.setSerializeFilters(new ValueFilter() {
        @Override
        public Object process(Object o, String s, Object source) {
            if (source == null) {
                return "";
            }
            if (source instanceof Date) {
                return ((Date) source).getTime();
            }
            return source;
        }
    });
    httpMessageConverter.setFastJsonConfig(fastJsonConfig);
    return httpMessageConverter;
}
 
Example #5
Source File: WebMvcConfig.java    From spring-boot-cookbook with Apache License 2.0 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteDateUseDateFormat);
    fastJsonConfig.setSerializeFilters(new ValueFilter() {
        @Override
        public Object process(Object o, String s, Object source) {
            if (source == null) {
                return "";
            }
            if (source instanceof Date) {
                return ((Date) source).getTime();
            }
            return source;
        }
    });
    httpMessageConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(httpMessageConverter);
}
 
Example #6
Source File: DefaultFastjsonConfig.java    From MeetingFilm with Apache License 2.0 5 votes vote down vote up
/**
 * fastjson的配置
 */
public FastJsonConfig fastjsonConfig() {
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteEnumUsingToString
    );
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    ValueFilter valueFilter = new ValueFilter() {
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setCharset(Charset.forName("utf-8"));
    fastJsonConfig.setSerializeFilters(valueFilter);

    //解决Long转json精度丢失的问题
    SerializeConfig serializeConfig = SerializeConfig.globalInstance;
    serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
    serializeConfig.put(Long.class, ToStringSerializer.instance);
    serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
    fastJsonConfig.setSerializeConfig(serializeConfig);
    return fastJsonConfig;
}
 
Example #7
Source File: DefaultFastjsonConfig.java    From flash-waimai with MIT License 5 votes vote down vote up
/**
 * fastjson的配置
 */
public FastJsonConfig fastjsonConfig() {
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteEnumUsingToString,
            SerializerFeature.DisableCircularReferenceDetect
    );
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    ValueFilter valueFilter = new ValueFilter() {
        @Override
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setCharset(Charset.forName("utf-8"));
    fastJsonConfig.setSerializeFilters(valueFilter);

    //解决Long转json精度丢失的问题
    SerializeConfig serializeConfig = SerializeConfig.globalInstance;
    serializeConfig.put(BigInteger.class, ToStringSerializer.instance);
    serializeConfig.put(Long.class, ToStringSerializer.instance);
    serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
    fastJsonConfig.setSerializeConfig(serializeConfig);
    return fastJsonConfig;
}
 
Example #8
Source File: FastJsonHttpMessageConvertersConfiguration.java    From wenku with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean({ FastJsonHttpMessageConverter4.class })
//当没有注册这个类时,自动注册Bean
public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter() {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();

    //使用最新的官方推荐配置对象的方式来注入fastjson的序列化特征
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.WriteNullStringAsEmpty
     );
    
    //添加对json值的过滤,因为像移动APP,服务端在传json值时最好不要传null,而是使用“”,这是一个演示
    ValueFilter valueFilter = new ValueFilter() {//5
        //o 是class
        //s 是key值
        //o1 是value值
        public Object process(Object o, String s, Object o1) {
            if (null == o1) {
                o1 = "";
            }
            return o1;
        }
    };
    fastJsonConfig.setSerializeFilters(valueFilter);

    converter.setFastJsonConfig(fastJsonConfig);

    return converter;
}
 
Example #9
Source File: FastJsonProvider.java    From metrics with Apache License 2.0 4 votes vote down vote up
public String toJSONString(Object object, SerializeFilter filter, SerializerFeature[] features) {
    SerializeWriter out = new SerializeWriter();

    try {
        JSONSerializer serializer = new JSONSerializer(out, fastJsonConfig.serializeConfig);
        if (features != null) {
            for (SerializerFeature feature : features) {
                serializer.config(feature, true);
            }
        }

        if (filter != null) {
            if (filter instanceof PropertyPreFilter) {
                serializer.getPropertyPreFilters().add((PropertyPreFilter) filter);
            }

            if (filter instanceof NameFilter) {
                serializer.getNameFilters().add((NameFilter) filter);
            }

            if (filter instanceof ValueFilter) {
                serializer.getValueFilters().add((ValueFilter) filter);
            }

            if (filter instanceof PropertyFilter) {
                serializer.getPropertyFilters().add((PropertyFilter) filter);
            }

            if (filter instanceof BeforeFilter) {
                serializer.getBeforeFilters().add((BeforeFilter) filter);
            }

            if (filter instanceof AfterFilter) {
                serializer.getAfterFilters().add((AfterFilter) filter);
            }
        }

        serializer.write(object);

        return out.toString();
    } finally {
        out.close();
    }
}