Skip to content

Instantly share code, notes, and snippets.

@utamori
Last active March 19, 2020 05:20
Show Gist options
  • Save utamori/840c9d1a5ef35f121f626b6aefa6cf7e to your computer and use it in GitHub Desktop.
Save utamori/840c9d1a5ef35f121f626b6aefa6cf7e to your computer and use it in GitHub Desktop.
//build.gradle.kts
plugins {
    application
    kotlin("jvm") version "1.3.70"
    kotlin("plugin.serialization") version "1.3.70"
}
dependencies {
    // jvmの場合
    implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0")
}
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json.Default.stringify

//アノテーションでシリアライザ追加
@Serializable
data class Data(val a: Int,val b: String = "42")

//stringify(Class.serializer(),Class())  クラスのシリアライザとインスタンスを引数にする
val jsondata = stringify(Data.serializer(),Data(100)) // {"a":100,"b":"42"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment