Java Code Examples for com.alibaba.fastjson.support.config.FastJsonConfig#setDateFormat()

The following examples show how to use com.alibaba.fastjson.support.config.FastJsonConfig#setDateFormat() . 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: WebMvcConfig.java    From yue-library with Apache License 2.0 6 votes vote down vote up
/**
 * <p>使用FastJson优先于默认的Jackson做json解析
 * <p>https://github.com/alibaba/fastjson/wiki/%E5%9C%A8-Spring-%E4%B8%AD%E9%9B%86%E6%88%90-Fastjson
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	// 1. 确认是否启用FastJsonHttpMessageConverter
	if (!webProperties.isEnabledFastJsonHttpMessageConverter()) {
		return;
	}
	
	// 2. 配置FastJsonHttpMessageConverter
	FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
	FastJsonConfig config = new FastJsonConfig();
	config.setDateFormat(JSON.DEFFAULT_DATE_FORMAT);
	config.setSerializerFeatures(
			SerializerFeature.PrettyFormat, 
			SerializerFeature.BrowserCompatible, 
			SerializerFeature.WriteMapNullValue, 
			SerializerFeature.WriteNullBooleanAsFalse,
			SerializerFeature.WriteNullListAsEmpty, 
			SerializerFeature.WriteNullNumberAsZero,
			SerializerFeature.WriteNullStringAsEmpty
	);
	converter.setFastJsonConfig(config);
	converters.add(0, converter);
	
	log.info("【初始化配置-FastJsonHttpMessageConverter】默认配置为false,当前环境为true:使用FastJson优先于默认的Jackson做json解析 ... 已初始化完毕。");
}
 
Example 3
Source File: FastJsonConfiguration.java    From momo-cloud-permission with Apache License 2.0 6 votes vote down vote up
@Bean
    public HttpMessageConverters fastJsonConfigure(){
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
        //日期格式化
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //修改配置返回内容的过滤
        fastJsonConfig.setSerializerFeatures(
//                SerializerFeature. WriteMapNullValue,//是否输出值为null的字段,默认为false
                SerializerFeature.WriteDateUseDateFormat//格式化标签
//        		SerializerFeature.WriteNullListAsEmpty  ,//List字段如果为null,输出为[],而非null
//                SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
//                SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false。
//                SerializerFeature.WriteNullStringAsEmpty//字符类型字段如果为null,输出为"",而非null
//                SerializerFeature.WriteNullBooleanAsFalse//Boolean字段如果为null,输出为false,而非null
        );
        converter.setFastJsonConfig(fastJsonConfig);
        //处理中文乱码问题
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        converter.setSupportedMediaTypes(fastMediaTypes);
        converter.setFastJsonConfig(fastJsonConfig);
        //将fastjson添加到视图消息转换器列表内
        return new HttpMessageConverters(converter);
    }
 
Example 4
Source File: FastJsonConfiguration.java    From momo-cloud-permission with Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastJsonConfigure(){
    FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    //日期格式化
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    //修改配置返回内容的过滤
    fastJsonConfig.setSerializerFeatures(
            SerializerFeature. WriteMapNullValue,//是否输出值为null的字段,默认为false
            SerializerFeature.WriteDateUseDateFormat,//格式化标签
    		SerializerFeature.WriteNullListAsEmpty , //List字段如果为null,输出为[],而非null
            SerializerFeature.DisableCircularReferenceDetect,//消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
            SerializerFeature.WriteMapNullValue,//是否输出值为null的字段,默认为false。
            SerializerFeature.WriteNullStringAsEmpty,//字符类型字段如果为null,输出为"",而非null
            SerializerFeature.WriteNullBooleanAsFalse//Boolean字段如果为null,输出为false,而非null
    );
    converter.setFastJsonConfig(fastJsonConfig);
    //处理中文乱码问题
    List<MediaType> fastMediaTypes = new ArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    converter.setSupportedMediaTypes(fastMediaTypes);
    converter.setFastJsonConfig(fastJsonConfig);
    //将fastjson添加到视图消息转换器列表内
    return new HttpMessageConverters(converter);
}
 
Example 5
Source File: WebMvcConfigurer.java    From mysiteforme with Apache License 2.0 6 votes vote down vote up
/**
 *  fastjson序列化
 *
 * */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    List<MediaType> supportedMediaTypes = new ArrayList<>();
    supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");    // 自定义时间格式
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.DisableCircularReferenceDetect,
            SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullBooleanAsFalse,SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullListAsEmpty,SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.BrowserCompatible,SerializerFeature.WriteNonStringKeyAsString);
    converters.add(fastJsonHttpMessageConverter);
    converters.add(responseBodyConverter());
}
 
Example 6
Source File: FastjsonConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setSerializerFeatures(serializerFeatures);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 7
Source File: FastjsonConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setSerializerFeatures(serializerFeatures);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 8
Source File: FastjsonConfiguration.java    From seed with Apache License 2.0 6 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonConfig.setSerializerFeatures(serializerFeatures);
    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}
 
Example 9
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 10
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 11
Source File: FastJsonConfiguration.java    From flower with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  FastJsonConfig fastJsonConfig = new FastJsonConfig();
  fastJsonConfig.setSerializerFeatures(
      SerializerFeature.DisableCircularReferenceDetect, // SerializerFeature.WriteMapNullValue,
      SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty,
      SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.PrettyFormat);
  fastJsonConfig.setCharset(Charset.forName("UTF-8"));
  fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");

  FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
  converter.setFastJsonConfig(fastJsonConfig);
  converters.add(0, converter);
}
 
Example 12
Source File: WebConfig.java    From sanshanblog with Apache License 2.0 5 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig config = new FastJsonConfig();
    config.setDateFormat("yyyy-MM-dd HH:mm:ss");
    fastJsonHttpMessageConverter.setFastJsonConfig(config);
    converters.add(fastJsonHttpMessageConverter);
    converters.add(resourceHttpMessageConverter);
    super.configureMessageConverters(converters);
}
 
Example 13
Source File: FastjsonConfiguration.java    From seed with Apache License 2.0 4 votes vote down vote up
@Bean
public HttpMessageConverters fastjsonConverter(){
    List<SerializerFeature> serializerFeatureList = new ArrayList<>();
    //格式化输出(默认只会输出成一行的字符串)
    serializerFeatureList.add(SerializerFeature.PrettyFormat);
    //输出key时是否使用双引号,默认为true
    serializerFeatureList.add(SerializerFeature.QuoteFieldNames);
    //是否输出值为null的字段,默认为true
    serializerFeatureList.add(SerializerFeature.WriteMapNullValue);
    //List字段如果为null,输出为[],而非null
    serializerFeatureList.add(SerializerFeature.WriteNullListAsEmpty);
    //数值字段如果为null,输出为0,而非null
    serializerFeatureList.add(SerializerFeature.WriteNullNumberAsZero);
    //字符类型字段如果为null,输出为"",而非null
    serializerFeatureList.add(SerializerFeature.WriteNullStringAsEmpty);
    //Boolean字段如果为null,输出为false,而非null
    serializerFeatureList.add(SerializerFeature.WriteNullBooleanAsFalse);
    //使用默认的日期格式[yyyy-MM-dd HH:mm:ss]输出Date类型,未指定该属性则会将java.util.Date类型输出为1484030642746
    serializerFeatureList.add(SerializerFeature.WriteDateUseDateFormat);
    ////测试发现,无论是否设置该属性,都会输出:["age":0]、["age":123.456]
    //serializerFeatureList.add(SerializerFeature.WriteBigDecimalAsPlain);
    ////设置该属性会使得在输出时以字符串来输出非字符串的值,比如["age":"0"]、["abc":"false"],但是List不是这样,还是会输出["goodsList":[]]
    //serializerFeatureList.add(SerializerFeature.WriteNonStringValueAsString);
    ////假设序列化的实体类为com.jadyer.demo.open.model.ReqData,设置该属性会使得输出的json中增加一个key=["@type":"com.jadyer.demo.open.model.ReqData",]
    //serializerFeatureList.add(SerializerFeature.WriteClassName);
    ////设置该属性会使得序列化时增加特殊处理字符处理,比如原本的["mytime":"2017-01-10 14:56:41",]会输出为["mytime":"2017\u002D01\u002D10\u002014\u003A56\u003A41",]
    //serializerFeatureList.add(SerializerFeature.BrowserSecure);
    SerializerFeature[] serializerFeatures = new SerializerFeature[serializerFeatureList.size()];
    serializerFeatureList.toArray(serializerFeatures);

    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    //Fastjson默认就是UTF-8
    fastJsonConfig.setCharset(StandardCharsets.UTF_8);
    //Fastjson默认就是[yyyy-MM-dd HH:mm:ss]
    fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
    //设置序列化输出时的一些额外属性
    fastJsonConfig.setSerializerFeatures(serializerFeatures);

    FastJsonHttpMessageConverter fastjson = new FastJsonHttpMessageConverter();
    fastjson.setFastJsonConfig(fastJsonConfig);
    return new HttpMessageConverters(fastjson);
}