Repository navigation
codec: make JSON an untyped JSON value - #257
Merged
Merged
Conversation
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]>
Deploying dang with
|
| 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 |
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]>
This was referenced Nov 9, 2026
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Makes Dang's builtin
JSONthe 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:
Since there was no "any" type, dagger/dagger.io#5802 checked
modules.yamlfor unknown keys by decoding intoMap[Platform]. That works because a schema scalar happens to accept non-string values. To go a level deeper it then re-decodedtoString(value).What changes
For the builtin
JSON, and for ascalar JSONdeclared in Dang (which merges with it):JSON,Map[JSON!]!,[JSON!]!andJSON-typed fields. The value is stored as its compact JSON text."x"becomes the text"x", notx.toString(j)and${j}return that text.JSON.decode(j). JSON values now degrade toStringlike other custom scalars, soJSON.decodeaccepts them directly. I picked this over makingj :: Ta cast, because::would have needed inference changes.JSON.encode,YAML.encodeandTOML.encodeembed 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.toStringand.joinof lists, maps and records that hold JSON values also render them as data, through main's directive-freetoStringJSON.JSON.encode'sindent,escapeHTMLandsortKeysoptions apply inside JSON values too.ScalarValue.MarshalJSONandObject.MarshalJSONare unchanged, so state retention and values sent to Dagger behave as before.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: true) checks keys against a type, so it doesn't look inside a JSON value, the same as with aMap's keys. Repeated keys inside a JSON value are still rejected, and decoding the value again withstrict: truechecks keys at the type given there.::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, soj == "b"is a type error with the hinta JSON string is written with its quotes: "\"b\"". Valid text is compacted withjson.Compact, which removes whitespace but keeps keys in the order they were written.==,contains,uniqandcaseignore 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,Map[JSON]!), JSON null becomes Dangnull. In aJSON!it's stored as the JSON valuenull, soJSON!accepts any JSON at all. This keepsJSON.encode(null) :: JSON!working for Dagger entrypoints that answer void functions.Schema
scalar JSONis unchangedA
scalar JSONthat 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
JSONalways means the builtin, because imports never shadow builtins. That meansYAML.decode(...) :: Map[JSON!]!works inside Dagger modules. To pass a value to a schema JSON argument, convert it throughString:(j :: String!) :: Dagger.JSON!.Release notes
Both changes below were accepted with no version gate: they're runtime behavior, not syntax.
JSONused to give the bare string; it now gives the JSON text, quotes included ("x"). A comparison likem["mode"]! == "fast"is now a compile error: writem["mode"]! == "\"fast\"", or decode intoString!to get the old result.JSON.encode(j)used to output that value as a quoted string; it now embeds the JSON text.tests/test_codec.dangandtests/test_json_import.dangpinned the old output and are updated.JSONmust be valid JSON. A String cast to the builtinJSONis 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" :: JSONis Dangnull;"null" :: JSON!is the JSON valuenull.Known limits (documented)
JSON.encodeescapes<,>and&in embedded text, the same way it already does for every string.JSON.encode. Decoded values come out with keys sorted. Equality ignores key order either way.2.0is stored as2. YAML numbers that aren't valid JSON are rewritten and can lose precision.Not in this PR
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.JSONand a schema'sJSON. Convert explicitly with(j :: String!) :: Dagger.JSON!.Tests and docs
tests/test_json_value.dangcovers decoding any value, walking, exact round-trips, YAML/TOML input, YAML numbers,toString/.joinof collections, null handling, validation and compaction of casts, and data equality. New error goldens:json_cast_invalid(a runtime cast of invalid text) andjson_literal_not_json(the compile-time literal check).tests/test_json_import.dangcovers the builtin alongside an importedscalar JSON, and pins that the schema scalar's behavior is unchanged.docs/lit/data/json-yaml.md, a note inenums-scalars.md, and updates to the dang-language skill'sstdlib.mdandtypes.md. I also corrected the existing claims that a schema JSON scalar "merges" with the builtin: onlyencode/decodeare attached to it.