Skip to content

codec: make JSON an untyped JSON value - #257

Merged
vito merged 13 commits into
mainfrom
json-any-value
Nov 9, 2026
Merged

vito merged 13 commits into
mainfrom
json-any-value

Conversation

@vito

@vito vito commented Nov 7, 2026 •

Copy link
Copy Markdown
Owner

Makes Dang's builtin JSON the type for JSON data whose shape isn't known ahead of time.

Why

Dang-owned scalars could only be built from strings, so this failed:

JSON.decode("{\"a\": [1, 2]}") :: Map[JSON!]!
# ["a"]: expected string for scalar JSON, got array

Since there was no "any" type, dagger/dagger.io#5802 checked modules.yaml for unknown keys by decoding into Map[Platform]. That works because a schema scalar happens to accept non-string values. To go a level deeper it then re-decoded toString(value).

What changes

For the builtin JSON, and for a scalar JSON declared in Dang (which merges with it):

  • Decoding accepts any value. JSON, YAML and TOML decode into JSON, Map[JSON!]!, [JSON!]! and JSON-typed fields. The value is stored as its compact JSON text.
  • A JSON value always holds JSON text. A string is stored with its quotes: "x" becomes the text "x", not x. toString(j) and ${j} return that text.
  • Walking goes one level at a time with JSON.decode(j). JSON values now degrade to String like other custom scalars, so JSON.decode accepts them directly. I picked this over making j :: T a cast, because :: would have needed inference changes.
    let top = YAML.decode(raw) :: Map[JSON!]!
    let mods = JSON.decode(top["modules"]!) :: [Map[JSON!]!]!
    
  • Encoding writes the data, not a quoted string. JSON.encode, YAML.encode and TOML.encode embed a JSON value as the data it holds, so JSON round-trips: same values, numbers keep their digits, object keys come out sorted. Text that isn't valid JSON raises an error. toString and .join of lists, maps and records that hold JSON values also render them as data, through main's directive-free toStringJSON. JSON.encode's indent, escapeHTML and sortKeys options apply inside JSON values too. ScalarValue.MarshalJSON and Object.MarshalJSON are unchanged, so state retention and values sent to Dagger behave as before.
  • YAML numbers that aren't valid JSON (0755, +1, -.5) are rewritten as the number :: Int! or :: Float! would read them. A number JSON can't represent (.inf) raises an error.
  • Strict decoding (strict: true) checks keys against a type, so it doesn't look inside a JSON value, the same as with a Map's keys. Repeated keys inside a JSON value are still rejected, and decoding the value again with strict: true checks keys at the type given there.
  • Strings are validated and compacted. A String that becomes a JSON value (a :: cast, a typed slot, a literal argument) must be valid JSON, and invalid text raises at that point: cannot use "{nope" as JSON: invalid JSON: .... A string literal is checked at compile time, so j == "b" is a type error with the hint a JSON string is written with its quotes: "\"b\"". Valid text is compacted with json.Compact, which removes whitespace but keeps keys in the order they were written.
  • JSON values compare as data. ==, contains, uniq and case ignore key order, string escapes and number spelling, so a cast of {"b": 1, "a": 2} equals the decoded {"a":2,"b":1}. Each value computes a canonical key once when it's created, so comparisons don't re-parse.
  • JSON null. Where null is allowed (JSON, Map[JSON]!), JSON null becomes Dang null. In a JSON! it's stored as the JSON value null, so JSON! accepts any JSON at all. This keeps JSON.encode(null) :: JSON! working for Dagger entrypoints that answer void functions.

Schema scalar JSON is unchanged

A scalar JSON that comes from a schema (SourceSchema != nil, e.g. Dagger.JSON) still behaves like any other schema scalar: a string stays bare, and other values are stored as their JSON and sent back as those values. Servers send this scalar differently: Dagger sends JSON text inside a string, while graphql-type-json-style servers send the raw value. So Dang keeps whatever the server sends.

Unqualified JSON always means the builtin, because imports never shadow builtins. That means YAML.decode(...) :: Map[JSON!]! works inside Dagger modules. To pass a value to a schema JSON argument, convert it through String: (j :: String!) :: Dagger.JSON!.

Release notes

Both changes below were accepted with no version gate: they're runtime behavior, not syntax.

  • Decoded JSON strings keep their quotes. Decoding a JSON string into the builtin JSON used to give the bare string; it now gives the JSON text, quotes included ("x"). A comparison like m["mode"]! == "fast" is now a compile error: write m["mode"]! == "\"fast\"", or decode into String! to get the old result.
  • Encoding a JSON value made from a String writes the JSON. JSON.encode(j) used to output that value as a quoted string; it now embeds the JSON text. tests/test_codec.dang and tests/test_json_import.dang pinned the old output and are updated.
  • Strings cast to JSON must be valid JSON. A String cast to the builtin JSON is now validated and compacted, and invalid text or a repeated key raises where it's converted. A non-JSON string literal in a JSON slot is now a compile error.
  • "null" :: JSON is Dang null; "null" :: JSON! is the JSON value null.

Known limits (documented)

  • JSON.encode escapes <, > and & in embedded text, the same way it already does for every string.
  • A JSON value made from a String keeps its written key order when passed to JSON.encode. Decoded values come out with keys sorted. Equality ignores key order either way.
  • TOML floats go through float64, so 2.0 is stored as 2. YAML numbers that aren't valid JSON are rewritten and can lose precision.

Not in this PR

  • A schema's scalar JSON (e.g. Dagger.JSON) is unchanged here. A stacked follow-up treats it as JSON text carried in a string, which is Dagger's convention.
  • No automatic conversion between the builtin JSON and a schema's JSON. Convert explicitly with (j :: String!) :: Dagger.JSON!.

Tests and docs

  • New tests/test_json_value.dang covers decoding any value, walking, exact round-trips, YAML/TOML input, YAML numbers, toString/.join of collections, null handling, validation and compaction of casts, and data equality. New error goldens: json_cast_invalid (a runtime cast of invalid text) and json_literal_not_json (the compile-time literal check).
  • tests/test_json_import.dang covers the builtin alongside an imported scalar JSON, and pins that the schema scalar's behavior is unchanged.
  • Docs: a new "Data of unknown shape" section in docs/lit/data/json-yaml.md, a note in enums-scalars.md, and updates to the dang-language skill's stdlib.md and types.md. I also corrected the existing claims that a schema JSON scalar "merges" with the builtin: only encode/decode are attached to it.

vito added 3 commits November 7, 2026 02:54
Decoding JSON, YAML or TOML into Dang's JSON (the builtin, or a `scalar
JSON` declared in Dang) now accepts a value of any shape and holds it as
its compact JSON text. A string is held quoted ("x" as `"x"`), so a JSON
value is always JSON text: toString and interpolation give that text,
JSON.decode(j) re-materializes it at a type (JSON values now degrade to
String like other scalars), and JSON/YAML/TOML.encode embed it as the
data it holds rather than as a string, so documents round-trip. toString
and join of collections holding JSON values render them as data too.
YAML numbers not spelled as JSON (0755, +1, -.5) are respelled as the
number `:: Int!`/`:: Float!` would read.

Before, `Map[JSON!]` failed on any non-string value, so walking data of
unknown shape needed workarounds such as `Map[Platform]` plus re-decoding
`toString(value)` (dagger/dagger.io#5802).

A schema's `scalar JSON` is left as any schema scalar: servers disagree
on its wire form (Dagger sends JSON text in a string, graphql-type-json
style servers send the value itself), so its values stay as the server
sends them. Unqualified `JSON` is always the builtin, since imports never
shadow builtins.

JSON.encode of a builtin JSON value used to quote it; the tests pinning
that are updated.

Signed-off-by: Alex Suraci <[email protected]>
Add a "Data of unknown shape" section to the JSON/YAML/TOML page, note it
from the custom scalars page, and describe the JSON-text rule (and how a
schema's `scalar JSON` differs) in the dang-language skill's stdlib and
types references.

Signed-off-by: Alex Suraci <[email protected]>
The codec pages said a schema's `scalar JSON` merges with the builtin;
only the encode/decode members do, unqualified `JSON` stays the builtin,
and the schema's scalar keeps its own values. Also note the JSON value
caveats review turned up: decoded strings now keep their quotes, TOML
floats lose their spelling, JSON.encode escapes <, > and &, and String
casts are unvalidated text; and that builtin JSON values degrade to
String.

Signed-off-by: Alex Suraci <[email protected]>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 578c79a
Status: ✅  Deploy successful!
Preview URL: https://e73a6abd.dang-3kk.pages.dev
Branch Preview URL: https://json-any-value.dang-3kk.pages.dev

View logs

vito added 10 commits November 8, 2026 23:49
A String becoming a builtin JSON value (a `::` cast, a typed slot, a
literal argument or comparison) must now be JSON: invalid text raises at
that point, naming the text and the parse error, instead of later when
encoding. Valid text is compacted with json.Compact, which drops
whitespace but keeps keys in the order written, so equal data compares
equal: a cast of `{"a": 1}` is `==` the decoded `{"a":1}`.

Signed-off-by: Alex Suraci <[email protected]>
Main's skill now spells out which scalars decode from non-strings; the
builtin JSON is now one of them.

Signed-off-by: Alex Suraci <[email protected]>
A cast keeps the key order and spelling it was written with, while a
decode re-encodes with sorted keys, so text equality made a cast and a
decode of the same object unequal. `==` (and contains, uniq, case) on
two JSON values now compares the data they hold: keys in any order,
numbers by value, strings after unescaping.

A String of `null` cast to JSON is now Dang null, or raises for JSON!,
as decoding JSON null does, rather than a non-null value holding null.

Signed-off-by: Alex Suraci <[email protected]>
A string literal in a JSON slot (an argument, `==`, a case arm) that is
not JSON is now a compile error with a quoting hint, like an enum
literal that names no value, instead of raising when the comparison
runs: `j == "b"` almost always means `j == "\"b\""`.

JSON values carry a canonical key (sorted keys, numbers by value) made
once when they are created, so comparing them in uniq/contains doesn't
re-parse both texts each time. join writes JSON values as their text,
as toString does.

Signed-off-by: Alex Suraci <[email protected]>
A non-null JSON slot raised on JSON null, so `"null" :: JSON!` and
`j != "null"` failed at runtime, and a Dagger entrypoint answering a
void function with `JSON.encode(null) :: JSON!` broke. JSON null is now
Dang null only where null may go (`JSON`, `Map[JSON]!`); a `JSON!`
holds it as the JSON value null, so `JSON!` takes any JSON at all.

Also correct comments on how builtin scalars merge and the skill's
custom-scalar example, which used `scalar JSON` as a plain scalar.

Signed-off-by: Alex Suraci <[email protected]>
A JSON! holding JSON null that flows into a nullable JSON slot becomes
Dang null there, so a nullable JSON has one null however it was made.
The quoting hint for a non-JSON literal is only given when the literal
looks like a bare word (not an attempted object, list or string), and
no longer HTML-escapes it. Docs note that JSON.encode keeps a cast
value's written key order.

Signed-off-by: Alex Suraci <[email protected]>
Strict decoding checks keys against a type, so a JSON value's insides
are data and are not checked, like a Map's keys, while keys around it
and keys repeated inside it still are; decoding the value again applies
strict at the type given there. Tested, and noted in the strict docs.

Signed-off-by: Alex Suraci <[email protected]>
JSON.encode now writes fields in declaration order and takes indent,
escapeHTML and sortKeys (#255). A JSON value goes through them like any
other object: a cast keeps its written key order unless sortKeys sorts
it, indent lays out its insides, and escapeHTML applies to its strings.
Tested, and the record test now expects declaration order.

Signed-off-by: Alex Suraci <[email protected]>
A JSON value holding JSON null reached a nullable slot through a merge
(an if/else, a list literal) without widening, so `== null` said false;
equality now treats a Dang-owned JSON null as null. A String cast to JSON
(or a JSON literal) with a repeated key is rejected, as strict decoding
rejects it, since its text would not decode back strictly. JSON.encode
checks a JSON value's text with json.Valid rather than decoding it.

Signed-off-by: Alex Suraci <[email protected]>
The skill's stdlib reference now says a String cast to JSON with a
repeated key raises, as the code does.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito merged commit 4132977 into main Nov 9, 2026
9 checks passed
vito added a commit that referenced this pull request Nov 9, 2026
#257 (unreleased) made the type named JSON an untyped value held as JSON
text, which put format special cases in the checker, materialization,
equality, enum literal checks, List.join, the encoder and ScalarValue
(isJSONScalar, jsonKey). Decoded now names data of any shape without
belonging to a format, so the JSON value type goes: JSON is a plain
namespace like YAML and TOML (and Random). `:: JSON` code moves to
`:: Decoded!` (or a schema's own scalar, `Dagger.JSON`), and String-to-JSON
casts to `JSON.decode(s)`.

The lit docs drop the "Data of unknown shape" section (its cases are
covered by #decoded-values) and point its references there.
vito added a commit that referenced this pull request Nov 9, 2026
#257 (unreleased) made the type named JSON an untyped value held as JSON
text, which put format special cases in the checker, materialization,
equality, enum literal checks, List.join, the encoder and ScalarValue
(isJSONScalar, jsonKey). Decoded now names data of any shape without
belonging to a format, so the JSON value type goes: JSON is a plain
namespace like YAML and TOML (and Random). `:: JSON` code moves to
`:: Decoded!` (or a schema's own scalar, `Dagger.JSON`), and String-to-JSON
casts to `JSON.decode(s)`.

The lit docs drop the "Data of unknown shape" section (its cases are
covered by #decoded-values) and point its references there.
vito added a commit that referenced this pull request Nov 9, 2026
#257 (unreleased) made the type named JSON an untyped value held as JSON
text, which put format special cases in the checker, materialization,
equality, enum literal checks, List.join, the encoder and ScalarValue
(isJSONScalar, jsonKey). Decoded now names data of any shape without
belonging to a format, so the JSON value type goes: JSON is a plain
namespace like YAML and TOML (and Random). `:: JSON` code moves to
`:: Decoded!` (or a schema's own scalar, `Dagger.JSON`), and String-to-JSON
casts to `JSON.decode(s)`.

The lit docs drop the "Data of unknown shape" section (its cases are
covered by #decoded-values) and point its references there.
vito added a commit that referenced this pull request Nov 9, 2026
#257 (unreleased) made the type named JSON an untyped value held as JSON
text, which put format special cases in the checker, materialization,
equality, enum literal checks, List.join, the encoder and ScalarValue
(isJSONScalar, jsonKey). Decoded now names data of any shape without
belonging to a format, so the JSON value type goes: JSON is a plain
namespace like YAML and TOML (and Random). `:: JSON` code moves to
`:: Decoded!` (or a schema's own scalar, `Dagger.JSON`), and String-to-JSON
casts to `JSON.decode(s)`.

The lit docs drop the "Data of unknown shape" section (its cases are
covered by #decoded-values) and point its references there.
vito added a commit that referenced this pull request Nov 9, 2026
#257 (unreleased) made the type named JSON an untyped value held as JSON
text, which put format special cases in the checker, materialization,
equality, enum literal checks, List.join, the encoder and ScalarValue
(isJSONScalar, jsonKey). Decoded now names data of any shape without
belonging to a format, so the JSON value type goes: JSON is a plain
namespace like YAML and TOML (and Random). `:: JSON` code moves to
`:: Decoded!` (or a schema's own scalar, `Dagger.JSON`), and String-to-JSON
casts to `JSON.decode(s)`.

The lit docs drop the "Data of unknown shape" section (its cases are
covered by #decoded-values) and point its references there.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant