While working with a technology, we used to being habitual of an API and if someday in newer version it becomes deprecated, it just make us irritated.
So yes, I am talking about “org.apache.commons.json.* “. While writing a code in AEM 6.3, I came to know that ohhh… this has been deprecated now.
And if it is not there what are all alternatives for this. So AEM has some better alternatives now. AEM 6.3 now provides the support of Gson and jackson as well and makes AEM development more easier.
Let’s talk about alternatives:
1. GSON
Fig - GSON Bundle in felix console |
2. Jackson
Fig - JACKSON Bundle in felix console |
Before going in detail with these two API’s we need to understand two terms:
Serialization: When Pojo classes or java objects can be converted into json, it is called serialization.
Deserialization: When json data is converted to java objects it is called deserialization. So serialization and deserialization are two basic functionalities, for which we need these API’s.
1. GSON:
Add maven dependency in your pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
|
Serialization and Deserialization using GSON
The demonstration video of Serialization and Deserialization using GSON can be seen from here:
References to know more about GSON:
GSON Annotations
GSON provides some Annotations:
- @Expose:
- @SerializedName
- @Since @Until
- @JsonAdapter
The detailed description of all basic annotations will be in the demo video
References to know more about these annotations:
2.Jackson:
Add maven dependency in your pom.xml
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
|
Serialization and Deserialization using Jackson
The demonstration video of Serialization and Deserialization using Jackson can be seen from here:
References to know more about Jackson API:
Jackson Annotations
Jackson provides much more annotations than GSON.
➤Jackson Serialization Annotation
@JsonAnyGetter
@JsonGetter
@JsonPropertyOrder
@JsonRawValue
@JsonValue
@JsonRootName
@JsonSerialize
➤Jackson Deserialization Annotations
- @JsonCreator
@JacksonInject
@JsonAnySetter
@JsonSetter
@JsonDeserialize
➤Jackson Property Inclusion Annotations
- @JsonIgnoreProperties
@JacksonInject
@JsonAnySetter
@JsonSetter
The detailed description of all basic jackson annotations will be in the demo video.
References to know more about these annotations:
- http://www.baeldung.com/jackson-annotations
- https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
Jackson Vs GSON
To use both Libraries is quite easy but you must be thinking that when to use what?
Are there some specific scenarios where I should choose a particular Library?
So the answer is yes.
- For converting small- or medium-sized lists, Gson provides a better response compared to Jackson.
- For large lists, Jackson provides a better response than Gson.
- Based on this results one can conclude that for converting small or medium size list to JSON one can use Gson for better performance.
Fig - Complexity Comparison in GSON and JACKSON |
So, When to use which API is up to the requirements.
If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com
Hope it will help you guys !!
Thanks and Happy Learning.
If you have any query or suggestion then kindly comment or mail us at sgaem.blog02@gmail.com
Hope it will help you guys !!
Thanks and Happy Learning.