Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin cross-platform / multi-format serialization

Kotlin cross-platform / multi-format serialization

# About me
- Twitter
- https://twitter.com/shihochandesu

- Github
- https://github.com/shihochan

# Links
- WEB+DB PRESS Vol.105
- https://www.amazon.co.jp/dp/477419851X

- Kotlin / kotlinx.serialization
- https://github.com/Kotlin/kotlinx.serialization

- JakeWharton / SdkSearch
- https://github.com/JakeWharton/SdkSearch

- JakeWharton / retrofit2-kotlinx-serialization-converter
- https://github.com/JakeWharton/retrofit2-kotlinx-serialization-converter

- Kotlin Serialization
- https://discuss.kotlinlang.org/t/kotlin-serialization/2063

- Performance issue? #89
- https://github.com/Kotlin/kotlinx.serialization/issues/89

Yuki Shiho

June 26, 2018
Tweet

More Decks by Yuki Shiho

Other Decks in Programming

Transcript

  1. About me 志甫 侑紀 / YUKI SHIHO @shihochan Software Engineer

    at AbemaTV, Inc. @shihochandesu Shibuya.apk
  2. What is kotlinx.serialization? • Kotlin / kotlinx.serialization • Supports Kotlin

    classes marked as @Serializable and standard collections • Formats: JSON, CBOR and Protobuf • Platform: Kotlin/JVM, Kotlin/JS
  3. import kotlinx.serialization.* import kotlinx.serialization.json.JSON @Serializable data class Data(val a: Int,

    @Optional val b: String = "42") • Example • stringify: convert a serializable object to JSON string. JSON.stringify(Data(42)) // {"a": 42, "b": “42"} • parse: convert well formed JSON back to a data class. JSON.parse<Data>("""{"a":42}""") // Data(a=42, b="42") What is kotlinx.serialization?
  4. What is kotlinx.serialization? • Annotations • @SerialName: for overriding property

    name with custom name. • @Optional: will be considered during serialization. But if not present, it won’t break serialization. • @Transient: will not be considered during serialization. But if present, it will cause an exception. • Nesting • are recursively serialized: enums, primitive types, arrays, lists, maps and other serializable classes.
  5. @Serializable class MyData(val s: String) { @Serializer(forClass = MyData::class) companion

    object { override fun save(output: KOutput, obj: MyData) { output.writeStringValue(…) } override fun load(input: KInput): MyData { return MyData(…) } } } • Custom Serializer What is kotlinx.serialization?
  6. • Gson vs. Moshi vs. kotlinx.serialization Which of serialization libraries?

    (TPO W .PTIJ W LPUMJOY TFSJBMJ[BUJPO ,PUMJO4VQQPSU  ̋ ̋ 1FSGPSNBODF ˕ ̋ ̋ .FUIPE$PVOU   
  7. Conclusion • Kotlin / kotlinx.serialization • Kotlin Official • Supports

    Kotlin classes • Supports Multi-Platform (JVM & JS) …(Native) • Jake Wharton is using in SdkSearch • An Android app and Chrome Extension • Exists Retrofit2 convertor