我试图用毒药编码这个字符串元组:
{"product existed but could not add categories to product",
"Shop existed but could not add product to shop"}
出现此错误:
19:10:21.593 [error] #PID<0.339.0> running Api.Router terminated
Server: 192.168.20.3:4000 (http)
Request: POST /products
** (exit) an exception was raised:
** (Poison.EncodeError) unable to encode value: {"product existed but could not add categories to product", "Shop ex
isted but could not add product to shop"}
(poison) lib/poison/encoder.ex:383: Poison.Encoder.Any.encode/2
(poison) lib/poison/encoder.ex:227: anonymous fn/4 in Poison.Encoder.Map.encode/3
(poison) lib/poison/encoder.ex:228: Poison.Encoder.Map."-encode/3-lists^foldl/2-0-"/3
(poison) lib/poison/encoder.ex:228: Poison.Encoder.Map.encode/3
(poison) lib/poison.ex:41: Poison.encode!/2
(api) lib/api/router.ex:90: anonymous fn/1 in Api.Router.do_match/4
(api) lib/api/router.ex:1: Api.Router.plug_builder_call/2
(api) lib/plug/debugger.ex:123: Api.Router.call/2
Poison可以对字符串元组进行编码,还是应该将errors
的类型更改为其他内容?
答案 0 :(得分:2)
如果您确实需要将元组编码为列表类型,则可以这样做:
defmodule TupleEncoder do
alias Poison.Encoder
defimpl Encoder, for: Tuple do
def encode(data, options) when is_tuple(data) do
data
|> Tuple.to_list()
|> Encoder.List.encode(options)
end
end
end
您应该能够使用类似的模式,根据需要将其转换为另一个原始结构。