84 JSON value roundtrip
A @Serializable value has a checked JSON shape. toJson encodes its fields; fromJson<Note> decodes back to the declared type rather than leaving a dynamic tree.
norm
import std.serialization.Serializable
import std.json.fromJson
import std.json.toJson
@Serializable()
value Note {
String title
Integer priority
}
main() {
Note original = Note(title: "Learn Norm", priority: 2)
String encoded = original.toJson()
Note restored = encoded.fromJson<Note>()
printLine(encoded)
printLine(restored.title)
printLine(restored.priority)
}Expected output:
text
{"title":"Learn Norm","priority":2}
Learn Norm
2Try it: Change priority to 3 and compare the encoded and decoded values.
Precise rules: JSON API.