gpt4 book ai didi

elixir - 用毒药对字符串元组进行编码

转载 作者:行者123 更新时间:2023-12-03 09:09:20 25 4
gpt4 key购买 nike

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

{"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 的类型更改为其他类型?

最佳答案

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

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

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

关于elixir - 用毒药对字符串元组进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44196041/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com