圖片來源:網路
最近在整理專案引用的3rd-party library,發現許多案子經過很多人開發,然後每個人喜愛的Lib不同,所以一個案子同樣是處理Json的Lib就有兩三種,其中最常見的就是Jackson和Gson,這下傷腦筋了,到底改保留哪一個呢?還是要根據不同的應用,取其優點來使用呢?(每次要比較Solution真的很傷腦筋,除非自己下來做實驗,不然看別人的比較其實都含有蠻多成分的主觀意識),但沒辦法,只好硬著頭皮上網收集一下比較的資訊。
| Jackson 1.x ~ 2.x | Gson |
Provider | fasterxml.com | Google |
License | ASL, LGPL | Apache License 2.0 |
Description | A High-performance JSON processor. Jackson is a multi-purpose Java library for processing JSON data format. Jackson aims to be the best possible combination of fast, correct, lightweight, and ergonomic for developers. | Gson 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. Gson can work with arbitrary Java objects
including pre-existing objects that you do not have source-code of. |
Design Goal |
- Jackson offers three alternative methods (one with two variants) for processing JSON
- Streaming API (aka "Incremental parsing/generation") reads and writes JSON content as discrete events
- Tree Model provides a mutable in-memory tree representation of a JSON document.
- Data Binding converts JSON to and from POJOs based either on property accessor conventions or annotations.
|
- Provide simple toJson() and fromJson() 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)
(Gson 沒有Tree Model)
|
優點比較表,參考 Gson v Jackson - Part 1~6[1] |
- +1 Jackson for using checked exceptions for errors during JSON parsing and generation
- Both Gson and Jackson skip transient fields. *
- 1 Jackson for very simple deserialization of any JSON object to a Map, and any JSON array to a List, composed of type-appropriate, standard Java library value objects
- +1 Jackson for providing optional use of getters and setters
- +1 Jackson for providing optional use of getters and setters
- +1 Jackson for significantly more complete data-binding support of multi-dimensional arrays
- +1 Jackson for significantly more complete data-binding support of arbitrarily complex array element types
- +1 Jackson for significantly better support of deserializing to non-generic collections
- +1 Jackson for significantly better support of deserializing to non-generic collections
- +1 Jackson for more built-in serializers and deserializers
- +1 Jackson for allowing easy plug-in of custom pretty printing
- +1 Jackson for greater configurability of null reference serialization
- +1 Jackson for ability to configure field inclusion by default visibility
- +1 Jackson for simple configurability to exclude only specified
fields for participation in serialization/deserialization (the Gson
equivalent, demonstrated below, is almost as simple)
- +1 Jackson
for greater configurability options to include only specified fields for
participation in serialization/deserialization (including
global/class/field/getter/setter configuration options)
- +1 Jackson for providing a facility to specify different translations for fields versus setter methods versus getter methods
|
- Gson instance is immutable (and thus thread-safe)
- +1 Gson for simple unwrapping single-component arrays
- +1 Gson for ability to create Java instances without no-argument constructors and without explicit configuration information
- +1 Gson for simple, contextual deserialization with parameterized type information
- +1 Gson for built-in versioning support
- +1 Gson for ability to configure field inclusion by modifiers static, transient, or volatile
- +1 Gson for providing some built-in naming strategies
|
|
* 之前有遇到一個問題,就是把JPA物件轉換成JSON格式時,如過遇到有Join的物件(例如ManyToOne),Gson會遞迴進去,所以必須要自己寫一個Annotation,讓Gson不要遞迴進去。Jackson 有 @JsonIgnore 可以使用。
** By default, during serialization, Gson skips null reference fields, and they do not appear in the JSON. Jackson includes them.During deserialization, both libraries use a JSON element null value to
set the associated Java field accordingly, as a null reference.
*** According to the latest results at
https://github.com/eishay/jvm-serializers/wiki,
Gson databinding is more than 6x slower than Gson stream processing.
This performance increase comes at the cost of coding effort.
The Gson manual stream processing solution is over 300 lines long, while
the Gson databinding solution is just a few lines.
上面是針對特性的比較,下面則是performacne的比較(TPS == transactions per second),不過這也太誇張了吧?
Performance 差那麼多,讓我有點嚇到,所以又趕緊去Google發現這個
Java serialization benchmarking,也是有人佛心來著,把所有Serialization的技術和格式做比較,看完發現....真的效能差抄多..=_=
其他選擇:
BSON for Jackson
FlexJson
Reference:
[1]
Gson v Jackson - Part 1
[2]
Java JSON Tutorial
[3]
JSON data binding performance: Jackson vs Google-gson vs BerliOS JSON Tools
[4]
Japex Report: Test suite for Json drivers 2009
沒有留言 :
張貼留言