Gson Adapters

Library that provides adapters for Gson JSON library

Лицензия

Лицензия

Категории

Категории

Gson Данные JSON
Группа

Группа

io.github.isharipov
Идентификатор

Идентификатор

gson-adapters
Последняя версия

Последняя версия

0.1
Дата

Дата

Тип

Тип

jar
Описание

Описание

Gson Adapters
Library that provides adapters for Gson JSON library
Ссылка на сайт

Ссылка на сайт

https://github.com/iSharipov/gson-adapters
Система контроля версий

Система контроля версий

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

Скачать gson-adapters

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (1)

Идентификатор библиотеки Тип Версия
com.google.code.gson : gson jar 2.8.6

test (7)

Идентификатор библиотеки Тип Версия
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

Модули Проекта

Данный проект не имеет модулей.

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.

Версии библиотеки

Версия
0.1