Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kotlinx immutable collections JSON adapters #478

Conversation

brentwatson
Copy link

Resolve issue #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
Copy link
Owner

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

  • support Sets
  • support Collection
  • simplify the API to just ImmutableCollectionsJsonAdapter.Factory, rather than one for each.

@ZacSweers
Copy link
Owner

@brentwatson are you planning to continue this?

@brentwatson
Copy link
Author

brentwatson commented Sep 3, 2023 via email

@ZacSweers
Copy link
Owner

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

@ZacSweers
Copy link
Owner

Implemented in #586

@ZacSweers ZacSweers closed this May 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants