兼容json-lib


这是一系列特性组合起来实现的,使用时,自己封装一个类,例如:

importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
importcom.alibaba.fastjson.serializer.JSONSerializerMap;
importcom.alibaba.fastjson.serializer.SerializerFeature;
 
privatestaticfinalJSONSerializerMap   mapping;
static{
    mapping =newJSONSerializerMap();
    mapping.put(Date.class,newJSONLibDataFormatSerializer());// 使用和json-lib兼容的日期输出格式
}
 
privatestaticfinalSerializerFeature[] features = { SerializerFeature.WriteMapNullValue,// 输出空置字段
        SerializerFeature.WriteNullListAsEmpty,// list字段如果为null,输出为[],而不是null
        SerializerFeature.WriteNullNumberAsZero,// 数值字段如果为null,输出为0,而不是null
        SerializerFeature.WriteNullBooleanAsFalse,// Boolean字段如果为null,输出为false,而不是null
        SerializerFeature.WriteNullStringAsEmpty// 字符类型字段如果为null,输出为"",而不是null
                                                  };
 
// 序列化为和JSON-LIB兼容的字符串
publicstaticString toCompatibleJSONString(Object object) {
    returnJSON.toJSONString(object, mapping, features);
}

你可能感兴趣的:(fastjson,json-lib,兼容)