Repository navigation
Description
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 fordynamicfields on models and endpoints #5013) wraps every value viaencodeWithType, so the column holds{"className":...,"data":...}envelopes —@>against the user's actual shape never matches. It also still throws on nested untyped maps:Protocol.wrapWithClassNamecallsgetClassNameForObject(data)!, which returnsnullfor an unregisteredMap<String, dynamic>and crashes on the non-null assertion.serializationDataType=jsonb(feat: Add jsonb column support and GIN index operator classes #4960) requires statically-knownT(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.
- spy.yaml type:
Json= JSON sum (null | bool | num | String | List<Json> | Map<String, Json>). - Allowed in both model fields and endpoint parameters / return types.
- Dart projection:
Object?(no typedef). - Codegen: identity
toJson/fromJson— no envelope, plain JSON in/out. - DB column: native
jsonb. Composes with feat: Add jsonb column support and GIN index operator classes #4960's GIN indexes /serializeAsJsonbByDefaultand Support JSON field type and add query interface for json/jsonb fields #4284's planned query interface. - Validation: codegen + runtime guard rejects non-JSON-native values (
DateTime,SerializableModel, etc.) with an error pointing todynamicfor the type-tagged case.
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+ manualjsonEncode/jsonDecode. Loses framework ergonomics; no jsonb queryability without further hacks. String+ Postgres-side generateddata_jsonbcolumn (GENERATED ALWAYS AS (data::jsonb) STORED) +unsafeQueryfor@>. 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 fordynamicfields 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
- Closed parent issue: Asymmetric handling of Map<String, dynamic>: serialization works, deserialization throws #4996 (asymmetric handling of
Map<String, dynamic>). - Settling PR for
dynamic: feat: Add support fordynamicfields on models and endpoints #5013 — solves in-framework type-tagged round-trip; orthogonal to this proposal. - First-class jsonb storage: feat: Add jsonb column support and GIN index operator classes #4960 / feat: Support lossless JSON <-> JSONB column type migration #4989 — assumes typed
T. - Open issue with struck-through "first-class JSON type" bullet: Support JSON field type and add query interface for json/jsonb fields #4284. This proposal revives that scope with a precise contract.
- Maintainer guidance on
dynamic: No deserialization found for type dynamic #2722, Storing JSON (Map<String,dynamic>) #2600.Jsonhonors it as a closed sum type with codegen-enforced validation, not a type-system backdoor.
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
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsBacklog 🗂️
Activity
marcelomendoncasoares commentedon Jun 26, 2026
Hi @ThangVuNguyenViet! Sorry for the delay in getting back to you. The
Mapserialization ondynamicshould be fixed, but you are right thatdynamicis not meant for storing arbitrary JSON data that needs to respect the original schema for queries. Your proposal is a good direction: a dedicatedJsontype that has special encoding/decoding and stores whatever is received in aColumnStructured(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
::jsonbcast. We should also be careful to distinguish SQLNULLfrom JSONnull. One possible design is to always wrap values in something likeJsonValue, wherenullmeans an absent nullable field / SQLNULL, whileJsonValue(null)means the JSON valuenull. The same wrapper would also let the encoder reliably handle top-level scalars like strings, numbers, and booleans by validating the value and applyingjsonEncodebefore storing or querying it asjsonb.