Coder Social home page Coder Social logo

kson's Introduction

maven Android Arsenal

Kson - kotlin type adapter generator

An annotation processor generates Gson TypeAdapter from Kotlin Data Classes

Motivation

By default, Gson uses reflection to read/write data from JSON. It's not only slow (benchmarks), also it breaks Kotlin's null-safe types.

For example:

// your entity class with non-nullable property
data class Entity(val id: Int)

// wrong response from server
val json = """{ "id": null }"""

// this won't throw error
val entity = gson.fromJson(json, Entity::class.java)

// throws NPE somewhere in runtime when you don't expect
entity.id

In order to avoid reflection, need to create a custom TypeAdapter for every entity class. It takes time, need to write boilerplate code, tests, etc. Here this library comes, it generates TypeAdapters automatically. You just need to register generated GsonTypeAdapterFactory in your GsonBuilder.

Usage

Add @Kson annotation to your data classes and Kson will automatically generate <class name>TypeAdapter.kt files.

@Kson
data class RoleEntity(
    val id: Int, 
    @SerializedName("roleName") val name: String
)

@Kson
data class UserEntity(
    val firstname: String,
    val lastname: String,
    val roles: List<RoleEntity>
)

// etc

Also you can use @KsonFactory annotation to generate TypeAdapterFactory class

@KsonFactory
object FactoryProvider {

    get() = KsonFactoryProvider()

}

val gson = GsonBuilder()
    .registerTypeAdapterFactory(FactoryProvider.get())
    .create()

// gson.fromJson(...)

Limitations & Known issues

Since this is an early version there are some unsupported properties

@Kson
data class UnsupportedDataClass(
    @JsonAdapter(CustomAdapter::class) val id: String // custom type adapter
    val name: String = "no name" // default values
    val list: List<String?> // nullable generics
    val `natural name`: String // "natural names"
)

Installation

To add KSON to your project, add the following to your module's build.gradle:

repositories {
    jcenter()
}

dependencies {
    compile 'dev.afanasev:kson-annotation:<version>'   
    kapt 'dev.afanasev:kson-processor:<version>'
}

Mentions

Code of Conduct

Please refer to Code of Conduct document.

kson's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

kson's Issues

Don't use synchronized delegates

Now generated code looks like this:

private val adapter by lazy { gson.getAdapter(...) }

Since the evaluation of the delegated properties is synchronized and it's redundant in TypeAdapter case, need to use LazyThreadSafetyMode.NONE mode, which doesn't incur any thread-safety guarantees and the related overhead.

Support gson RuntimeTypeAdapterFactory generation

gson-extras has such class: https://github.com/google/gson/blob/master/extras/src/main/java/com/google/gson/typeadapters/RuntimeTypeAdapterFactory.java which is helpful for polymorphic types serialization without reflection.

For example, it can be used to handle Kotlin sealed class serialization:

sealed class Shape {
        class Rectangle() : Shape()
        class Circle() : Shape()
}
val adapter: RuntimeTypeAdapterFactory<Shape> = RuntimeTypeAdapterFactory.of(Shape::class.java)
        .registerSubtype(Rectangle::class.java)
        .registerSubtype(Circle::class.java)

And then in your gson instance builder:

registerTypeAdapterFactory(adapter)

However, seems it is a boilerplate and not safe solution to write such factory for every sealed class in the project. Would be nice to handle it with annotations like kotlinx-serialization does with polymorphic serialization problems: https://medium.com/@andreclassen1337/goodbye-runtimetypeadapterfactory-polymorphic-serialization-using-kotlinx-serialization-46a8cec36fdc

@Polymorphic
sealed class Shape {
        @SerialName("Rectangle")
        class Rectangle() : Shape()
        @SerialName("Circle")
        class Circle() : Shape()
}

Generate adapters based on type, not property

@Kson
data class UserEntity(
    val firstname: String, 
    val lastname: String
)

// generated code
...
private val firstnameAdapter by lazy { gson.getAdapter(String::class.java) }  
private val lastnameAdapter by lazy { gson.getAdapter(String::class.java) }  
...

As you see, each property will have it's own adapter. I think better to have adapters grouped by types, f.ex:

private val stringAdapter by lazy { gson.getAdapter(String::class.java) } 

and use this adapter in both properties

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.