# `Skir`
[🔗](https://github.com/gepheum/skir/blob/main/lib/skir.ex#L1)

Official Elixir runtime for generated Skir schemas.

Non-bang functions return `{:ok, value}` or `{:error, %Skir.Error{}}`.
Bang functions raise `Skir.Error`. `encode/3` produces framed Skir binary;
`encode_json/3` produces a UTF-8 JSON string (`format: :dense` by default, or
2-space indented `format: :readable` for debugging). `to_json/3` returns an
Elixir JSON term.

Unknown fields are discarded by default on decode. With `unknown_fields: :preserve`,
unrecognized dense-JSON and binary values are preserved across round-trips in the
same wire format and silently dropped when serializing to another format or to
readable JSON. No atom is created from untrusted input. SkirRPC lives under `Skir.RPC`.

# `float_value`

```elixir
@type float_value() :: float() | integer() | :nan | :infinity | :neg_infinity
```

# `primitive`

```elixir
@type primitive() ::
  :bool
  | :int32
  | :int64
  | :hash64
  | :float32
  | :float64
  | :timestamp
  | :string
  | :bytes
```

# `result`

```elixir
@type result(value) :: {:ok, value} | {:error, Skir.Error.t()}
```

# `type`

```elixir
@type type() ::
  primitive()
  | {:optional, type()}
  | {:array, type()}
  | {:array, type(), String.t()}
  | {:record, module()}
```

# `decode`

```elixir
@spec decode(type(), binary(), keyword()) :: result(term())
```

Decodes framed Skir binary or JSON bytes, validating the input and enforcing resource limits.

# `decode!`

```elixir
@spec decode!(type(), term(), keyword()) :: term()
```

Like `decode/3`, but returns the value directly and raises `Skir.Error` on failure.

# `decode_json`

```elixir
@spec decode_json(type(), binary(), keyword()) :: result(term())
```

Decodes a UTF-8 JSON string, accepting dense and readable JSON and enforcing resource limits.

# `decode_json!`

```elixir
@spec decode_json!(type(), term(), keyword()) :: term()
```

Like `decode_json/3`, but returns the value directly and raises `Skir.Error` on failure.

# `default`

```elixir
@spec default(type()) :: term()
```

Returns the zero/default value for a primitive, composite, or generated record type.

# `encode`

```elixir
@spec encode(type(), term(), keyword()) :: result(binary())
```

Encodes a value as Skir binary with the `skir` prefix. This is not Erlang external-term format.

# `encode!`

```elixir
@spec encode!(type(), term(), keyword()) :: term()
```

Like `encode/3`, but returns the value directly and raises `Skir.Error` on failure.

# `encode_json`

```elixir
@spec encode_json(type(), term(), keyword()) :: result(binary())
```

Encodes a value as a UTF-8 JSON string. Dense JSON is the default; use `format: :readable` for 2-space indented JSON.

# `encode_json!`

```elixir
@spec encode_json!(type(), term(), keyword()) :: term()
```

Like `encode_json/3`, but returns the value directly and raises `Skir.Error` on failure.

# `from_json`

```elixir
@spec from_json(type(), term(), keyword()) :: result(term())
```

Decodes a dense or readable JSON term. Unknown fields are discarded by default.

# `from_json!`

```elixir
@spec from_json!(type(), term(), keyword()) :: term()
```

Like `from_json/3`, but returns the value directly and raises `Skir.Error` on failure.

# `index_by`

```elixir
@spec index_by(list(), [atom() | {atom(), module()}]) :: map()
```

Builds a map index for a keyed array. When duplicate keys exist, the last item wins.

# `to_json`

```elixir
@spec to_json(type(), term(), keyword()) :: result(term())
```

Converts a native value to a dense JSON term, or readable JSON term with `format: :readable`.

# `to_json!`

```elixir
@spec to_json!(type(), term(), keyword()) :: term()
```

Like `to_json/3`, but returns the value directly and raises `Skir.Error` on failure.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
