Skip to content

feat: LDContext is java.io.Serializable#97

Open
joshfriend wants to merge 1 commit into
launchdarkly:mainfrom
joshfriend:ldcontext-serializable
Open

feat: LDContext is java.io.Serializable#97
joshfriend wants to merge 1 commit into
launchdarkly:mainfrom
joshfriend:ldcontext-serializable

Conversation

@joshfriend

@joshfriend joshfriend commented Jan 8, 2026

Copy link
Copy Markdown

Requirements

  • I have added test coverage for new or changed functionality
  • I have followed the repository's pull request submission guidelines
  • I have validated my changes against all supported platform versions

Describe the solution you've provided

I Added Serializable to any base classes and interfaces needed to make LDContext itself serializable

Describe alternatives you've considered

My workaround for this was to manually serialize the LDContext to json because string is serializable and deserialize it again

Additional context

I was trying to use launchdarkly sdks inside of some gradle tooling that configures build features based on feature flags. To do this I wrote a ValueSource which took the following parameters:

  • Feature flag key
  • SDK key
  • LDContext

And gradle threw an error because LDContext is not serializable, and therefore not allowed as a ValueSourceParameters property.

I'm hoping that code will be open source soon, but for now here's a snippet:

abstract class LDBooleanValueSource: ValueSource<Boolean, LDBooleanValueSource.Params> {
  interface Params : ValueSourceParameters {
    val flagName: Property<String>
    val sdkKey: Property<String>
    val context: Property<LDContext> // Not serializable!
  }

  companion object {
    private val config: LDConfig = LDConfig.Builder().build()

    @JvmStatic
    fun ProviderFactory.launchDarklyBoolean(flagName: String, context: LDContext): Provider<Boolean> {
      return of(LDBooleanValueSource::class.java) {
        it.parameters.flagName.set(flagName)
        it.parameters.sdkKey.set(gradleProperty("launchdarkly.sdkKey"))
        it.parameters.context.set(context)
      }
    }
  }

  private val client by lazy { LDClient(parameters.sdkKey.get(), config) }

  override fun obtain(): Boolean =
    client.allFlagsState(parameters.context.get())
      .getFlagValue(parameters.flagName.get())
      .booleanValue()
}

Note

Low Risk
Additive API change on marker interfaces and internal types; behavior is covered by new serialization round-trip tests with no runtime path changes for normal SDK usage.

Overview
Enables Java native serialization (ObjectOutputStream / ObjectInputStream) for LDContext, so it can be used in frameworks that require Serializable parameters (e.g. Gradle ValueSource configuration) without JSON workarounds.

JsonSerializable now extends java.io.Serializable, which propagates serializability to LDContext and related JSON-modeled SDK types. AttributeMap is also marked Serializable directly. LDValueBool and LDValueNull add readResolve() so deserialized values restore the canonical singleton instances—required for equals() to behave correctly after a round trip.

New tests in LDContextTest assert single- and multi-kind contexts survive a Java serialize/deserialize cycle and remain equalTo the original.

Reviewed by Cursor Bugbot for commit f1ed0c7. Bugbot is set up for automated code reviews on this repo. Configure here.

@joshfriend joshfriend requested a review from a team as a code owner January 8, 2026 02:36
@joshfriend joshfriend force-pushed the ldcontext-serializable branch from fa5cb4a to f198141 Compare January 8, 2026 13:36
@joshfriend joshfriend changed the title JsonSerializable extends java.io.Serializable feat: LDContext is java.io.Serializable Jan 8, 2026
@tanderson-ld

Copy link
Copy Markdown
Contributor

Hi @joshfriend and thank you for the contribution. I will try to take a look in the next day or two.

@tanderson-ld tanderson-ld 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 your patience this week.

* Builder methods are not thread-safe.
*/
public final class ArrayBuilder {
public final class ArrayBuilder implements Serializable {

@tanderson-ld tanderson-ld Jan 16, 2026

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.

It seems a little unusual to me to make a builder serializable. Do you know which structure is leading the default serialization here? I don't see the dependency from LDContext to here.

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.

Is it the ArrayBuilder in LDValue?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I backed this one out, it doesn't need to be serializable.

@tanderson-ld tanderson-ld added the waiting for feedback Indicates LaunchDarkly is waiting for customer feedback before issue is closed due to staleness. label Mar 6, 2026
@github-actions github-actions Bot added the Stale label Jun 5, 2026
@joshfriend joshfriend force-pushed the ldcontext-serializable branch from f198141 to f1ed0c7 Compare June 11, 2026 14:31
@joshfriend

Copy link
Copy Markdown
Author

I rebased this and added a roundtrip serialization test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Stale waiting for feedback Indicates LaunchDarkly is waiting for customer feedback before issue is closed due to staleness.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants