Coding
Module coding
import coding
| Module | coding |
| Source | coding/attributes.zirr, coding/coding.zirr, coding/decode.zirr, coding/encode.zirr, coding/helpers.zirr, coding/module-docs.zirr |
Between your
datatypes and a tree any format can carry.
coding converts between your own declarations and a native tree of prelude.Dict, prelude.Array, String, Int, Float, Bool and void. It never touches text: json and yaml take that tree the rest of the way.
It works through reflect. Fields are read off the type, and the attributes written on them decide the details — the key a field travels under, whether it travels at all, what it becomes when absent, and how it is converted when the general rules are not what you want. A type carrying none of them encodes under the names it declares.
A format can override any of these for itself. encodeWith and decodeWith take a format module and consult its own attributes first, which is why @json.Name beats @coding.Name when encoding as JSON.
Dependencies
Contents
- Attributes —
Decode,Default,Encode,Ignore,Name - Functions —
decode,decodeWith,encode,encodeWith
Attributes
Decode
`coding/coding.zirr:21`
attr Decode {
parse
}
Decodes a field from the raw tree found under its key, instead of by the general rules.
Fields
| Field | Description |
|---|---|
parse |
fn(raw: Any) -> Result. |
Default
`coding/coding.zirr:15`
attr Default {
value
}
The value a field takes when its key is absent.
Fields
| Field | Description |
|---|---|
value |
The value to use. |
Encode
`coding/coding.zirr:27`
attr Encode {
format
}
Encodes a field to a tree, instead of by the general rules.
Fields
| Field | Description |
|---|---|
format |
fn(value: Any) -> Result. |
Ignore
`coding/coding.zirr:12`
attr Ignore
Marks a field as one that does not travel.
An ignored field is skipped when encoding, and takes its @Default — or void — when decoding.
Name
`coding/coding.zirr:5`
attr Name {
text
}
The key a field is read and written under, instead of its declared name.
Every attribute here is an override, so a type carrying none encodes under the names it declares.
Fields
| Field | Description |
|---|---|
text |
The key to use. |
Functions
decode
`coding/coding.zirr:38`
fn decode(t: Any, tree: Any) -> Result
A native tree as a value of type t.
decodeWith
`coding/coding.zirr:48`
fn decodeWith(format: AnyModule, t: Any, tree: Any) -> Result
As decode, but consulting the format module’s own attributes first.
encode
`coding/coding.zirr:33`
fn encode(value: Any) -> Result
A value as a native tree of Dict, Array, String, Int, Float, Bool and void.
encodeWith
`coding/coding.zirr:43`
fn encodeWith(format: AnyModule, value: Any) -> Result
As encode, but consulting the format module’s own attributes first, so @json.Name overrides @coding.Name.