<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="245523" data-post-id="245523">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kartheek" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kartheek/120/19143_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kartheek
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="Fl4m3Ph03n1x" data-post="11" data-topic="46526">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/fl4m3ph03n1x/48/11709_2.png" class="avatar"> Fl4m3Ph03n1x:</div>
<blockquote>
<p>One last thing though, just to confirm, the <em>only</em> reason for not doing option 1, is because test performance can be affected, correct?</p>
</blockquote>
</aside>
<p>Docs state advantage of protocol consolidation as below:</p>
<blockquote>
<p>Consolidation directly links protocols to their implementations in a way that invoking a function from a consolidated protocol is equivalent to invoking two remote functions.</p>
</blockquote>
<p>For solving the problem in original post - you have to create the module and encoder in a file say <code>test.ex</code> in lib folder and it will work.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="245523" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-have-custom-enconding-for-struct-using-jason/46526/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-245523" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="245523"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-solved cat-solved" title="Marked as solution"></div>
  </section>
</div>
    <div class="postbit" id="245524" data-post-id="245524">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Fl4m3Ph03n1x" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Fl4m3Ph03n1x/120/11709_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Fl4m3Ph03n1x
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Thank you!</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="245524" data-batch-url="/posts/batch_likers">
                        2
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-have-custom-enconding-for-struct-using-jason/46526/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-245524" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="245524"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="334961" data-post-id="334961">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="belgoros" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/belgoros/120/39798_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  belgoros
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I have the same issue. If I get it right from the <a href="https://hexdocs.pm/elixir/Protocol.html#module-consolidation" rel="noopener nofollow ugc">Consolidation</a> docs, adding this to the <code>mix.exs</code> file would have fixed the warning:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">def project do
  ...
  elixirc_paths: elixirc_paths(Mix.env())
  ...
end

defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
</code></pre>
<p>But I still have it.<br>
Here is how I defined my module and marked the fields to encode:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Draft.Json.User do
	
	@derive {Jason.Encoder, only: [:name, :email, :age]}
	defstruct [:name, :email, :age]
end
</code></pre>
<p>And here is a custom encoder module:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Draft.Json.JsonReader do
	alias Draft.Json.User
	
	def to_json(%User{} = user) do
		Jason.encode!(user)
	end	
	
	def to_model(json) do
		Jason.decode!(json)
	end
end
</code></pre>
<p>When starting an IEx session, the warning pooped up as before:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

    warning: the Jason.Encoder protocol has already been consolidated, an implementation for Draft.Json.User has no effect. If you want to implement protocols after compilation or during tests, check the "Consolidation" section in the Protocol module documentation
    │
  4 │ 	defstruct [:name, :email, :age]
    │         ~~~~~~~~~~~~~~~~~~~~~~~~
    │
    └─ lib/json/user.ex:4: Draft.Json.User (module)
</code></pre>
<p>What am I missing?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="334961" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-have-custom-enconding-for-struct-using-jason/46526/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-334961" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="334961"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="334962" data-post-id="334962">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="belgoros" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/belgoros/120/39798_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  belgoros
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Pff, my bad, I named my module in a wrong way, sorry for confusing. It had to be:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule ElixirDraft.Json.JsonReader do
	alias ElixirDraft.Json.User
	
	def to_json(%User{} = user) do
		Jason.encode!(user)
	end	
	
	def to_model(json) do
		Jason.decode!(json)
	end
end
</code></pre>
<p>No warnings, it worked as expected :).</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="334962" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/how-to-have-custom-enconding-for-struct-using-jason/46526/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-334962" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="334962"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>