Skip to content

feat: add decodeAs<T>() extension for GBValue in kotlinx.serialization module#234

Open
vazarkevych wants to merge 3 commits into
growthbook:mainfrom
vazarkevych:feature/decode-as
Open

feat: add decodeAs<T>() extension for GBValue in kotlinx.serialization module#234
vazarkevych wants to merge 3 commits into
growthbook:mainfrom
vazarkevych:feature/decode-as

Conversation

@vazarkevych

Copy link
Copy Markdown
Collaborator

Summary

  • Add decodeAs<T>() inline extension function on GBValue that deserializes any GBValue into an arbitrary kotlinx.serializable type via gbSerialize()
  • Fix GBArray to be a data class with a val property so that equals() and hashCode() work correctly
  • Add comprehensive JVM tests for decodeAs, gbSerialize, and GBValue.from()
  • Bump GrowthBookKotlinxSerialization version 1.0.01.1.0

Usage

val value: GBValue = feature.value
val config = value.decodeAs<MyConfig>() // returns null on failure

@vazarkevych vazarkevych requested a review from madhuchavva May 18, 2026 07:27

@madhuchavva madhuchavva left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this. The helper and GBArray value-semantics fix are directionally right, but I think we should fix the release/versioning and default decoding behavior before merge.


class GBArray(
value: List<GBValue>
data class GBArray(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This GBArray equality/hashCode fix is legitimate and the data class change is the standard Kotlin approach. The release concern is that this lives in Core, while the PR only bumps GrowthBookKotlinxSerialization to 1.1.0. The generated serialization POM still resolves Core-jvm:1.2.0, so if Core 1.2.0 is already published without this change, consumers upgrading only the serialization artifact will not receive this fix. Can we coordinate a Core version bump/publish, or split this into a Core release so the shipped artifacts match the PR behavior?

import com.sdk.growthbook.model.GBNumber
import com.sdk.growthbook.model.GBString
import com.sdk.growthbook.model.GBBoolean
import kotlinx.serialization.json.Json

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the default decode path tolerant of unknown keys, or explicitly test/document that callers must pass a custom Json? Feature config objects commonly contain fields older app versions do not model yet, and Json is strict by default. In that case decodeAs<MyConfig>() returns null for an otherwise valid config with extra fields, which is a surprising failure mode for the advertised helper.


group = "io.growthbook.sdk"
version = "1.0.0"
version = "1.1.0"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this bumps the published serialization module and adds a new public helper, can we include a changelog/docs update before merge? A short usage snippet should also call out the JSON configuration behavior, especially if callers need Json { ignoreUnknownKeys = true } for partial feature config decoding.

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.

2 participants