Saturday, August 26, 2017

Alternatives of Deprecated JSON API in AEM 6.3


Hello Everyone,

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
GSON.PNG
Fig - GSON Bundle in felix console
2. Jackson
Capture.PNG
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:
  1. @Expose:
  2. @SerializedName
  3. @Since @Until
  4. @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.


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.
table.PNG
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.

6 comments:

  1. Very Informative post..
    Thanks for posting :)

    ReplyDelete
  2. Excellent.Thanks for the comparative analogy

    ReplyDelete
  3. What about JSON Simple, its not available as inbuilt in aem, still it can be embedded.

    ReplyDelete
  4. Thanks Shivani, it helped and saved my time :)

    ReplyDelete
  5. Thanks Shivani for sharing . It helpls for my project alot.

    ReplyDelete