# `LiveSvelte.Encoder`
[🔗](https://github.com/woutdp/live_svelte/blob/v0.18.0/lib/live_svelte/encoder.ex#L1)

Protocol for encoding values for LiveSvelte JSON serialization.

Transforms structs and other terms into JSON-compatible data (maps, lists,
primitives) before the configured JSON library encodes to a string. Supports
`@derive` with `:only` and `:except` to control which struct fields are
encoded. By default all keys except `:__struct__` are encoded.

## Deriving

* `@derive LiveSvelte.Encoder` — encode all struct fields except `:__struct__`
* `@derive {LiveSvelte.Encoder, only: [:a, :b]}` — encode only listed keys
* `@derive {LiveSvelte.Encoder, except: [:secret]}` — encode all except listed keys

## Example

    defmodule User do
      @derive {LiveSvelte.Encoder, except: [:password]}
      defstruct [:name, :email, :password]
    end

For structs you don't own, use `Protocol.derive/3` outside the module.

# `opts`

```elixir
@type opts() :: Keyword.t()
```

# `t`

```elixir
@type t() :: term()
```

# `encode`

```elixir
@spec encode(t(), opts()) :: any()
```

Encodes a value to a JSON-compatible term (map, list, or primitive).

---

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