0
0

More than 1 year has passed since last update.

はじめに

今回はGsonが非推奨なのでMoshiを使ってのJson変換を紹介していきます

本文

コード自体は下記の通りです

Moshi.Builder()
.add(KotlinJsonAdapterFactory())
.build()
.adapter(HogeData::class.java)
.fromJson(Json)

adapterの中には変換先のデータクラスを渡し、fromJsonには変換したいJsonを渡します。
ここで2点注意が必要なのですが、
1つ目.add(KotlinJsonAdapterFactory())Kotlin用のFactoryを追加することです
2つ目は変換先のデータクラスで下記二つのアノテーションをつけることです。
データクラス自体には@JsonClass(generateAdapter = true)
各種変数には@Json(name = "hoge")

最後に

今回はだんだんと増えてくるであろうMoshiの実装方法を紹介していきました
どなたかのお役に立てれば幸いです

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0