Generate ordinary mttp_core JSON codecs from annotated MoonBit types.
moon add gaato/mttp_core///|
#mttp.schema
pub struct Note {
/// A short title.
title : String
#mttp.nullable
rating : Int32?
}moonx gaato/mttp_schemagen@0.1.1 model_schema.mbt model.mbt
moon check --deny-warn
moonx gaato/mttp_schemagen@0.1.1 --check model_schema.mbt model.mbtrule(
name: "schema",
command: "moonx gaato/mttp_schemagen@0.1.1 $output $input",
)
dev_build(rule: "schema", input: "model.mbt", output: "model_schema.mbt")| MoonBit type | Codec | JSON behaviour |
|---|---|---|
| Int / Int32, Int64, Double, Bool, String, Json | jint, jint64, jdouble, jbool, jstring, jvalue | |
| Array[T], Map[String, T] | array, map_of | |
| T? | option | Absent and null both give None. None is omitted when encoding. |
| T? with #mttp.omittable | omittable | Absent gives None. null goes to T's codec. |
| T? with #mttp.nullable | nullable | The key is required. null is None, and None encodes as null. |
| @jsonkit.Presence[T] | presence | Absent, null and a value stay distinct. |
| Another annotated type in the same package | its <name>_codec |
///|
#mttp.schema
#mttp.discriminator("kind")
enum Response {
#mttp.tag("note-v1")
Note(Note)
#mttp.unknown
Unknown(Json)
}Install
Download zipGenerate ordinary mttp_core JSON codecs from annotated MoonBit types.