kotlinx.serialization

 kotlinx.serialization

E28404a128b6014e6f499d2a967c72a0?s=128

Takuji Nishibayashi

October 04, 2020
Tweet

Transcript

  1. kotlinx .serialization @takuji31 J Lang Fest Kansai Online #1

  2. ࣗݾ঺հ • @takuji31 • Takuji Nishibayashi • Hatena Co., Ltd.

    • App Team • ίϛοΫDAYS • ΧΫϤϜ • δϟϯϓϧʔΩʔʂ
  3. ࣗݾ঺հ • Android • Kotlin • (Flutter / iOS /

    Material Design etc.) • ! https://blog.takuji31.jp • " https://nazuna.takuji31.jp • # https://photo.takuji31.jp
  4. ΞϓϦέʔγϣϯͰJSON΍YAML ΍XMLΛಡΜͩΓॻ͍ͨΓͯ͠·͢ ͔ʁ

  5. AGENDA • kotlinx.serializationʹ͍ͭͯ • kotlinx.serializationͷ࢖͍ํ • ڝ߹ϥΠϒϥϦʔͱͷൺֱ

  6. kotlinx. serializationʹ͍ͭͯ

  7. Kotlin੡ͷγϦΞϥΠθʔγϣϯϥ ΠϒϥϦʔ

  8. Kotlinެࣜ

  9. ϚϧνϓϥοτϑΥʔϜରԠ

  10. JSONҎ֎ʹ΋ෳ਺ͷϑΥʔϚοτ ʹରԠ

  11. ϦϑϨΫγϣϯͳ͠

  12. kaptෆཁ

  13. Null SafetyରԠ

  14. github.com/Kotlin/ kotlinx.serialization

  15. kotlinx. serializationͷ࢖͍ํ

  16. ηοτΞοϓखॱ

  17. plugins { kotlin("multiplatform") // or kotlin("jvm") or kotlin("android") etc. kotlin("plugin.serialization")

    version "1.4.10" // Kotlin version } kotlin { sourceSets { val commonMain by getting { dependencies { implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.0.0-RC2") } } } }
  18. or github.com/Kotlin/ kotlinx.serialization#setup

  19. JSON

  20. { "id": 1, "title": "సੜͨ͠ΒKotlinͩͬͨ݅", "author": "@takuji31", "stories": [ {

    "id": 1001, "number": 1, "title": "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠" } ] }
  21. Ϋϥεఆٛ

  22. @Serializable data class Comic( val id: Long, val title: String,

    val author: String, val stories: List<Story>, ) @Serializable data class Story( val id: Long, val number: Int, val title: String, )
  23. γϦΞϥΠζ

  24. val comic = Comic( id = 1L, title = "సੜͨ͠ΒKotlinͩͬͨ",

    author = "@takuji31", stories = listOf( Story( id = 1001L, number = 1, title = "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠", ), ), ) Json.encodeToString(Comic.serializer(), comic)
  25. val comic = Comic( id = 1L, title = "సੜͨ͠ΒKotlinͩͬͨ",

    author = "@takuji31", stories = listOf( Story( id = 1001L, number = 1, title = "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠", ), ), ) Json.encodeToString(comic)
  26. {"id":1,"title":"సੜͨ͠ΒKotlinͩͬͨ","author":"@takuji31","stories":[{"id":1001,"number":1,"title":"ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠"}]}

  27. !

  28. val comic = Comic( ... ) val format = Json

    { prettyPrint = true } format.encodeToString(comic)
  29. { "id": 1, "title": "సੜͨ͠ΒKotlinͩͬͨ", "author": "@takuji31", "stories": [ {

    "id": 1001, "number": 1, "title": "ਆ༷ͷϛεͰϓϩάϥϛϯάݴޠʹసੜͨ͠" } ] }
  30. prettyPrintҎ֎ͷΦϓγϣϯ͸ docs/json.md#json-configuration

  31. σγϦΞϥΠζ

  32. val json = //get json val comic: Comic = Json.decodeFromString(json)

  33. val format = Json { ignoreUnknownKeys = true } val

    json = //get json val comic: Comic = format.decodeFromString(json)
  34. ڝ߹ϥΠϒϥϦʔ ͱͷൺֱ

  35. ൺֱ͢ΔϥΠϒϥϦʔ • Gson • Jackson • Moshi

  36. σγϦΞϥΠζ࣌ͷܗࣜ

  37. ܗࣜ • ϦϑϨΫγϣϯ • kapt • GradleϓϥάΠϯ

  38. ֤ϥΠϒϥϦʔͷσγϦΞϥΠζܗࣜ ϥΠϒϥϦʔ ܗࣜ Gson ϦϑϨΫγϣϯ Jackson ϦϑϨΫγϣϯ Moshi ϦϑϨΫγϣϯ/kapt kotlinx.serialization

    GradleϓϥάΠϯ
  39. KotlinͷNull SafetyରԠ

  40. ֤ϥΠϒϥϦʔͷNull SafetyରԠ ϥΠϒϥϦʔ ରԠ/ඇରԠ Gson ඇରԠ Jackson ରԠ(jackson-module-kotlin) 1 Moshi

    ରԠ kotlinx.serialization ରԠ 1 https://github.com/FasterXML/jackson-dataformats-text
  41. JSONҎ֎ͷϑΥʔϚοτ΁ͷରԠ

  42. ֤ϥΠϒϥϦʔͷରԠϑΥʔϚοτ(Ұ෦) ϥΠϒϥϦʔ ରԠϑΥʔϚοτ Gson ͳ͠ Jackson YAML/CSV/XML/Properties/Protobuf 1 2 Moshi

    ͳ͠ kotlinx.serialization YAML/XML/Properties/Protobuf 3 3 https://github.com/Kotlin/kotlinx.serialization/blob/master/formats/README.md 2 https://github.com/FasterXML/jackson-dataformats-binary 1 https://github.com/FasterXML/jackson-dataformats-text
  43. Kotlin Multiplatform Project(KMP/ KMM)ରԠ

  44. ֤ϥΠϒϥϦʔͷରԠϑΥʔϚοτ(Ұ෦) ϥΠϒϥϦʔ ରԠ/ඇରԠ Gson ඇରԠ Jackson ඇରԠ Moshi ඇରԠ kotlinx.serialization

    ରԠ
  45. ·ͱΊ

  46. ·ͱΊ • kotlinx.serialization͸KotlinެࣜγϦΞϥΠθʔγϣϯϥΠϒϥϦʔ • ηοτΞοϓͨ͠Β؆୯ʹ࢖͑Δ • GradleϓϥάΠϯʹΑΔίʔυੜ੒ͳͷͰ࣮ߦ࣌/Ϗϧυ࣌ͷύϑΥʔϚϯ ε͕ྑ͍ • JSONҎ֎ʹ΋ଟ਺ͷϑΥʔϚοτʹରԠ

    • ϚϧνϓϥοτϑΥʔϜରԠͰiOS΍αʔόʔαΠυͰ΋࢖͑Δ • ϚϧνϓϥοτϑΥʔϜରԠ͍ͯ͠ͳ͍ϑΥʔϚοτ΋͋ΔͷͰ஫ҙ
  47. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠