Skip to content

Allow explode and implode definitions to specify object or array shape #151

Description

@kjellmorten

Context

explode can represent both objects and arrays as arrays of { key, value } entries. implode currently infers the source shape from the key types:

  • numeric keys produce an array
  • string keys produce an object

This inference cannot distinguish an empty object from an empty array because both explode to []. Consequently, an empty-array round trip currently behaves like this:

explode([]) // []
implode([]) // {}

Suggestion

Allow the transformer definition to specify the intended shape explicitly, for example:

{ $transform: "explode", type: "array" }
{ $transform: "explode", type: "object" }

The exact property name is open for discussion. When provided, it could:

  • resolve the empty-input ambiguity in the inverse direction
  • optionally validate that entry keys are compatible with the declared shape
  • make the transformation contract clearer for integrations with a known schema

Without an explicit shape, the current key-based inference could remain the default for compatibility.

Acceptance considerations

  • Add forward and reverse tests for empty objects and empty arrays.
  • Document behavior when the declared shape conflicts with entry key types.
  • Decide whether the option belongs to both explode and implode, or whether direction handling makes one shared option sufficient.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions