Hi everyone. I am getting Jason.EncodeError
s when displaying an index of the following structures:
@type id :: <<_::128>>
@type t :: %__MODULE__{
id: id(),
t_ids: [id()],
name: String.t()
}
The error itself is:
[error] GenServer #PID<0.4086.0> terminating
** (Jason.EncodeError) invalid byte 0xD2 in <<115, 111, 117, 114, 99, 105, 110, 103, 115, 45, 210, 225, 137, 135, 162, 33, 1, 115, 153, 143, 194, 172, 41, 112, 234, 147>>
(jason 1.4.4) lib/jason.ex:213: Jason.encode_to_iodata!/2
(phoenix 1.7.14) lib/phoenix/socket/serializers/v2_json_serializer.ex:72: Phoenix.Socket.V2.JSONSerializer.encode!/1
(phoenix 1.7.14) lib/phoenix/socket.ex:791: Phoenix.Socket.encode_reply/2
(phoenix 1.7.14) lib/phoenix/socket.ex:676: Phoenix.Socket.handle_in/4
(phoenix 1.7.14) lib/phoenix/transports/long_poll_server.ex:51: Phoenix.Transports.LongPoll.Server.handle_info/2
(stdlib 6.1.2) gen_server.erl:2345: :gen_server.try_handle_info/3
(stdlib 6.1.2) gen_server.erl:2433: :gen_server.handle_msg/6
(stdlib 6.1.2) proc_lib.erl:329: :proc_lib.init_p_do_apply/3
I implemented the Jason.Encoder
for the structures, like:
defimpl Jason.Encoder do
def encode(s, opts) do
map = %{
id: Base.encode64(s.id),
t_ids: Enum.map(s.t_ids, &Base.encode64/1),
name: s.name
}
Jason.Encode.map(map, opts)
end
end
What is Phoenix trying to do here? Why the encoder does not help? Where is the root cause and how do I solve this? Ples, halp!