Implement JSON.Encoder for Calendar types (#14061)
This commit is contained in:
@@ -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`.
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user