如何键入 cast decode JSON 就好像它来自数据库一样
How to type cast decode JSON as if it came from the database
从数据库加载 date/time 类型时,Ecto 将转换为 Ecto.DateTime 类型。从 JSON 字符串
加载模型时如何应用相同的类型转换
defmodule Rocket.User do
use Rocket.Model
schema "users" do
field :created_at, :datetime
field :name, :string
field :email, :string
field :password, :string
field :timezone, :string
end
end
iex(40)> Poison.decode!(~s({"created_at":"2015-01-21T06:05:10.891Z"}), as: Rocket.User)
%Rocket.User{created_at: "2015-01-21T06:05:10.891Z", email: nil, id: nil,
name: nil, password: nil, timezone: nil}
如果您使用的是 Ecto 0.6.0,最好的方法是使用变更集:
Ecto.Changeset.cast Poison.decode!(data), %Rocket.User{},
~w(required_fields), ~w(optional_fields)
如果您将其作为外部数据接收,实际上建议使用变更集,因为您需要在将其添加到模型之前转换、过滤和验证该数据。您可以在 in the Ecto introduction 以及 Ecto.Changeset
模块文档中找到有关它们的更多信息。
不过还有一个问题:Ecto 不知道如何将字符串转换为日期时间。但是,您可以通过使用自定义类型来教它如何操作。我在下面创建了一个模板,你只需要实现转换功能:
https://gist.github.com/josevalim/1ed574b388c32f056da1
然后在您的架构中:
timestamps type: Rocket.DateTime
您可以在 Ecto.Type
文档中找到更多信息。我知道我们需要在 Ecto 中改进这一点,我认为我们至少应该能够以 JSON.
中指定的格式解析日期时间
从数据库加载 date/time 类型时,Ecto 将转换为 Ecto.DateTime 类型。从 JSON 字符串
加载模型时如何应用相同的类型转换defmodule Rocket.User do
use Rocket.Model
schema "users" do
field :created_at, :datetime
field :name, :string
field :email, :string
field :password, :string
field :timezone, :string
end
end
iex(40)> Poison.decode!(~s({"created_at":"2015-01-21T06:05:10.891Z"}), as: Rocket.User)
%Rocket.User{created_at: "2015-01-21T06:05:10.891Z", email: nil, id: nil,
name: nil, password: nil, timezone: nil}
如果您使用的是 Ecto 0.6.0,最好的方法是使用变更集:
Ecto.Changeset.cast Poison.decode!(data), %Rocket.User{},
~w(required_fields), ~w(optional_fields)
如果您将其作为外部数据接收,实际上建议使用变更集,因为您需要在将其添加到模型之前转换、过滤和验证该数据。您可以在 in the Ecto introduction 以及 Ecto.Changeset
模块文档中找到有关它们的更多信息。
不过还有一个问题:Ecto 不知道如何将字符串转换为日期时间。但是,您可以通过使用自定义类型来教它如何操作。我在下面创建了一个模板,你只需要实现转换功能:
https://gist.github.com/josevalim/1ed574b388c32f056da1
然后在您的架构中:
timestamps type: Rocket.DateTime
您可以在 Ecto.Type
文档中找到更多信息。我知道我们需要在 Ecto 中改进这一点,我认为我们至少应该能够以 JSON.