Skip to content

Serialize signal values consistently #17

Description

@PauloCarvalho13

Structured signal values are currently serialized only when a signal is initialized through dataSignal or dataSignals.

val person = dataSignal("person", Person("John Doe", 18))

This correctly renders the initial value as a JavaScript object:

$person = {name: 'John Doe', age: 18}

However, assigning a new Kotlin value through setValue does not use the same serialization logic:

person.setValue(Person("Jane Doe", 21))

The value is currently inserted directly into the generated JavaScript expression rather than being rendered as a JavaScript object literal. This leads to inconsistent behavior between signal initialization and signal updates, and prevents structured values from being safely assigned after initialization.
Serialization of a Kotlin signal value should be owned by the signal API and applied consistently wherever a value is emitted into a Datastar expression.

Possible API

@DatastarSignal
data class Person(
    val name: String,
    val age: Int,
)
val person = dataSignal("person", Person("John Doe", 18))

dataOn(Event.Click) {
    person.setValue(Person("Jane Doe", 21))
}

Expected generated expression:

$person = {name: 'Jane Doe', age: 21}

Assignments from an existing Datastar expression should remain distinct from assignments of Kotlin values:

person.setValue(otherPerson)

Acceptance criteria

  • A signal value is rendered consistently during both initialization and setValue.
  • Nested typed values are serialized recursively into JavaScript object literals.
  • Strings remain JavaScript strings and are not interpreted as raw JavaScript objects.
  • Structured values are not rendered through arbitrary toString() output.
  • Existing escaping behavior for object keys and string values is preserved.
  • Tests cover signal initialization and setValue for scalars, nested values, null values, and escaped strings.

Related to: #15
Related to: #16

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions