# `Infusion.Runtime.Schema`
[🔗](https://github.com/mandar1045/infusion/blob/v0.1.0/runtime/lib/infusion/runtime/schema.ex#L1)

Decodes JSON into the structs of a generated SDK.

Every generated schema module defines `__schema__/1`:

  * `__schema__(:fields)`: `[{field_atom, "jsonName", type}]`, for objects
  * `__schema__(:type)`: the type, for schemas that aren't objects

Types:

    :string | :integer | :number | :boolean | :null | :any
    {:ref, module}                      # another schema
    {:array, type}
    {:map, type}                        # string keys to `type`
    {:enum, ["a", "b"]}                 # stays a string
    {:const, value}
    {:union, [type], discriminator}     # discriminator: nil | {"prop", %{"value" => module}}

Decoding never crashes on data the SDK wasn't generated for: unknown fields
are kept in `__extra__`, unknown enum values stay strings, and a union
variant that matches nothing is returned as the raw map.

# `decode`

```elixir
@spec decode(term(), term()) :: term()
```

Decodes `json` (already parsed) as `type`.

# `decode_module`

```elixir
@spec decode_module(term(), module()) :: term()
```

Decodes `json` as the schema `module`.

# `encode`

```elixir
@spec encode(term()) :: term()
```

Turns request data into JSON-ready terms: generated structs become maps
with their JSON names (fields left `nil` are dropped unless they were
present, and `__extra__` is kept), atom keys and atom values become
strings. Used for request bodies, so callers can pass structs, maps or
keyword lists.

---

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