JSON projections
witforge can participate in three different JSON shapes. They project the same WIT values but do not define one interchangeable JSON wire format.
For this WIT:
record diagnostic { start-byte: u32, id: u64, note: option<string>,}the default shapes are:
| Projection | Representative value |
|---|---|
generated Rust with --serde |
{"start_byte":4,"id":9,"note":null} |
| generated JSON Schema (Draft 7 / 2020-12) | {"start-byte":4,"id":9,"note":null} |
| emitted protobuf through proto3-JSON | {"startByte":4,"id":"9"} |
Plain Rust Serde
Section titled “Plain Rust Serde”--serde adds plain, format-independent #[derive(::serde::Serialize, ::serde::Deserialize)]
attributes to generated records, enums, variants, and flags. Derived Serde uses the generated Rust
spelling rather than the WIT spelling:
- Fields and flags:
start-bytebecomes"start_byte", upper-case words fold to Rust casing (URLbecomes"url"), non-raw escaped keywords keep their trailing underscore (%selfis keyed"self_"), and raw Rust identifiers dropr#(%typebecomesr#typeand is keyed"type"). - Enums and variants: enum cases serialize as PascalCase strings (
done-now->"DoneNow"), payload-less variant cases serialize as unit strings ("Started"), and payload variant cases use externally tagged single-key objects ({"Item": {...}}). - Bytes, characters, fixed-length lists, maps, and integers:
list<u8>(including byte aliases) andlist<u8, N>serialize as JSON arrays of integers ([0, 127, 255]), not base64; arrays with lengthN > 32(including insideOption,Vec,BTreeMap, orResult) use a private generated#[serde(with = "...")]helper module with no extra crate dependencies.charserializes as a single-character JSON string, andmap<K, V>(BTreeMap<K, V>) serializes as a JSON object with stringified keys in deterministic key order. Rust integer types serialize as exact JSON numbers, includingu64ands64(JavaScriptJSON.parseloses precision above±2^53). - Reading and evolution: derived Serde treats an omitted
option<T>field asNone(writingNoneasnull) and silently ignores unknown fields. Reading av0.1JSON document directly asv0.2therefore yieldsNonefor an addedoption<T>field, but fails with a missing-field error for an addedlist<T>ormap<K, V>field (unlike generatedconvert.rs, which defaultsoption<T>,list<T>, andmap<K, V>). serde_jsonlosses: because JSON has no distinct representation for nested nulls or non-finite floats,serde_jsonserializesSome(None)on nested options (option<option<T>>, through aliases, or in lists) asnulland deserializes it asNone, and serializesNaN/±Infinityasnull(which fails to deserialize for required fields/lists and deserializes asNoneinsideoption<f32>/option<f64>). Formats that tagOptionor encode binary IEEE-754 floats round-trip both. See the emitter specification section 3.3 for the full mapping table.
JSON Schema (Draft 7 / Draft 2020-12)
Section titled “JSON Schema (Draft 7 / Draft 2020-12)”The schema preserves the WIT member spelling, including start-byte (Draft 7 is the default with
definitions; opt-in Draft 2020-12 uses $defs and standardizes deprecated: true, which
witforge also emits as an annotation in Draft 7 with the same JSON value shape). Every record property is
required and records set additionalProperties: false. option<T> permits null as the
property’s value; it does not make the property omittable (option<option<T>> shares null for
None and Some(None), as in Serde). Integer schemas use the WIT type’s exact numeric minimum
and maximum (JSON parsers using IEEE 754 doubles are exact only up to ±2^53, and Draft 7
integer accepts 1.0). f32 carries its finite bounds (-3.4028235e38 to 3.4028235e38)
while accepting in-range decimals that round to f32; NaN and ±Infinity have no JSON form.
char is a JSON string with minLength: 1 and maxLength: 1 (Unicode scalar values); fixed-length
list<T, N> is an array with minItems: N and maxItems: N (for list<u8, N>, an array of
0..=255 integers, matching list<u8>); and map<K, V> is an object with additionalProperties
set to the value schema and propertyNames constraining non-string keys (bool, integer
patterns, or single-character char keys).
Every WIT result (including result<T>, result<_, E>, and bare result) is a closed single-key
object with "ok" or "err", using null for a side without a payload (matching payload-less
variant cases):
| WIT value | Generated Rust --serde |
JSON Schema |
|---|---|---|
result<u32, string> (Ok(7) / Err("e")) |
{"Ok":7} / {"Err":"e"} |
{"ok":7} / {"err":"e"} |
result<u32> (Ok(7) / Err(())) |
{"Ok":7} / {"Err":null} |
{"ok":7} / {"err":null} |
result<_, string> (Ok(()) / Err("e")) |
{"Ok":null} / {"Err":"e"} |
{"ok":null} / {"err":"e"} |
bare result (Ok(()) / Err(())) |
{"Ok":null} / {"Err":null} |
{"ok":null} / {"err":null} |
The schema describes a WIT-shaped JSON value. It does not describe Serde’s Rust-identifier shape or protobuf’s JSON mapping.
Proto3-JSON
Section titled “Proto3-JSON”The emitted .proto determines this projection. Its standard JSON mapping uses:
- lowerCamelCase output names, so protobuf
start_bytebecomesstartByte; parsers also accept the original protobuf spellingstart_byte; - decimal strings for
u64ands64; - base64 strings for
list<u8>and fixed-lengthlist<u8, N>bytes, unsigned integer code points (uint32) forchar, JSON arrays for otherlist<T>/list<T, N>(wrapped as{"values": [...]}when nested), JSON objects with stringified protobuf keys formap<K, V>(decimaluint32strings forcharkeys, wrapper objects when values are lists/maps/options/results, and{"entries": {...}}when the map itself is nested), and strings for non-finite floats; - enum names rather than integer values in normal output;
- omitted fields for absent optional values. A WIT
option<T>containingNoneis therefore normally absent rather than the explicitnullemitted by Rust Serde.
Parsing valid proto3-JSON does not enforce all WIT semantics. Required presence, narrow integer ranges, closed enums, and selected variant/result cases still need the checks described in Non-Rust implementations or the built-in checked glue emitters.
Application defaults
Section titled “Application defaults”WIT does not define defaults such as “missing severity means warning.” Keep application defaults in an explicit adapter and test them with fixtures.
Choosing a JSON boundary
Section titled “Choosing a JSON boundary”Before adopting generated output for an existing protocol:
- choose exactly one authoritative JSON profile;
- test full and minimal payloads, naming,
null, omission, 64-bit integers, enums, and bytes; - keep a custom wire schema or adapter when the existing protocol differs;
- do not validate one projection with another projection’s schema;
- do not describe a custom application profile as generated unless its actual names and presence rules match.
An opt-in naming mode could align future Serde output with WIT names, or a future schema mode could describe Serde names. Either changes the JSON behavior of generated per-version types and must not be applied retroactively to a frozen version.