csisnett

csisnett

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

Showing Posts 2 to 1

csisnett

csisnett OP

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!

gcauchon

gcauchon

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews