NyCodeGHG / retrofit2-kotlinx-serialization-converter

A Retrofit 2 Converter.Factory for Kotlin serialization.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kotlin Serialization Converter

This is a fork of @JakeWharton/retrofit2-kotlinx-serialization-converter with an Up-to-Date Kotlin Version (and JUnit 5 instead of JUnit 4).

A Retrofit 2 Converter.Factory for kotlinx.serialization.

Usage

Add a converter factory when building your Retrofit instance using the asConverterFactory extension function:

val contentType = "application/json".toMediaType()
val retrofit = Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addConverterFactory(Json.asConverterFactory(contentType))
    .build()

Response body types (e.g., Call<User>) and @Body types (e.g., @Body user: User) will now use the supplied serializer.

Because Kotlin serialization is so flexible in the types it supports, these converters assume that they can handle all types. If you are mixing this converter with another, you must add this instance last to allow the other converters a chance to see their types.

You can also use a Json or Protobuf builder to add custom serializer modules or ignore unknown keys.

val contentType = "application/json".toMediaType()
val retrofit = Retrofit.Builder()
    .baseUrl("https://example.com/")
    .addConverterFactory(Json {
        ignoreUnknownKeys = true
    }.asConverterFactory(contentType))
    .build()

Build Tools

Gradle Kotlin
implementation("de.nycode:retrofit2-kotlinx-serialization-converter:0.10.0")
Gradle Groovy
implementation 'de.nycode:retrofit2-kotlinx-serialization-converter:0.10.0'
Maven
<dependency>
    <groupId>de.nycode</groupId>
    <artifactId>retrofit2-kotlinx-serialization-converter</artifactId>
    <version>0.10.0</version>
</dependency>

License

Copyright 2018-2021 Jake Wharton, NyCode

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

A Retrofit 2 Converter.Factory for Kotlin serialization.

License:Apache License 2.0


Languages

Language:Kotlin 100.0%