March 29, 2020
Estimated Post Reading Time ~

Alternatives of Deprecated JSON API in AEM 6.3

In AEM 6.3 org.apache.commons.json.* has been deprecated now.
So AEM has some better alternatives now. AEM 6.3 now provides the support of Gson and Jackson as well and makes AEM development 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
References to know more about GSON:
https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

GSON Annotations
GSON provides some Annotations:
@Expose:
@SerializedName
@Since 
@Until
@JsonAdapter

References to know more about these annotations:
http://websystique.com/java/json/gson-json-annotations-example/
http://www.javacreed.com/gson-annotations-example/

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
References to know more about Jackson API:
https://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/

Jackson Annotations
Jackson provides many 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

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 these results one can conclude that for converting small or medium size lists 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.



By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.