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);}
?