Implement JSON.Encoder for Calendar types (#14061)

This commit is contained in:
José Valim
2024-12-12 00:37:04 +01:00
committed by GitHub
parent fb288a4a80
commit 8a0cd664cb
2 changed files with 13 additions and 0 deletions
+6
View File
@@ -150,6 +150,12 @@ defimpl JSON.Encoder, for: Map do
end
end
defimpl JSON.Encoder, for: [Date, Time, NaiveDateTime, DateTime] do
def encode(value, _encoder) do
[?", @for.to_iso8601(value), ?"]
end
end
defmodule JSON.DecodeError do
@moduledoc """
The exception raised by `JSON.decode!/1`.
+7
View File
@@ -46,6 +46,13 @@ defmodule JSONTest do
assert JSON.encode!(%Token{value: :example}) == "[\"example\"]"
assert JSON.encode!(%Token{value: "hello\0world"}) == "[\"hello\\u0000world\"]"
end
test "calendar" do
assert JSON.encode!(~D[2010-04-17]) == "\"2010-04-17\""
assert JSON.encode!(~T[14:00:00.123]) == "\"14:00:00.123\""
assert JSON.encode!(~N[2010-04-17 14:00:00.123]) == "\"2010-04-17T14:00:00.123\""
assert JSON.encode!(~U[2010-04-17 14:00:00.123Z]) == "\"2010-04-17T14:00:00.123Z\""
end
end
describe "JSON.Encoder" do