aafanasev / kson Goto Github PK
View Code? Open in Web Editor NEWGson TypeAdapter & Factory generator for Kotlin data classes
License: MIT License
Gson TypeAdapter & Factory generator for Kotlin data classes
License: MIT License
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.
@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
Can't find import javax.annotation.Generated
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()
}
data class Entity(val id: Int = 42)
data class Entity(@JsonAdapter(CustomAdapter:class) val id: Int)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.