2

我正在尝试用毒药对这个字符串元组进行编码:

{"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其他类型?

4

1 回答 1

5

如果您确实需要将元组编码为列表类型,则可以使用:

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

您应该能够根据需要使用类似的模式将其转换为另一个原始结构。

于 2018-06-29T05:23:34.703 回答