(Protocol.UndefinedError) protocol Jason.Encoder not implemented for %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}

Hi everyone I’m making an API with phoenix and trying to output json from a struct called Stamp:

defmodule Timestamp.Stamp do
   @derive Jason.Encoder
    defstruct [:unix, :utc]
end

When people access “/api/timestamp/:date” they should get a json like {“unix”: 1921922121, “utc”: “Fri, 25 Dec 2015 00:00:00 GMT”} depending on the date. But the error I receive is the following:

# protocol Jason.Encoder not implemented for %Plug.Conn{adapter: {Plug.Cowboy.Conn, :...}, assigns: %{layout: false, timestmap: %{error: "Invalid Date"}}, before_send: [#Function<1.112466771/1 in Plug.Logger.call/2>, #Function<0.66982185/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "GET", owner: #PID<0.529.0>, params: %{"date" => "2"}, path_info: ["api", "timestamp", "2"], path_params: %{"date" => "2"}, port: 4000, private: %{TimestampWeb.Router => {[], %{}}, :phoenix_action => :show, :phoenix_controller => TimestampWeb.DateController, :phoenix_endpoint => TimestampWeb.Endpoint, :phoenix_format => "json", :phoenix_layout => {TimestampWeb.LayoutView, :app}, :phoenix_pipelines => [:api], :phoenix_router => TimestampWeb.Router, :phoenix_template => "show.json", :phoenix_view => TimestampWeb.DateView, :plug_session_fetch => #Function<1.58261320/1 in Plug.Session.fetch_session/1>}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"}, {"accept-encoding", "gzip, deflate"}, {"accept-language", "en-US,en;q=0.5"}, {"cache-control", "max-age=0"}, {"connection", "keep-alive"}, {"host", "localhost:4000"}, {"upgrade-insecure-requests", "1"}, {"user-agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:64.0) Gecko/20100101 Firefox/64.0"}], request_path: "/api/timestamp/2", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "2lv5co8o7mv2bndehc0000b4"}], scheme: :http, script_name: [], secret_key_base: :..., state: :unset, status: nil}, Jason.Encoder protocol must always be explicitly implemented.

And the suggestion to add @derive Jason.encoder to the struct module but I already did that.

It also says the protocol is implemented for Timestamp.Stamp, Ecto.Schema.Metadata… etc, so I’m wondering if I’m missing something

Here is my view and controller:

defmodule TimestampWeb.DateView do
    use TimestampWeb, :view

    def render("show.json", timestamp) do
        timestamp
    end
end

defmodule TimestampWeb.DateController do
    use TimestampWeb, :controller

    alias Timestamp.Implementation
    def show(conn, %{"date" => date}) do
        timestamp = Implementation.get_timestamp(date)
        render(conn, "show.json", timestmap: timestamp)
    end
end

And my router:

defmodule TimestampWeb.Router do
....

  scope "/api/timestamp", TimestampWeb do
    pipe_through :api

    get "/:date", DateController, :show
    get "/", CurrentTimestampController, :show
  end

end

It looks like your code wants to serialize the whole Plug.Conn struct, not the %Timestamp.Stamp{…} returned by TimestampWeb.DateView.render/2?!

You might need to pattern match on timestamp on the second argument since the invocation in Timestamp.Implementation.show/2 uses the keyword list shorthand.

2 Likes

Thank you @gcauchon when I pattern matched timestamp like:

defmodule TimestampWeb.DateView do
use TimestampWeb, :view
alias Timestamp.Stamp

def render("show.json", %{timestamp: timestamp}) do
    timestamp
end

end

it worked!

2 Likes