Skip to content

Latest commit

 

History

History
71 lines (51 loc) · 1.87 KB

README.md

File metadata and controls

71 lines (51 loc) · 1.87 KB

gsonkeepdefault

This is a pure kotlin library used together with Gson in the scenarios of decoding JSON data with a data class in Kotlin.

Background

If we have code like this:

data class AValue(val a: String = "keep me")
val a1 = Gson.fromJson("""{"a":null}""", AValue::class.java)
print(a1.a) // null

This happens often during developing, since Kotlin can do no assurance on JVM runtime.

However, what we need is pretty simple: we want our codes run as they seem like. So I write this gsonkeepdefault library to help you keep the default values of data class in development.

How To Use

Dependency (Gradle)

repositories {
  maven { url "https://jitpack.io" }
}

apply plugin 'kotlin-kapt'

dependencies {
  implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
  implementation "com.github.zijing07.gsonkeepdefault:core:1.1"
  kapt "com.github.zijing07.gsonkeepdefault:processor:1.1"
}

Dependency (Kotlin DSL Build Script)

plugins {
  kotlin("jvm")
  kotlin("kapt")
}

repositories {
  maven(url = "https://jitpack.io")
}

dependencies {
  implementation(kotlin("stdlib"))
  implementation("com.github.zijing07.gsonkeepdefault:core:1.1")
  kapt("com.github.zijing07.gsonkeepdefault:processor:1.1")
}

Kotlin

@KeepDefault
data class AValue(val a: String = "keep me")
val a1 = KGson.fromJson<AValue>("""{"a":null}""")
print(a1.a) // "keep me"

Please note that you need to use KGson here instead of Gson.

Related Libraries

  • Moshi Innovated me on the idea
  • Kotlin Metadata Really excellent work, does help to extract the default modifier info from a data class
  • Kotlin Poet A really good poet to be a parter of annotation processors