use of com.alibaba.fastjson.support.config.FastJsonConfig in project fastjson by alibaba.
the class FastJsonProviderTest method test_1.
@SuppressWarnings("deprecation")
public void test_1() throws Exception {
FastJsonProvider provider1 = new FastJsonProvider("UTF-8");
Assert.assertEquals("UTF-8", provider1.getCharset().name());
FastJsonProvider provider2 = new FastJsonProvider();
provider2.setCharset(Charset.forName("GBK"));
Assert.assertEquals("GBK", provider2.getCharset().name());
Assert.assertNull(provider2.getDateFormat());
provider2.setDateFormat("yyyyMMdd");
provider2.setFeatures(SerializerFeature.IgnoreErrorGetter);
Assert.assertEquals(1, provider2.getFeatures().length);
Assert.assertEquals(SerializerFeature.IgnoreErrorGetter, provider2.getFeatures()[0]);
provider2.setFilters(serializeFilter);
Assert.assertEquals(1, provider2.getFilters().length);
Assert.assertEquals(serializeFilter, provider2.getFilters()[0]);
FastJsonProvider provider = new FastJsonProvider(new Class[] { VO.class });
Assert.assertNotNull(provider.getFastJsonConfig());
provider.setFastJsonConfig(new FastJsonConfig());
Assert.assertEquals(true, provider.isReadable(VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE));
Assert.assertEquals(true, provider.isWriteable(VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE));
Assert.assertEquals(true, provider.isReadable(VO.class, VO.class, null, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
Assert.assertEquals(true, provider.isWriteable(VO.class, VO.class, null, MediaType.APPLICATION_FORM_URLENCODED_TYPE));
Assert.assertEquals(false, provider.isReadable(VO.class, VO.class, null, MediaType.APPLICATION_XML_TYPE));
Assert.assertEquals(false, provider.isWriteable(VO.class, VO.class, null, MediaType.APPLICATION_XML_TYPE));
Assert.assertEquals(false, provider.isReadable(String.class, String.class, null, MediaType.valueOf("application/javascript")));
Assert.assertEquals(false, provider.isWriteable(String.class, String.class, null, MediaType.valueOf("application/x-javascript")));
Assert.assertEquals(false, provider.isReadable(String.class, String.class, null, MediaType.valueOf("applications/+json")));
Assert.assertEquals(false, provider.isWriteable(String.class, String.class, null, MediaType.valueOf("applications/x-json")));
Assert.assertEquals(false, provider.isReadable(null, null, null, MediaType.valueOf("application/x-javascript")));
Assert.assertEquals(false, provider.isWriteable(null, null, null, null));
VO vo = (VO) provider.readFrom(null, VO.class, null, MediaType.APPLICATION_JSON_TYPE, null, new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8"))));
Assert.assertEquals(123, vo.getId());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
provider.writeTo(vo, VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE, new MultivaluedHashMap<String, Object>(), byteOut);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
provider.getSize(vo, VO.class, VO.class, null, MediaType.APPLICATION_JSON_TYPE);
}
use of com.alibaba.fastjson.support.config.FastJsonConfig in project fastjson by alibaba.
the class FastJsonHttpMessageConverter4Test method test_1.
public void test_1() throws Exception {
FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
Assert.assertNotNull(converter.getFastJsonConfig());
converter.setFastJsonConfig(new FastJsonConfig());
converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
Method method1 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
method1.setAccessible(true);
method1.invoke(converter, int.class);
HttpInputMessage input = new HttpInputMessage() {
public HttpHeaders getHeaders() {
// TODO Auto-generated method stub
return null;
}
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
}
};
VO vo = (VO) converter.read(VO.class, VO.class, input);
Assert.assertEquals(123, vo.getId());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders();
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("{\"id\":123}", new String(bytes, "UTF-8"));
Method method2 = FastJsonHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
method2.setAccessible(true);
method2.invoke(converter, VO.class, input);
}
use of com.alibaba.fastjson.support.config.FastJsonConfig in project fastjson by alibaba.
the class FastJsonConfigTest method test_0.
public void test_0() throws Exception {
FastJsonConfig config = new FastJsonConfig();
Assert.assertEquals(Charset.forName("UTF-8"), config.getCharset());
config.setCharset(Charset.forName("GBK"));
Assert.assertEquals(Charset.forName("GBK"), config.getCharset());
Assert.assertNull(config.getDateFormat());
config.setDateFormat("yyyyMMdd");
Assert.assertNotNull(config.getDateFormat());
config.setParserConfig(ParserConfig.getGlobalInstance());
Assert.assertNotNull(config.getParserConfig());
config.setSerializeConfig(SerializeConfig.globalInstance);
Assert.assertNotNull(config.getSerializeConfig());
config.setFeatures(Feature.AllowComment, Feature.AutoCloseSource);
Assert.assertEquals(2, config.getFeatures().length);
Assert.assertEquals(Feature.AllowComment, config.getFeatures()[0]);
Assert.assertEquals(Feature.AutoCloseSource, config.getFeatures()[1]);
config.setSerializerFeatures(SerializerFeature.IgnoreErrorGetter);
Assert.assertEquals(1, config.getSerializerFeatures().length);
Assert.assertEquals(SerializerFeature.IgnoreErrorGetter, config.getSerializerFeatures()[0]);
config.setSerializeFilters(serializeFilter);
Assert.assertEquals(1, config.getSerializeFilters().length);
Assert.assertEquals(serializeFilter, config.getSerializeFilters()[0]);
classSerializeFilter.put(TestVO.class, serializeFilter);
config.setClassSerializeFilters(classSerializeFilter);
Assert.assertEquals(1, config.getClassSerializeFilters().size());
Assert.assertEquals(classSerializeFilter, config.getClassSerializeFilters());
config.setClassSerializeFilters(null);
}
use of com.alibaba.fastjson.support.config.FastJsonConfig in project fastjson by alibaba.
the class FastJsonpHttpMessageConverter4Test method test_2.
public void test_2() throws Exception {
FastJsonpHttpMessageConverter4 converter = new FastJsonpHttpMessageConverter4();
Assert.assertNotNull(converter.getFastJsonConfig());
converter.setFastJsonConfig(new FastJsonConfig());
converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
Method method1 = FastJsonpHttpMessageConverter4.class.getDeclaredMethod("supports", Class.class);
method1.setAccessible(true);
method1.invoke(converter, int.class);
HttpInputMessage input = new HttpInputMessage() {
public HttpHeaders getHeaders() {
return null;
}
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("{\"id\":123}".getBytes(Charset.forName("UTF-8")));
}
};
VO vo = (VO) converter.read(VO.class, VO.class, input);
Assert.assertEquals(123, vo.getId());
final ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpOutputMessage out = new HttpOutputMessage() {
public HttpHeaders getHeaders() {
return new HttpHeaders();
}
public OutputStream getBody() throws IOException {
return byteOut;
}
};
MappingFastJsonValue mappingFastJsonValue = new MappingFastJsonValue(vo);
mappingFastJsonValue.setJsonpFunction("callback");
converter.write(mappingFastJsonValue, VO.class, MediaType.TEXT_PLAIN, out);
byte[] bytes = byteOut.toByteArray();
Assert.assertEquals("/**/callback({\"id\":123});", new String(bytes, "UTF-8"));
Method method2 = FastJsonpHttpMessageConverter4.class.getDeclaredMethod("readInternal", Class.class, HttpInputMessage.class);
method2.setAccessible(true);
method2.invoke(converter, VO.class, input);
}
use of com.alibaba.fastjson.support.config.FastJsonConfig in project springboot_op by SnailFastGo.
the class FastJsonConfiguration method configureMessageConverters.
/**
* 修改自定义消息转换器
* @param converters 消息转换器列表
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//调用父类的配置
super.configureMessageConverters(converters);
//创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//处理中文乱码问题
List<MediaType> fastMediaTypes = new ArrayList<>();
fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
fastConverter.setSupportedMediaTypes(fastMediaTypes);
//创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty);
fastConverter.setFastJsonConfig(fastJsonConfig);
//将fastjson添加到视图消息转换器列表内
converters.add(fastConverter);
}
Aggregations