# How to have custom enconding for struct using Jason?

**URL:** <https://forum.devtalk.com/t/how-to-have-custom-enconding-for-struct-using-jason/44467>\
**Category:** Backend Questions/Help\
**Tags:** elixir, json\
**Created:** [11 March 2022 09:26 UTC](https://forum.devtalk.com/t/how-to-have-custom-enconding-for-struct-using-jason/44467 "2022-03-11T09:26:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.devtalk.com/user_avatar/forum.devtalk.com/fl4m3ph03n1x/32/3808_2.png) [@Fl4m3Ph03n1x](https://forum.devtalk.com/u/Fl4m3Ph03n1x)\
**Post date:** [11 March 2022 09:26 UTC](https://forum.devtalk.com/t/how-to-have-custom-enconding-for-struct-using-jason/44467/1 "2022-03-11T09:26:50Z")

</div>

## Background

I am trying to encode a structure into json format using the [Jason](https://github.com/michalmuskala/jason) library. However, this is not working as expected.

## Code

Let’s assume I have this struct:

```auto
defmodule Test do
   defstruct [:foo, :bar, :baz]
end

```

And that when using `Jason.enconde(%Test{foo: 1, bar: 2, baz:3 })` I want this json to be created:

```auto
%{"foo" => 1, "banana" => 5}

```

## Error

It is my understanding that to achieve this I need to implement the `Jason.Enconder` protocol in my struct:

> **[Jason.Encoder — jason v1.4.4](https://hexdocs.pm/jason/Jason.Encoder.html)**

```auto
defmodule Test do
   defstruct [:foo, :bar, :baz]
   
   defimpl Jason.Encoder do
      @impl Jason.Encoder 
      def encode(value, opts) do
         Jason.Encode.map(%{foo: Map.get(value, :foo), banana: Map.get(value, :bar, 0) + Map.get(value, :baz, 0)}, opts)
      end
   end
end

```

However, this will not work:

```auto
Jason.encode(%Test{foo: 1, bar: 2, baz: 3})
{:error,
 %Protocol.UndefinedError{
   description: "Jason.Encoder protocol must always be explicitly implemented.\n\nIf you own the struct, you can derive the implementation specifying which fields should be encoded to JSON:\n\n @derive {Jason.Encoder, only: [....]}\n defstruct ...\n\nIt is also possible to encode all fields, although this should be used carefully to avoid accidentally leaking private information when new fields are added:\n\n @derive Jason.Encoder\n defstruct ...\n\nFinally, if you don't own the struct you want to encode to JSON, you may use Protocol.derive/3 placed outside of any module:\n\n Protocol.derive(Jason.Encoder, NameOfTheStruct, only: [...])\n Protocol.derive(Jason.Encoder, NameOfTheStruct)\n",
   protocol: Jason.Encoder,
   value: %Test{bar: 2, baz: 3, foo: 1}
 }}

```

From what I understand, it looks like I can only select/exclude keys to serialize, I cannot transform/add new keys.  
Since I own the structure in question, using `Protocol.derive` is not necessary.

However I fail to understand how I can leverage the `Jason.Encoder` protocol to achieve what I want.

## Questions

1. Is my objective possible using the Jason library, or is this a limitation?
2. Am I miss understanding the documentation and doing something incorrect?

---

<div class="post-metadata">

**Author:** ![Fl4m3Ph03n1x](https://forum.devtalk.com/user_avatar/forum.devtalk.com/fl4m3ph03n1x/32/3808_2.png) [@Fl4m3Ph03n1x](https://forum.devtalk.com/u/Fl4m3Ph03n1x)\
**Post date:** [15 March 2022 08:19 UTC](https://forum.devtalk.com/t/how-to-have-custom-enconding-for-struct-using-jason/44467/3 "2022-03-15T08:19:56Z")

</div>

## Answer

Turns out there is nothing wrong with the code _itself_.

The issue here is that I was trying these examples in the `iex` shell. Due to [Protocol Consolidation](https://hexdocs.pm/elixir/Protocol.html#module-consolidation) this means that the protocol will be _consolidated_ at compile time. Therefore, because when I launch `iex` there is nothing regarding the `Jason.Encoder` protocol compiled, I loose the chance to do it runtime (and `iex` is runtime).

This also affects tests in an interesting way, to fix them you can either disable Protocol Consolidation wen running under `Mix.env() == :test` (but it will absolutely hammer your test’s performance) or you can do it at compile time with some extra options.

A very good answer was also given by the community:

- [How to have custom enconding for struct using Jason? - #10 by kartheek - Questions / Help - Elixir Programming Language Forum](https://elixirforum.com/t/how-to-have-custom-enconding-for-struct-using-jason/46526/10?u=fl4m3ph03n1x)

And I recommend you read the full thread if you want all the juicy details that I have quickly summarized here.
