fastJosn是阿里爸爸推出一款高性能序列化工具,号称最快的json序列化工具。不过好像也就那样,在量比较大的时候优势才会明显。单次序列化时间还是会达到几十毫秒的级别。
Fastjson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Fastjson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
Fastjson Goals
- Provide best performance in server side and android client.
- Provide simple toJSONString() and parseObject() methods to convert Java objects to JSON and vice-versa.
- Allow pre-existing unmodifiable objects to be converted to and from JSON.
- Extensive support of Java Generics.
- Allow custom representations for objects.
- Support arbitrarily complex objects (with deep inheritance hierarchies and extensive use of generic types).
toJSONString:
使用toJSONString时有两个点需要注意:
(1)默认情况下属性值为null的字段不会打印,若需要展示需使用SerializerFeature.WRITE_MAP_NULL_FEATURES
SerializerFeature属性:
名称 | 含义 |
QuoteFieldNames | 输出key时是否使用双引号,默认为true |
UseSingleQuotes | 使用单引号而不是双引号,默认为false |
WriteMapNullValue | 是否输出值为null的字段,默认为false |
WriteEnumUsingToString | Enum输出name()或者original,默认为false |
SortField | 按字段名称排序后输出。默认为false |
WriteTabAsSpecial | 把\t做转义输出,默认为false |
PrettyForma | 结果是否格式化,默认为false |
WriteClassName | 序列化时写入类型信息,默认为false。反序列化是需用到 |
(2)只会打印正常类型的属性信息,自定义类无法展示。
/*** This method serializes the specified object into its equivalent Json representation. Note that this method works fine if the any of the object fields are of generic type,* just the object itself should not be of a generic type. If you want to write out the object to a* {@link Writer}, use {@link #writeJSONString(Writer, Object, SerializerFeature[])} instead.** @param object the object for which json representation is to be created setting for fastjson* @return Json representation of {@code object}.*/public static String toJSONString(Object object) {return toJSONString(object, emptyFilters);}