Java 类com.alibaba.fastjson.support.config.FastJsonConfig 实例源码

项目:GitHub    文件:FastJsonProvider.java   
/**
 * Method that JAX-RS container calls to deserialize given value.
 */
public Object readFrom(Class<Object> type, //
                       Type genericType, //
                       Annotation[] annotations, //
                       MediaType mediaType, //
                       MultivaluedMap<String, String> httpHeaders, //
                       InputStream entityStream) throws IOException, WebApplicationException {

    try {
        FastJsonConfig fastJsonConfig = locateConfigProvider(type, mediaType);

        return JSON.parseObject(entityStream, fastJsonConfig.getCharset(), genericType, fastJsonConfig.getFeatures());

    } catch (JSONException ex) {

        throw new WebApplicationException("JSON parse error: " + ex.getMessage(), ex);
    }
}
项目:GitHub    文件:Issue1341.java   
public boolean configure(final FeatureContext context) {
    final Configuration config = context.getConfiguration();
    final String jsonFeature = CommonProperties.getValue(config.getProperties(), config.getRuntimeType(), InternalProperties.JSON_FEATURE, JSON_FEATURE,
            String.class);
    // Other JSON providers registered.
    if (!JSON_FEATURE.equalsIgnoreCase(jsonFeature)) {
        return false;
    }
    // Disable other JSON providers.
    context.property(PropertiesHelper.getPropertyNameForRuntime(InternalProperties.JSON_FEATURE, config.getRuntimeType()), JSON_FEATURE);
    // Register FastJson.
    if (!config.isRegistered(FastJsonProvider.class)) {
        //DisableCircularReferenceDetect
        FastJsonProvider fastJsonProvider = new FastJsonProvider();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,SerializerFeature.BrowserSecure);

        fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect);

        fastJsonProvider.setFastJsonConfig(fastJsonConfig);

        context.register(fastJsonProvider, MessageBodyReader.class, MessageBodyWriter.class);
    }
    return true;
}
项目:GitHub    文件:Issue1341.java   
@Override
protected Application configure() {
    enable(TestProperties.LOG_TRAFFIC);
    enable(TestProperties.DUMP_ENTITY);

    ResourceConfig config = new ResourceConfig();

    FastJsonProvider fastJsonProvider = new FastJsonProvider();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect, SerializerFeature.BrowserSecure);
    fastJsonProvider.setFastJsonConfig(fastJsonConfig);

    config.register(fastJsonProvider);

    config.packages("com.alibaba.json.bvt.issue_1300");
    return config;
}
项目:GitHub    文件:FastJsonJsonViewTest.java   
@Test
public  void test_jsonp() throws Exception {
    FastJsonJsonView view = new FastJsonJsonView();

    Assert.assertNotNull(view.getFastJsonConfig());
    view.setFastJsonConfig(new FastJsonConfig());
    view.setExtractValueFromSingleKeyModel(true);
    view.setDisableCaching(true);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("callback", "queryName");
    MockHttpServletResponse response = new MockHttpServletResponse();


    Assert.assertEquals(true, view.isExtractValueFromSingleKeyModel());


    view.render(Collections.singletonMap("abc", "cde中文"), request, response);
    String contentAsString = response.getContentAsString();
    int contentLength = response.getContentLength();

    Assert.assertEquals(contentLength, contentAsString.getBytes(view.getFastJsonConfig().getCharset().name()).length);
}
项目:springboot_op    文件:FastJsonConfiguration.java   
/**
 * 修改自定义消息转换器
 * @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);
}
项目:sns-todo    文件:WebMvcConfig.java   
private 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;
}
项目:ontology_setting    文件:FastJsonConfiguration.java   
/**
 * 修改自定义消息转换器
 * @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);
}
项目:utils    文件:FastJsonAutoConfiguration.java   
@Bean
@ConditionalOnMissingBean(com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter.class)
public HttpMessageConverters customConverters(FastJsonHttpMessageConverter converter) {
    Collection<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();

    if (null == converter) {
        Class<?> converterClass = properties.getConverter();
        converter = (FastJsonHttpMessageConverter) BeanUtils.instantiate(converterClass);
    }

    FastJsonConfig config = new FastJsonConfig();
    List<SerializerFeature> features = properties.getFeatures();
    if (!CollectionUtils.isBlank(features)) {
        SerializerFeature[] featureArray = new SerializerFeature[features.size()];
        config.setSerializerFeatures(features.toArray(featureArray));
    }

    converter.setFastJsonConfig(config);
    messageConverters.add(converter);

    return new HttpMessageConverters(true, messageConverters);
}
项目:easyweb    文件:WebConfig.java   
/**
 * 配置使用springmvc fastjson
 * @return
 */
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    HttpMessageConverter<?> converter = fastConverter;
    return new HttpMessageConverters(converter);
}
项目:SpringBootStudy    文件:WebMvcConfig.java   
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,//保留空的字段
            SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
            SerializerFeature.WriteNullNumberAsZero);//Number null -> 0
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
项目:SpringBootStudy    文件:WebMvcConfig.java   
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
    FastJsonConfig config = new FastJsonConfig();
    config.setSerializerFeatures(SerializerFeature.WriteMapNullValue,//保留空的字段
            SerializerFeature.WriteNullStringAsEmpty,//String null -> ""
            SerializerFeature.WriteNullNumberAsZero);//Number null -> 0
    converter.setFastJsonConfig(config);
    converter.setDefaultCharset(Charset.forName("UTF-8"));
    converters.add(converter);
}
项目:spring-boot-frameset    文件:MyWebMvcConfigurerAdapter.java   
/**
 * 配置fastJson
 * @param converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(fastConverter);
    super.configureMessageConverters(converters);
}
项目:spring-boot-frameset    文件:MyWebMvcConfigurerAdapter.java   
/**
 * 配置fastJson
 * @param converters
 */
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(fastConverter);
    super.configureMessageConverters(converters);
}
项目:wenku    文件:FastJsonHttpMessageConvertersConfiguration.java   
@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;
}
项目:summer    文件:DefaultFastJsonMessageConverterRegister.java   
@Override
public void registry(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter4();
    ArrayList<MediaType> arrayList = new ArrayList<MediaType>() {{
        add(MediaType.APPLICATION_JSON_UTF8);
        add(MediaType.valueOf("text/html;charset=UTF-8"));
        add(MediaType.MULTIPART_FORM_DATA);
    }};
    fastJsonHttpMessageConverter.setSupportedMediaTypes(arrayList);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
            SerializerFeature.DisableCircularReferenceDetect);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(0, fastJsonHttpMessageConverter);
}
项目:summer    文件:FastJsonMessageConverterRegister.java   
@Override
public void registry(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter4();
    ArrayList<MediaType> arrayList = new ArrayList<MediaType>() {{
        add(MediaType.APPLICATION_JSON_UTF8);
        add(MediaType.valueOf("text/html;charset=UTF-8"));
        add(MediaType.MULTIPART_FORM_DATA);
    }};
    fastJsonHttpMessageConverter.setSupportedMediaTypes(arrayList);
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
            SerializerFeature.DisableCircularReferenceDetect);
    fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    converters.add(0, fastJsonHttpMessageConverter);
}
项目:jbase    文件:MvcConfig.java   
@Bean
public FastJsonHttpMessageConverter fastJsonHttpMessageConverters() {
    ParserConfig.getGlobalInstance().setAutoTypeSupport(true);

    FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    fastConverter.setFastJsonConfig(fastJsonConfig);

    return fastConverter;
}
项目:GitHub    文件:FastJsonRedisSerializer.java   
public FastJsonConfig getFastJsonConfig() {
    return fastJsonConfig;
}
项目:GitHub    文件:FastJsonRedisSerializer.java   
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
    this.fastJsonConfig = fastJsonConfig;
}
项目:GitHub    文件:FastJsonProvider.java   
/**
     * Method that JAX-RS container calls to serialize given value.
     */
    public void writeTo(Object obj, //
                        Class<?> type, //
                        Type genericType, //
                        Annotation[] annotations, //
                        MediaType mediaType, //
                        MultivaluedMap<String, Object> httpHeaders, //
                        OutputStream entityStream //
    ) throws IOException, WebApplicationException {

        FastJsonConfig fastJsonConfig = locateConfigProvider(type, mediaType);

        SerializerFeature[] serializerFeatures = fastJsonConfig.getSerializerFeatures();
        if (pretty) {
            if (serializerFeatures == null)
                serializerFeatures = new SerializerFeature[]{SerializerFeature.PrettyFormat};
            else {
                List<SerializerFeature> featureList = new ArrayList<SerializerFeature>(Arrays
                        .asList(serializerFeatures));
                featureList.add(SerializerFeature.PrettyFormat);
                serializerFeatures = featureList.toArray(serializerFeatures);
            }
            fastJsonConfig.setSerializerFeatures(serializerFeatures);
        }

        try {
            int len = JSON.writeJSONString(entityStream, //
                    fastJsonConfig.getCharset(), //
                    obj, //
                    fastJsonConfig.getSerializeConfig(), //
                    fastJsonConfig.getSerializeFilters(), //
                    fastJsonConfig.getDateFormat(), //
                    JSON.DEFAULT_GENERATE_FEATURE, //
                    fastJsonConfig.getSerializerFeatures());

//            // add Content-Length
//            if (fastJsonConfig.isWriteContentLength()) {
//                httpHeaders.add("Content-Length", String.valueOf(len));
//            }

            entityStream.flush();

        } catch (JSONException ex) {

            throw new WebApplicationException("Could not write JSON: " + ex.getMessage(), ex);
        }
    }
项目:GitHub    文件:FastJsonProvider.java   
/**
 * Helper method that is called if no config has been explicitly configured.
 */
protected FastJsonConfig locateConfigProvider(Class<?> type, MediaType mediaType) {

    if (providers != null) {

        ContextResolver<FastJsonConfig> resolver = providers.getContextResolver(FastJsonConfig.class, mediaType);

        if (resolver == null) {

            resolver = providers.getContextResolver(FastJsonConfig.class, null);
        }

        if (resolver != null) {

            return resolver.getContext(type);
        }
    }

    return fastJsonConfig;
}
项目:GitHub    文件:FastJsonHttpMessageConverter4Test.java   
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);
    }
项目:GitHub    文件:FastJsonpHttpMessageConverter4Test.java   
public void test_1() 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;
            }
        };
        converter.write(vo, VO.class, MediaType.TEXT_PLAIN, out);

        byte[] bytes = byteOut.toByteArray();
        Assert.assertEquals("{\"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);
    }
项目:GitHub    文件:FastJsonpHttpMessageConverter4Test.java   
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);
    }
项目:GitHub    文件:FastJsonHttpMessageConverterTest.java   
public void test_1() throws Exception {

        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();

        Assert.assertNotNull(converter.getFastJsonConfig());
        converter.setFastJsonConfig(new FastJsonConfig());

        converter.canRead(VO.class, MediaType.APPLICATION_JSON_UTF8);
        converter.canWrite(VO.class, MediaType.APPLICATION_JSON_UTF8);
        converter.canRead(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);
        converter.canWrite(VO.class, VO.class, MediaType.APPLICATION_JSON_UTF8);

        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"));

        converter.setSupportedMediaTypes(Collections
                .singletonList(MediaType.APPLICATION_JSON));

        converter.write(vo, VO.class, null, out);

        converter.write(vo, VO.class, MediaType.ALL, out);

        HttpOutputMessage out2 = new HttpOutputMessage() {

            public HttpHeaders getHeaders() {

                return new HttpHeaders() {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public MediaType getContentType() {

                        return MediaType.APPLICATION_JSON;
                    }

                    @Override
                    public long getContentLength() {

                        return 1;
                    }
                };
            }

            public OutputStream getBody() throws IOException {
                return byteOut;
            }
        };

        converter.write(vo, VO.class, MediaType.ALL, out2);

    }
项目:GitHub    文件:FastJsonJsonViewTest.java   
public void test_1() throws Exception {

    FastJsonJsonView view = new FastJsonJsonView();

    Assert.assertNotNull(view.getFastJsonConfig());
    view.setFastJsonConfig(new FastJsonConfig());

    Map<String, Object> model = new HashMap<String, Object>();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    view.render(model, request, response);

    view.setRenderedAttributes(null);
    view.render(model, request, response);

    view.setUpdateContentLength(true);
    view.render(model, request, response);

    view.setExtractValueFromSingleKeyModel(true);
    Assert.assertEquals(true, view.isExtractValueFromSingleKeyModel());

    view.setDisableCaching(true);
    view.render(Collections.singletonMap("abc", "cde"), request, response);

}
项目:GitHub    文件:FastJsonJsonViewTest.java   
@Test
public  void test_jsonp_invalidParam() throws Exception {
    FastJsonJsonView view = new FastJsonJsonView();

    Assert.assertNotNull(view.getFastJsonConfig());
    view.setFastJsonConfig(new FastJsonConfig());
    view.setExtractValueFromSingleKeyModel(true);
    view.setDisableCaching(true);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("callback", "-methodName");
    MockHttpServletResponse response = new MockHttpServletResponse();


    Assert.assertEquals(true, view.isExtractValueFromSingleKeyModel());


    view.render(Collections.singletonMap("doesn't matter", Collections.singletonMap("abc", "cde中文")), request, response);
    String contentAsString = response.getContentAsString();
    Assert.assertTrue(contentAsString.startsWith("{\"abc\":\"cde中文\"}"));

}
项目:GitHub    文件:FastJsonConfigTest.java   
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);

        config.setWriteContentLength(false);
        Assert.assertEquals(false, config.isWriteContentLength());
    }
项目:GitHub    文件:FastJsonProviderTest.java   
@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());

    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setWriteContentLength(false);
    provider.setFastJsonConfig(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);

    try {
        provider.readFrom(null, VO.class, null, MediaType.APPLICATION_JSON_TYPE, null, new ByteArrayInputStream("\"id\":123".getBytes(Charset
                .forName("UTF-8"))));
    } catch (WebApplicationException ex) {
        Assert.assertNotNull(ex);
    }

}
项目:validator-web    文件:FastJsonHttpMessageReader.java   
/**
 * @return the fastJsonConfig.
 */
public FastJsonConfig getFastJsonConfig() {
    return fastJsonConfig;
}
项目:validator-web    文件:FastJsonHttpMessageReader.java   
/**
 * @param fastJsonConfig the fastJsonConfig to set.
 */
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
    this.fastJsonConfig = fastJsonConfig;
}
项目:GitHub    文件:Issue1392.java   
public FastJsonConfig getContext(Class<?> type) {

            FastJsonConfig fastJsonConfig = new FastJsonConfig();

            fastJsonConfig.setSerializerFeatures(
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.BrowserSecure);

            return fastJsonConfig;
        }
项目:GitHub    文件:FastJsonJsonView.java   
/**
 * @return the fastJsonConfig.
 * @since 1.2.11
 */
public FastJsonConfig getFastJsonConfig() {
    return fastJsonConfig;
}
项目:GitHub    文件:FastJsonJsonView.java   
/**
 * @param fastJsonConfig the fastJsonConfig to set.
 * @since 1.2.11
 */
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
    this.fastJsonConfig = fastJsonConfig;
}
项目:GitHub    文件:FastJsonHttpMessageConverter.java   
/**
 * @return the fastJsonConfig.
 * @since 1.2.11
 */
public FastJsonConfig getFastJsonConfig() {
    return fastJsonConfig;
}
项目:GitHub    文件:FastJsonHttpMessageConverter.java   
/**
 * @param fastJsonConfig the fastJsonConfig to set.
 * @since 1.2.11
 */
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
    this.fastJsonConfig = fastJsonConfig;
}
项目:GitHub    文件:FastJsonProvider.java   
/**
 * @return the fastJsonConfig.
 * @since 1.2.11
 */
public FastJsonConfig getFastJsonConfig() {
    return fastJsonConfig;
}
项目:GitHub    文件:FastJsonProvider.java   
/**
 * @param fastJsonConfig the fastJsonConfig to set.
 * @since 1.2.11
 */
public void setFastJsonConfig(FastJsonConfig fastJsonConfig) {
    this.fastJsonConfig = fastJsonConfig;
}