Java Code Examples for com.alibaba.fastjson.serializer.SerializeConfig#globalInstance()

The following examples show how to use com.alibaba.fastjson.serializer.SerializeConfig#globalInstance() . 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: DefaultWebMvcConfigurerAdapter.java    From dk-foundation with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();

    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    JSON.defaultTimeZone = TimeZone.getTimeZone("Asia/Shanghai");
    JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
    fastJsonConfig.setSerializerFeatures(SerializerFeature.BrowserCompatible,
            SerializerFeature.BrowserSecure,
            SerializerFeature.PrettyFormat,
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteMapNullValue,
            SerializerFeature.DisableCircularReferenceDetect);
    /**
     * 解决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);
    fastConvert.setFastJsonConfig(fastJsonConfig);
    converters.add(fastConvert);
}
 
Example 2
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 3
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 4
Source File: JsonDataCodec.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] encodeResponseData(Object data,RpcMethod rpcMethod) {
    if(data == null){
        return EMPTY;
    }

    try (SerializeWriter out = new SerializeWriter(null, JSON.DEFAULT_GENERATE_FEATURE,
            SERIALIZER_FEATURES)) {
        JSONSerializer serializer = new JSONSerializer(out, SerializeConfig.globalInstance);
        serializer.write(data);
        return out.toBytes(CHARSET_UTF8);
    }
}
 
Example 5
Source File: ApiManager.java    From actframework with Apache License 2.0 5 votes vote down vote up
public void load(App app) {
    LOGGER.info("start compiling API book");
    if (app.isProd()) {
        try {
            deserialize();
        } catch (Exception e) {
            warn(e, "Error deserialize api-book");
        }
        if (!endpoints.isEmpty()) {
            return;
        }
    }
    loadActAppDocs();
    Router router = app.router();
    AppConfig config = app.config();
    Set<Class> controllerClasses = new HashSet<>();
    ApiDocCompileContext ctx = new ApiDocCompileContext();
    ctx.saveCurrent();
    SerializeConfig fjConfig = SerializeConfig.globalInstance;
    Class<?> stringSObjectType = SObject.of("").getClass();
    fjConfig.put(stringSObjectType, new FastJsonSObjectSerializer());
    try {
        load(router, null, config, controllerClasses, ctx);
        for (NamedPort port : app.config().namedPorts()) {
            router = app.router(port);
            load(router, port, config, controllerClasses, ctx);
        }
        if (Act.isDev()) {
            exploreDescriptions(controllerClasses);
        }
        buildModuleLookup();
        serialize();
    } finally {
        ctx.destroy();
    }
    LOGGER.info("API book compiled");
}