Skip to content

Commit

Permalink
feat: add from/to json for uint/uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-jerry-ye committed Jan 3, 2025
1 parent dd6cdac commit 4e8f0ae
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions json/from_json.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,42 @@ pub impl FromJson for Int with from_json(json, path) {
///|
pub impl FromJson for Int64 with from_json(json, path) {
match json {
Number(n) => n.to_int64()
_ => decode_error!(path, "Int64::from_json: expected number")
String(str) =>
try {
@strconv.parse_int64!(str)
} catch {
error =>
decode_error!(path, "Int64::from_json: parsing failure \{error}")
}
_ =>
decode_error!(
path, "Int64::from_json: expected number in string representation",
)
}
}

///|
pub impl FromJson for UInt with from_json(json, path) {
match json {
Number(n) => n.to_int64().reinterpret_as_uint64().to_uint()
_ => decode_error!(path, "UInt::from_json: expected number")
}
}

///|
pub impl FromJson for UInt64 with from_json(json, path) {
match json {
String(str) =>
try {
@strconv.parse_uint64!(str)
} catch {
error =>
decode_error!(path, "UInt64::from_json: parsing failure \{error}")
}
_ =>
decode_error!(
path, "UInt64::from_json: expected number in string representation",
)
}
}

Expand Down

0 comments on commit 4e8f0ae

Please sign in to comment.