Gson Adapters

Library that provides adapters for Gson JSON library

License

License

Categories

Categories

Gson Data JSON
GroupId

GroupId

io.github.isharipov
ArtifactId

ArtifactId

gson-adapters
Last Version

Last Version

0.1
Release Date

Release Date

Type

Type

jar
Description

Description

Gson Adapters
Library that provides adapters for Gson JSON library
Project URL

Project URL

https://github.com/iSharipov/gson-adapters
Source Code Management

Source Code Management

https://github.com/iSharipov/gson-adapters

Download gson-adapters

How to add to project

<!-- https://jarcasting.com/artifacts/io.github.isharipov/gson-adapters/ -->
<dependency>
    <groupId>io.github.isharipov</groupId>
    <artifactId>gson-adapters</artifactId>
    <version>0.1</version>
</dependency>
// https://jarcasting.com/artifacts/io.github.isharipov/gson-adapters/
implementation 'io.github.isharipov:gson-adapters:0.1'
// https://jarcasting.com/artifacts/io.github.isharipov/gson-adapters/
implementation ("io.github.isharipov:gson-adapters:0.1")
'io.github.isharipov:gson-adapters:jar:0.1'
<dependency org="io.github.isharipov" name="gson-adapters" rev="0.1">
  <artifact name="gson-adapters" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.github.isharipov', module='gson-adapters', version='0.1')
)
libraryDependencies += "io.github.isharipov" % "gson-adapters" % "0.1"
[io.github.isharipov/gson-adapters "0.1"]

Dependencies

compile (1)

Group / Artifact Type Version
com.google.code.gson : gson jar 2.8.6

test (7)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter jar 5.6.2
org.junit.vintage : junit-vintage-engine jar 5.6.2
org.assertj : assertj-core jar 3.6.1
org.hamcrest : java-hamcrest jar 2.0.0.0
com.squareup.retrofit2 : retrofit jar 2.9.0
com.squareup.retrofit2 : converter-gson jar 2.9.0
com.github.tomakehurst : wiremock-jre8 jar 2.26.3

Project Modules

There are no modules declared in this project.

Adapters for Gson JSON library

Webp net-resizeimage

Java CI with Maven Total alerts Language grade: Java
LinkedIn

Why use Gson adapters?

Sometimes we need to deserialize a JSON to a concrete class which inherits from an abstract class.

Alt text

Gson + Retrofit + Wiremock example - io.github.isharipov.gson.adapters.PolymorphDeserializerTest

Library provides a set of annotations you can apply to abstract class. Also, library contains Polymorph Deserializer which do the trick.

To tell the deserializer which implementation to use to deserialization, annotate abstract class with @JsonType annotation and provide information about subtipes through set of @JsonSubtype annotations.

@JsonType(
        property = "type",
        subtypes = {
                @JsonSubtype(clazz = CommissionEmployee.class, name = "commission"),
                @JsonSubtype(clazz = HourlyEmployee.class, name = "hourly"),
                @JsonSubtype(clazz = SalariedEmployee.class, name = "salaried")
        }
)
public abstract class Employee {
    private final String id;
    private final String type;
...    
Gson gson = new GsonBuilder()
                .registerTypeAdapter(Employee.class, new PolymorphDeserializer<Employee>())
                .create();

Pay attention, that in each implementation you should have to have a magic field, based on which, the Deserializer will determine the final implementation.

Gradle

dependencies {
    implementation("io.github.isharipov:gson-adapters:0.1")
}

Maven

<dependencies>
    <dependency>
        <groupId>io.github.isharipov</groupId>
        <artifactId>gson-adapters</artifactId>
        <version>0.1</version>
    </dependency>
<dependencies>

Requirements

Library requires Java 1.8 or later.

Versions

Version
0.1