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
Trending in Questions
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
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
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
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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 2 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
csisnett
Thank you @gcauchon when I pattern matched timestamp like:
defmodule TimestampWeb.DateView do
use TimestampWeb, :view
alias Timestamp.Stamp
end
it worked!
gcauchon
It looks like your code wants to serialize the whole
Plug.Connstruct, not the%Timestamp.Stamp{…}returned byTimestampWeb.DateView.render/2?!You might need to pattern match on
timestampon the second argument since the invocation inTimestamp.Implementation.show/2uses the keyword list shorthand.