ZacSweers / MoshiX

Extensions for Moshi including IR plugins, moshi-sealed, and more.
Apache License 2.0
514 stars 37 forks source link

kotlinx immutable collections JSON adapters #478

Closed brentwatson closed 4 months ago

brentwatson commented 1 year ago

Resolve issue https://github.com/ZacSweers/MoshiX/issues/460

Moshi JsonAdapters for kotlinx.collections.immutable collection types

Note the JsonAdapterFactory implementations in this PR are similar to the core Moshi versions of the adaptors (eg: MapJsonAdapter.kt).

Usage

Gradle dependency

dependencies {
  implementation("dev.zacsweers.moshix:moshi-adapters-kotlinx-immutable:<version>")
}

Immutable Collection Adapters

Use addKotlinXImmutableAdapters() to add PersistentListJsonAdapterFactory and PersistentMapJsonAdapterFactory type adapters. These will deserialize standard JSON list and dictionary types into kotlinx.collections.immutable PersistentList and PersistentMap types.

val moshi = Moshi.Builder()
  .addKotlinXImmutableAdapters()
  .build()

@JsonClass(generateAdapter = true)
data class MyImmutableModel(
  val stringList: PersistentList<String>, // Supports List of Strings
  val objList: PersistentList<SomeObject>, // Also supports List of Objects
  val stringMap: PersistentMap<String, String>, // Supports Map of <Strings, String>
  val objMap: PersistentMap<String, SomeObject>, // Also supports Map of <String, Object>
)
ZacSweers commented 1 year ago

Thanks for starting this! There are some things I think we need to do along the way.

ZacSweers commented 1 year ago

@brentwatson are you planning to continue this?

brentwatson commented 1 year ago

@ZacSweers yes, I was hoping to get to it this week, but wasn't able to. Next week I'm going to prep for DroidCon NY talk, and then AFK for a week after that so it likely won't be until late Sept / early Oct. If you want to close this you can and I can open a new PR when I have something ready. Or we can merge this as a v1 and I'll do Set and Collection support as a follow-up PR... up to you

On Sat, Sep 2, 2023, 8:36 PM Zac Sweers @.***> wrote:

@brentwatson https://github.com/brentwatson are you planning to continue this?

— Reply to this email directly, view it on GitHub https://github.com/ZacSweers/MoshiX/pull/478#issuecomment-1703990998, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKE6OW2OX5XJGJK4J4HM23XYPUKHANCNFSM6AAAAAA3V64PIQ . You are receiving this because you were mentioned.Message ID: @.***>

ZacSweers commented 1 year ago

No worries, just continue this PR when you're ready. Just asking as I'm about to cut a bugfix release

ZacSweers commented 4 months ago

Implemented in #586