dersar00
Problem with encoding Geo.Point
Hello, I have a record structure that contains Geo.Point coordinates field(geo_postgis).
Thats I receive from db when I’m using serializer.
%{
coordinates: %Geo.Point{
coordinates: {48.51572187715065, 48.51572187715065},
properties: %{},
srid: 4326
},
id: 10,
name: "default name"
}
When I’m try to send them to my client through websocket, I got error that says
** (Poison.EncodeError) unable to encode value: {48.442894571035936, 48.442894571035936}
My serializer
defmodule ScenesSerializer do
use Remodel
attributes [:name, :id, :coordinates]
end
Most Liked
massimo
Seems quite right.
Poison (or any other Json encoder for that matter) doesn’t support tuples, because they don’t exists in Json.
One way to solve the problem is to convert the tuple to a list
{48.51572187715065, 48.51572187715065} |> Tuple.to_list() |> Poison.encode
{:ok, "[48.51572187715065,48.51572187715065]"}
In your specific case, Geo supports encoding Points to a Json compatible format.
You have to use Geo.JSON.encode(point)
From their Github page
#Examples using Poison as the JSON parser
iex(1)> Geo.JSON.encode(point)
{:ok, %{ "type" => "Point", "coordinates" => [100.0, 0.0] }}
iex(2)> point = Poison.decode!("{ \"type\": \"Point\", \"coordinates\": [100.0, 0.0] }") |> Geo.JSON.decode
%Geo.Point{ coordinates: {100.0, 0.0}, srid: nil }
iex(3)> Geo.JSON.encode!(point) |> Poison.encode!
"{\"type\":\"Point\",\"coordinates\":[100.0,0.0]}"
1
Popular in Questions
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lets say i have a sample like
a = 20; b = 10;
if (a > b) do
{:ok, "a"}
end
if (a < b) do
{:ok, b}
end
if (a == b) do
{:ok, "equa...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Other popular topics
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









