///|
#custom.serde
struct Id(Int32)
///|
#custom.serde
struct Point(Int32, Int32)///|
pub impl ToJson for Id with fn to_json(self) {
match self {
Id(inner) => ToJson::to_json(inner)
}
}
///|
pub impl @json.FromJson for Id with fn from_json(value, path) {
let inner : Int32 = @json.from_json(value, path~)
Id(inner)
}
///|
pub impl ToJson for Point with fn to_json(self) {
match self {
Point(item_0, item_1) =>
Json::array([ToJson::to_json(item_0), ToJson::to_json(item_1)])
}
}
///|
pub impl @json.FromJson for Point with fn from_json(value, path) {
let values = @serde.tuple(value, 2, path)
let item_0 : Int32 = @json.from_json(values[0], path=path.add_index(0))
let item_1 : Int32 = @json.from_json(values[1], path=path.add_index(1))
Point(item_0, item_1)
}///|
#custom.serde(decode_only)
struct InputId(Int32)///|
pub impl @json.FromJson for InputId with fn from_json(value, path) {
let inner : Int32 = @json.from_json(value, path~)
InputId(inner)
}///|
#custom.serde(encode_only)
struct OutputId(Int32)///|
pub impl ToJson for OutputId with fn to_json(self) {
match self {
OutputId(inner) => ToJson::to_json(inner)
}
}///|
#custom.serde(rename_all="camelCase", deny_unknown_fields)
struct Profile {
display_name : String
#custom.serde(rename="email", aliases=["mail"])
address : String
#custom.serde(path=["meta", "status"])
status : String
#custom.serde(default)
count : Int32
#custom.serde(skip)
cache : Int32
#custom.serde(skip_serializing_if="Option::is_none")
locale : String?
}{
"displayName": "Aki",
"mail": "a@example.net",
"meta": { "status": "active" }
}{
"displayName": "Aki",
"email": "a@example.net",
"meta": { "status": "active" },
"count": 0
}///|
pub impl ToJson for Profile with fn to_json(self) {
let obj : Map[String, Json] = Map([])
obj.set("displayName", ToJson::to_json(self.display_name))
obj.set("email", ToJson::to_json(self.address))
@serde.write_path(obj, ["meta", "status"], ToJson::to_json(self.status))
obj.set("count", ToJson::to_json(self.count))
if self.locale is Some(_) {
obj.set(
"locale",
match self.locale {
None => Json::null()
Some(option_1) => ToJson::to_json(option_1)
},
)
}
Json::object(obj)
}
///|
pub impl @json.FromJson for Profile with fn from_json(value, path) {
let obj = @serde.object(value, path)
@serde.deny_unknown_paths(
obj,
[
["displayName"],
["email"],
["mail"],
["meta", "status"],
["count"],
["cache"],
["locale"],
],
path,
)
let field_display_name : String = match obj.get("displayName") {
Some(String(value)) => value
Some(value) => @json.from_json(value, path=path.add_key("displayName"))
None => @serde.missing_field(path.add_key("displayName"), "displayName")
}
let field_address : String = match
@serde.field(obj, "email", ["mail"], path) {
Some(String(value)) => value
Some(value) => @json.from_json(value, path=path.add_key("email"))
None => @serde.missing_field(path.add_key("email"), "email")
}
let field_status : String = match
@serde.read_path(obj, ["meta", "status"], path) {
(Some(value), at, _) => @json.from_json(value, path=at)
(None, at, key) => @serde.missing_field(at, key)
}
let field_count : Int32 = match obj.get("count") {
Some(value) => @json.from_json(value, path=path.add_key("count"))
None => Default::default()
}
let field_cache : Int32 = Default::default()
let field_locale : String? = match obj.get("locale") {
Some(value) =>
match value {
Null => None
option_1 => Some(@json.from_json(option_1, path=path.add_key("locale")))
}
None => None
}
{
display_name: field_display_name,
address: field_address,
status: field_status,
count: field_count,
cache: field_cache,
locale: field_locale,
}
} obj.set("displayName", ToJson::to_json(self.display_name))///|
let field_display_name : String = match obj.get("displayName") {
Some(String(value)) => value
Some(value) => @json.from_json(value, path=path.add_key("displayName"))
None => @serde.missing_field(path.add_key("displayName"), "displayName")
} obj.set("email", ToJson::to_json(self.address))///|
let field_address : String = match @serde.field(obj, "email", ["mail"], path) {
Some(String(value)) => value
Some(value) => @json.from_json(value, path=path.add_key("email"))
None => @serde.missing_field(path.add_key("email"), "email")
} @serde.write_path(obj, ["meta", "status"], ToJson::to_json(self.status))///|
let field_status : String = match
@serde.read_path(obj, ["meta", "status"], path) {
(Some(value), at, _) => @json.from_json(value, path=at)
(None, at, key) => @serde.missing_field(at, key)
}///|
let field_count : Int32 = match obj.get("count") {
Some(value) => @json.from_json(value, path=path.add_key("count"))
None => Default::default()
}///|
let field_cache : Int32 = Default::default() if self.locale is Some(_) {
obj.set(
"locale",
match self.locale {
None => Json::null()
Some(option_1) => ToJson::to_json(option_1)
},
)
} @serde.deny_unknown_paths(
obj,
[
["displayName"],
["email"],
["mail"],
["meta", "status"],
["count"],
["cache"],
["locale"],
],
path,
)///|
#custom.serde
pub struct WithExtras {
label : String
#custom.serde(flatten)
extra : Map[String, Json]
} @serde.write_flattened(obj, ToJson::to_json(self.extra), ["label"])///|
let remaining = @serde.flattened_remainder(obj, ["label"])///|
let field_extra : Map[String, Json] = @json.from_json(remaining, path~)///|
fn encode_count(value : Int32) -> Json {
ToJson::to_json(value)
}
///|
fn decode_count(
value : Json,
path : @json.JsonPath,
) -> Int32 raise @json.JsonDecodeError {
@json.from_json(value, path~)
}
///|
#custom.serde
pub struct Count {
#custom.serde(serialize_with="encode_count", deserialize_with="decode_count")
count : Int32
} obj.set("count", encode_count(self.count)) Some(value) => decode_count(value, path.add_key("count"))///|
priv struct Wire(Int32)
///|
fn Wire::serialize(value : Wire) -> Json {
let Wire(number) = value
ToJson::to_json(number)
}
///|
fn Wire::deserialize(
value : Json,
path : @json.JsonPath,
) -> Wire raise @json.JsonDecodeError {
Wire(@json.from_json(value, path~))
}
///|
#custom.serde
priv struct Packet {
#custom.serde(codec="Wire")
id : Wire
} obj.set("id", Wire::serialize(self.id)) Some(value) => Wire::deserialize(value, path.add_key("id"))///|
#custom.serde
struct JobSettings {
#custom.serde(skip_serializing)
token : String
#custom.serde(skip_deserializing)
retries : Int32
#custom.serde(default="normal_priority", skip_serializing_if="is_normal_priority")
priority : Int32
}
///|
fn normal_priority() -> Int32 {
10
}
///|
fn is_normal_priority(value : Int32) -> Bool {
value == normal_priority()
} obj.set("retries", ToJson::to_json(self.retries))
if is_normal_priority(self.priority) == false {
obj.set("priority", ToJson::to_json(self.priority))
}///|
let field_token : String = match obj.get("token") {
Some(String(value)) => value
Some(value) => @json.from_json(value, path=path.add_key("token"))
None => @serde.missing_field(path.add_key("token"), "token")
}
///|
let field_retries : Int32 = Default::default()
///|
let field_priority : Int32 = match obj.get("priority") {
Some(value) => @json.from_json(value, path=path.add_key("priority"))
None => normal_priority()
}///|
#custom.serde
pub enum External {
Idle
#custom.serde(rename="message")
Text(String)
}///|
pub impl ToJson for External with fn to_json(self) {
match self {
Idle => Json::string("Idle")
Text(inner) => {
let payload = ToJson::to_json(inner)
Json::object({ "message": payload })
}
}
}///|
#custom.serde(tag="type", rename_all="snake_case")
pub enum Internal {
Idle
NewMessage(text~ : String)
} NewMessage(text=field_text) => {
let obj :Map[String, Json] = Map([])
obj.set("text", ToJson::to_json(field_text))
obj.set("type", Json::string("new_message"))
Json::object(obj)
}///|
#custom.serde(decode_only, tag="type")
pub enum InternalInput {
Read(InternalData)
}
///|
#custom.serde(decode_only, deny_unknown_fields)
pub struct InternalData {
#custom.serde(rename="type")
kind : String
text : String
} "Read" => Read(@json.from_json(value, path~))///|
#custom.serde(tag="type", content="data")
pub enum Adjacent {
Idle
Text(String)
} Text(inner) => {
let tagged :Map[String, Json] = { "type": Json::string("Text") }
let payload = ToJson::to_json(inner)
tagged.set("data", payload)
Json::object(tagged)
}///|
#custom.serde(untagged)
pub enum Untagged {
Text(String)
Count(Int32)
}///|
pub impl @json.FromJson for Untagged with fn from_json(value, path) {
@serde.untagged(value, path, [
("Text", serde_decode_Untagged_0),
("Count", serde_decode_Untagged_1),
])
}///|
#custom.serde
pub struct Box[T](T)///|
pub impl[T : ToJson] ToJson for Box[T] with fn to_json(self) {
match self {
Box(inner) => ToJson::to_json(inner)
}
}
///|
pub impl[T : @json.FromJson] @json.FromJson for Box[T] with fn from_json(
value,
path,
) {
let inner : T = @json.from_json(value, path~)
Box(inner)
}#deprecated("`GenerateError::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn GenerateError::to_repr(GenerateError) -> Reprpub(all) struct EnumDef {
name : String
params : Array[String]
variants : Array[Variant]
repr : Representation
decode_only : Bool
encode_only : Bool
is_private : Bool
}pub(all) struct Field {
name : String
label : String?
wire_name : String
aliases : Array[String]
path : Array[String]
ty : Type
optional : Bool
option_depth : Int
default : Bool
default_with : Hook?
skip : Bool
skip_serializing : Bool
skip_deserializing : Bool
omit_none : Bool
skip_serializing_if : Hook?
flatten : Bool
serialize_with : Hook?
deserialize_with : Hook?
}pub(all) enum Representation {
External
Internal(String)
Adjacent(String, String)
Untagged
}