Skip to content

Native jsonb storage for opaque user-defined JSON (schema unknown at compile time) #5094

Open

Description

@ThangVuNguyenViet

Problem to Solve

Serverpod has no way to declare a field whose type itself is JSON (schema unknown at compile time) and have it stored as native Postgres jsonb. Existing features all assume the developer owns the schema:

  • dynamic / Map<String, dynamic> (feat: Add support for dynamic fields on models and endpoints #5013) wraps every value via encodeWithType, so the column holds {"className":...,"data":...} envelopes — @> against the user's actual shape never matches. It also still throws on nested untyped maps: Protocol.wrapWithClassName calls getClassNameForObject(data)!, which returns null for an unregistered Map<String, dynamic> and crashes on the non-null assertion.
  • serializationDataType=jsonb (feat: Add jsonb column support and GIN index operator classes #4960) requires statically-known T (List<String>, Map<String, String>, typed model, etc.). Doesn't apply when the type is JSON.

So users with opaque JSON have to fall back to String + manual jsonEncode/jsonDecode on both server and client, plus hand-rolled SQL for any jsonb operator.

Proposal

Add a Json schema-level keyword in .spy.yaml. Distinct from dynamic (#5013); does not change its behavior.

Honors the "we always know the type" rule from #2722 / #2600 — Json is a precise schema-level type with a closed sum, opaque-by-design the way Postgres jsonb and GraphQL JSONScalar are.

Use Case

We're building dart_desk_be, a Serverpod-based headless CMS. Each Document has a data field whose schema is defined by end users at runtime — we don't own it and cannot model it as a typed class.

We need native jsonb storage so Postgres operators work on the actual user payload:

SELECT * FROM documents WHERE data @> '{"slug":"about"}'::jsonb;
SELECT data->'title' FROM documents WHERE ...;
-- jsonb_path_query, GIN indexes, etc.

Same shape applies to webhook receivers, audit logs, analytics events, and any gateway/passthrough endpoint where the schema is foreign data.

Alternatives

  • Stay on String + manual jsonEncode/jsonDecode. Loses framework ergonomics; no jsonb queryability without further hacks.
  • String + Postgres-side generated data_jsonb column (GENERATED ALWAYS AS (data::jsonb) STORED) + unsafeQuery for @>. What we currently do — works but is invisible to Serverpod tooling and has to be hand-edited into every migration.
  • Map<String, dynamic> post-feat: Add support for dynamic fields on models and endpoints #5013. Crashes on nested untyped maps (see Problem). Even when it doesn't crash, storage is the tagged envelope, not the user shape — @> queries miss.
  • Model the schema with typed classes. Not an option when end users own the schema.

Additional context

Real-world workaround currently in use: public_content_endpoint.dart:165-235, document_endpoint.dart:418-430.

Happy to submit a PR if the proposal lands favorably.

How experienced are you with this library?

Intermediate - Familiar with the basics or have used it in a few projects

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. added
    enhancement
    on May 9, 2026
  2. marcelomendoncasoares commented on Jun 26, 2026

    @marcelomendoncasoares
    Collaborator

    Hi @ThangVuNguyenViet! Sorry for the delay in getting back to you. The Map serialization on dynamic should be fixed, but you are right that dynamic is not meant for storing arbitrary JSON data that needs to respect the original schema for queries. Your proposal is a good direction: a dedicated Json type that has special encoding/decoding and stores whatever is received in a ColumnStructured (jsonb) column type.

    The main concern is that query encoding needs to be aware of the field type, so the value is converted to valid JSON text before receiving the ::jsonb cast. We should also be careful to distinguish SQL NULL from JSON null. One possible design is to always wrap values in something like JsonValue, where null means an absent nullable field / SQL NULL, while JsonValue(null) means the JSON value null. The same wrapper would also let the encoder reliably handle top-level scalars like strings, numbers, and booleans by validating the value and applying jsonEncode before storing or querying it as jsonb.

  3. added
    area: models
    area: database
    on Jun 26, 2026
  4. moved this from Inbox 📫 to Backlog 🗂️ in Serverpod Frameworkson Jun 26, 2026
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

    area: databasearea: modelsenhancement

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions