Add overloading examples to README

This commit is contained in:
Eric 2024-02-08 12:11:19 +11:00
parent 377c48f3ca
commit bcabc173d4
No known key found for this signature in database

View File

@ -243,6 +243,27 @@ assert res.error of SerdeError
assert res.error.msg == "json field(s) missing in object: {\"extra\"}"
```
## Custom types
If `serde` can't de/serialize a custom type, de/serialization can be supported by
overloading `%` and `fromJson`. For example:
```nim
type
Address* = distinct array[20, byte]
SerializationError* = object of CatchableError
func `%`*(address: Address): JsonNode =
%($address)
func fromJson(_: type Address, json: JsonNode): ?!Address =
expectJsonKind(Address, JString, json)
without address =? Address.init(json.getStr), error:
return failure newException(SerializationError,
"Failed to convert '" & $json & "' to Address: " & error.msg)
success address
```
## Serializing to string (`toJson`)
`toJson` is a shortcut for serializing an object into its serialized string representation: