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

The following examples show how to use com.alibaba.fastjson.serializer.SerializeConfig#put() . 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: FastJson.java    From actframework with Apache License 2.0 6 votes vote down vote up
private void handleForSerializer(final ObjectSerializer serializer, Class targetType) {
    ClassNode node = repo.node(targetType.getName());
    if (null == node) {
        warn("Unknown target type: " + targetType.getName());
        return;
    }
    final SerializeConfig config = SerializeConfig.getGlobalInstance();
    node.visitSubTree(new Lang.Visitor<ClassNode>() {
        @Override
        public void visit(ClassNode classNode) throws Lang.Break {
            Class type = app.classForName(classNode.name());
            config.put(type, serializer);
        }
    });
    config.put(targetType, serializer);
}
 
Example 3
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 4
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 5
Source File: JsonSerialization.java    From joyrpc with Apache License 2.0 5 votes vote down vote up
/**
 * 创建序列化配置
 *
 * @return
 */
protected SerializeConfig createSerializeConfig() {
    //不采用全局配置,防止用户修改,造成消费者处理错误
    SerializeConfig config = new SerializeConfig();
    config.put(MonthDay.class, MonthDaySerialization.INSTANCE);
    config.put(YearMonth.class, YearMonthSerialization.INSTANCE);
    config.put(Year.class, YearSerialization.INSTANCE);
    config.put(ZoneOffset.class, ZoneOffsetSerialization.INSTANCE);
    config.put(ZoneId.class, ZoneIdSerialization.INSTANCE);
    config.put(ZoneId.systemDefault().getClass(), ZoneIdSerialization.INSTANCE);
    config.put(Invocation.class, InvocationCodec.INSTANCE);
    config.put(ResponsePayload.class, ResponsePayloadCodec.INSTANCE);
    config.put(BackupShard.class, BackupShardSerializer.INSTANCE);
    return config;
}
 
Example 6
Source File: JbootSwaggerController.java    From jboot with Apache License 2.0 5 votes vote down vote up
/**
 * 渲染json
 * 参考:http://petstore.swagger.io/ 及json信息 http://petstore.swagger.io/v2/swagger.json
 */
@EnableCORS
public void json() {
    Swagger swagger = JbootSwaggerManager.me().getSwagger();
    if (swagger == null) {
        renderText("swagger config error.");
        return;
    }

    // 适配swaggerUI, 解决页面"Unknown Type : ref"问题。
    SerializeConfig serializeConfig = new SerializeConfig();
    serializeConfig.put(RefProperty.class, new RefPropertySerializer());
    renderJson(JSON.toJSONString(swagger, serializeConfig));
}
 
Example 7
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");
}
 
Example 8
Source File: FastJsonKvCodecTest.java    From actframework with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void prepare() {
    SerializeConfig config = SerializeConfig.getGlobalInstance();
    config.put(KV.class, FastJsonKvCodec.INSTANCE);
    config.put(KVStore.class, FastJsonKvCodec.INSTANCE);

    ParserConfig parserConfig = ParserConfig.getGlobalInstance();
    parserConfig.putDeserializer(KV.class, FastJsonKvCodec.INSTANCE);
    parserConfig.putDeserializer(KVStore.class, FastJsonKvCodec.INSTANCE);

    JSON.DEFAULT_PARSER_FEATURE = Feature.config(JSON.DEFAULT_PARSER_FEATURE, Feature.UseBigDecimal, false);
}
 
Example 9
Source File: GHIssue297.java    From actframework with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void prepareFastJson() {
    FastJsonObjectIdCodec objectIdCodec = new FastJsonObjectIdCodec();

    SerializeConfig serializeConfig = SerializeConfig.getGlobalInstance();
    serializeConfig.put(ObjectId.class, objectIdCodec);
}