Java Code Examples for com.alibaba.fastjson.support.config.FastJsonConfig#setCharset()
The following examples show how to use
com.alibaba.fastjson.support.config.FastJsonConfig#setCharset() .
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: WebMvcConfig.java From littleca with Apache License 2.0 | 6 votes |
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //1.需要定义一个convert转换消息的对象; FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3处理中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON); fastJsonConfig.setCharset(Charset.forName("UTF-8")); //4.在convert中添加配置信息. fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); //5.将convert添加到converters当中. converters.add(1, fastJsonHttpMessageConverter); }
Example 2
Source File: WebMvcConfig.java From littleca with Apache License 2.0 | 6 votes |
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { //1.需要定义一个convert转换消息的对象; FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter(); //2.添加fastJson的配置信息,比如:是否要格式化返回的json数据; FastJsonConfig fastJsonConfig = new FastJsonConfig(); fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat); //3处理中文乱码问题 List<MediaType> fastMediaTypes = new ArrayList<>(); fastMediaTypes.add(MediaType.APPLICATION_JSON); fastJsonConfig.setCharset(Charset.forName("UTF-8")); //4.在convert中添加配置信息. fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); //5.将convert添加到converters当中. converters.add(1,fastJsonHttpMessageConverter); }
Example 3
Source File: FastjsonConfiguration.java From seed with Apache License 2.0 | 6 votes |
@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 4
Source File: FastjsonConfiguration.java From seed with Apache License 2.0 | 6 votes |
@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 5
Source File: FastjsonConfiguration.java From seed with Apache License 2.0 | 6 votes |
@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 6
Source File: DefaultFastjsonConfig.java From MeetingFilm with Apache License 2.0 | 5 votes |
/** * 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 |
/** * 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: FastJsonConfiguration.java From flower with Apache License 2.0 | 5 votes |
@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 9
Source File: FastjsonConfiguration.java From seed with Apache License 2.0 | 4 votes |
@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); }