Skip to content

Std.Dynamic

enum Dynamic

A value in no particular format: what a format such as JSON parses into and writes from.

  • Null
  • Bool(Bool)
  • Number(Number)
  • Text(String)
  • Items(List[Dynamic])
  • Fields(Map[String, Dynamic])
  • Binary(Bytes)
enum Segment

A step of a path into a Dynamic: the key of Fields or the position of Items.

  • Key(String)
  • Index(Int)
error DecodeError(expected: String, actual: Dynamic, path: List[Segment])

A Dynamic that could not be read as expected. path leads from the value being decoded to actual. Show writes the kind of actual, not its contents.

  • expected: String
  • actual: Dynamic
  • path: List[Segment]
trait Encode

Turns a value into a Dynamic. Derivable ([trait.derive]).

  • fn to_dynamic(self: Self) -> Dynamic

    The value as a Dynamic.

  • fn to_field(self: Self) -> Option[Dynamic]

    The value written for a field of an object. None leaves the key out.

error DecodeErrors(errors: List[DecodeError])

Every failure of from_dynamic_all, in the order the parts were read. Never empty.

  • errors: List[DecodeError]
trait Decode

Builds a value from a Dynamic. Derivable, except for opaque types ([trait.derive]).

  • fn from_dynamic(d: Dynamic) -> Self fails DecodeError

    Reads d, stopping at the first failure.

  • fn from_dynamic_all(d: Dynamic) -> Self fails DecodeErrors

    Reads like from_dynamic, but reads every part and fails with all the failures. An impl that only writes from_dynamic stops at its first failure, which this wraps.

  • fn from_absent() -> Self fails DecodeError

    The value of a field whose key is missing. The default reads the key as null.

enum Attempt[A]

The result of reading one part while collecting failures.

  • Decoded(A)
  • Failed(List[DecodeError])
trait ObjectKey: Ord

The keys of a Map written as Fields.

  • fn to_key(self: Self) -> String

    The key as a String.

  • fn from_key(key: String) -> Self fails DecodeError

    Reads a key written by to_key.

fn kind(value: Dynamic) -> String

The kind of value: "null", "bool", "number", "text", "items", "fields" or "binary".

fn string(value: Dynamic) -> String fails DecodeError

Reads Text as a String.

fn bytes(value: Dynamic) -> Bytes fails DecodeError

Reads Binary as Bytes.

fn bool(value: Dynamic) -> Bool fails DecodeError

Reads a Bool.

fn int(value: Dynamic) -> Int fails DecodeError

Reads a number as an Int. Its value must be an integer in the range of Int; 1.0 and 1e3 are read.

fn int64(value: Dynamic) -> Int64 fails DecodeError

Reads a number as an Int64. Its value must be an integer in the range of Int64; 1.0 and 1e3 are read.

fn float(value: Dynamic) -> Float fails DecodeError

Reads a number as the nearest Float.

fn list[A](item: fn(Dynamic) -> A fails DecodeError uses { R(item) }) -> fn(Dynamic) -> List[A] fails DecodeError uses { R(item) }

A decoder that reads Items, each element by item.

fn optional[A](decoder: fn(Dynamic) -> A fails DecodeError uses { R(decoder) }) -> fn(Dynamic) -> Option[A] fails DecodeError uses { R(decoder) }

A decoder that reads Null as None and any other value as Some of decoder.

fn dict[V](value: fn(Dynamic) -> V fails DecodeError uses { R(value) }) -> fn(Dynamic) -> Map[String, V] fails DecodeError uses { R(value) }

A decoder that reads Fields, each value by value.

fn index[A](n: Int, decoder: fn(Dynamic) -> A fails DecodeError uses { R(decoder) }) -> fn(Dynamic) -> A fails DecodeError uses { R(decoder) }

A decoder that reads the element n of Items by decoder. Fewer elements is a DecodeError.

fn field[A](value: Dynamic, name: String, decoder: fn(Dynamic) -> A fails DecodeError uses { R(decoder) }) -> A fails DecodeError uses { R(decoder) }

Reads the key name of Fields by decoder. A missing key is read as Null.

fn lookup(value: Dynamic, name: String) -> Option[Dynamic] fails DecodeError

The value of the key name of Fields, or None if the key is missing.

fn items(value: Dynamic, n: Int) -> List[Dynamic] fails DecodeError

The elements of Items that has exactly n elements.

fn attempt[A](f: fn() -> A fails DecodeErrors uses { R(f) }) -> Attempt[A] uses { R(f) }

The value of f, or its failures instead of failing.

fn errors_of[A](a: Attempt[A]) -> List[DecodeError]

The failures of a: empty when it was decoded.

fn field_all[A](value: Dynamic, name: String, decoder: fn(Dynamic) -> A fails DecodeErrors uses { R(decoder) }) -> A fails DecodeErrors uses { R(decoder) }

field for a decoder that fails with every failure. name is added in front of the path of each.

fn index_all[A](n: Int, decoder: fn(Dynamic) -> A fails DecodeErrors uses { R(decoder) }) -> fn(Dynamic) -> A fails DecodeErrors uses { R(decoder) }

index for a decoder that fails with every failure.

fn tag(value: Dynamic) -> String fails DecodeError

The "tag" of an enum written by Encode.

fn object(fields: List[(String, Dynamic)]) -> Dynamic

Fields of fields. For a repeated key, the later value.

fn variant(tag: String, value: Option[Dynamic]) -> Dynamic

The form of an enum written by Encode: an object with the "tag", and the "value" if there is one.