Jackson Parser Library is a very popular library to map JSON response to the POJO or Model classes in android. The Jackson parser has a better parsing speed as compared to other popular parsing libraries like JSONP, GSON, etc. In this blog, I will explain to you the basic procedure that you can use in your own android projects.
First of all, we need to add the dependency for it in the app/build.gradle file.
1 2 3 4 |
implementation 'com.squareup.retrofit2:converter-jackson:2.7.2' implementation 'com.fasterxml.jackson.core:jackson-databind:2.10.3' implementation 'com.fasterxml.jackson.core:jackson-core:2.10.3' implementation 'com.fasterxml.jackson.core:jackson-annotations:2.10.3' |
After adding the required dependencies you just have to sync gradle. Now, let’s start making a simple POJO for the JSON.
1 2 3 4 5 |
{ "class": 12, "subject": ["English","Physics","Chemistry"], "Passed": false } |
Now let’s build the Model Kotlin class for it.
1 2 3 4 5 6 7 8 9 10 |
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) class Student { @JasonProperty("class") val classLevel:Int=0 @JasonProperty("subject") val subject: ArrayList<String> = ArrayList() @JasonProperty("passed") val passed: Boolean= false } |
Over the above model class, we have used various annotation well what is annotation you can refer to the official blog: https://developer.android.com/reference/java/text/Annotation
For the annotation we have used in our above demo these are the Jackson Annotations. Jackson contains a set of Java annotations which you can use to modify how Jackson reads and writes JSON to and from Java objects.
1 2 3 4 5 6 |
@JsonInclude is used when there are specific type of property can be included like if they are non-null, non-empty, or have non-default values as shown above. @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) class Student { -- } |
@JsonProperty
It is used to map property names with JSON keys during serialization and deserialization.
1 2 |
@JasonProperty("subject") val subject: ArrayList<String> = ArrayList() |
@JsonAlias
is used when there is a property that can have multiple names to map to the same variable.
1 2 3 |
@JasonProperty("passed") @JsonAlias("passed", "failed") val passed: Boolean= false |
Now let’s proceed to see how we can map our JSON schema response to model class.
1 2 |
ObjecMapper mapper=new ObjectMapper(); mapper.readValue(reponseBody ,Student::class.java); |
If you want to create the JSON schema from the model object you can do it by using the ObjectMapper class object. In the below example as student object(“studentObject”) is the object of the Student class.
1 |
mapper.writeValuesAsString(studentObject) |
How to use Jackson parser when there is dynamic JSON
When we work with the predefined JSON data with Jackson is plain sailing but there are times when we need to handle dynamic JSON objects on which we have unknown properties. Let’s see the below dynamic JSON response example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
{ "class": 12, "subject": [ "English", "Physics", "Chemistry" ], "passed": false, "year": { "2021": "failed", "2020": "passed", "2021": "passed" } } |
When we are dealing with the Dynamic JSON response, we need an appropriate representation for the details object here come role of JsonNode which can handle dynamic keys.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) class Student { @JasonProperty("class") val classLevel:Int=0 @JasonProperty("subject") val subject: ArrayList<String> = ArrayList() @JasonProperty("passed") @JsonAlias("passed", "failed") val passed: Boolean= false @JasonProperty("year") val year: JsonNode?= null } |
Now you can manage the JsonNode object as you wish to provide the value as key-value pair. As you see on the below code.
1 2 3 4 |
When(year.fields().hasNext()){ val getValue : String = year.fields().next()?.value //can do operation based on the gathered value. } |
You can also check out various methods or values of the JsonNode that you can use as per your requirement, you can also check this link.
. . . . . . . . .
That’s it from my side for today, thanks for reading it until now. You can check our other android blogs Click Here.