loading...

JSON deserialize generic types using Gson and Jackson

jeetmp3 profile image Jitendra Singh Bisht Originally published at blogs.jbisht.com on ・1 min read

This blog will guide you how to deserialize json into java generic types. Suppose we have 2 json as given below.

SocialAccountResponse.json
UserResponse.json

Both json have common keys, only data object is being change. To deserialize these jsons without generic types we would have to create 2 wrappers with 2 actual data class. But with generic types we only need to create generic class for root keys and actual data class for every object in data key.

GenericResponse.java
SocialAccountsResponse.java
UserResponse.java

Now We’ll deserialize above JSON in Generic class using below libraries

  1. Google Gson
  2. Jackson

1. Deserialize using Google Gson

2. Deserialize using Jackson

Posted on Jul 24 '19 by:

jeetmp3 profile

Jitendra Singh Bisht

@jeetmp3

Backend engineer and provide geek. Love to read and write about #java #groovy #spring #springboot

Discussion

markdown guide