@@ -150,9 +150,93 @@ defimpl JSON.Encoder, for: Map do
150150 end
151151end
152152
153- defimpl JSON.Encoder , for: [ Date , Time , NaiveDateTime , DateTime , Duration ] do
153+ defimpl JSON.Encoder , for: Duration do
154154 def encode ( value , _encoder ) do
155- [ ?" , @ for . to_iso8601 ( value ) , ?" ]
155+ [ ?" , Duration . to_iso8601 ( value ) , ?" ]
156+ end
157+ end
158+
159+ defimpl JSON.Encoder , for: Date do
160+ def encode ( % { calendar: Calendar.ISO } = date , _encoder ) do
161+ % { year: year , month: month , day: day } = date
162+ [ ?" , Calendar.ISO . date_to_iodata ( year , month , day ) , ?" ]
163+ end
164+
165+ def encode ( value , _encoder ) do
166+ [ ?" , Date . to_iso8601 ( value ) , ?" ]
167+ end
168+ end
169+
170+ defimpl JSON.Encoder , for: Time do
171+ def encode ( % { calendar: Calendar.ISO } = time , _encoder ) do
172+ % {
173+ hour: hour ,
174+ minute: minute ,
175+ second: second ,
176+ microsecond: microsecond
177+ } = time
178+
179+ [ ?" , Calendar.ISO . time_to_iodata ( hour , minute , second , microsecond ) , ?" ]
180+ end
181+
182+ def encode ( value , _encoder ) do
183+ [ ?" , Time . to_iso8601 ( value ) , ?" ]
184+ end
185+ end
186+
187+ defimpl JSON.Encoder , for: NaiveDateTime do
188+ def encode ( % { calendar: Calendar.ISO } = naive_datetime , _encoder ) do
189+ % {
190+ year: year ,
191+ month: month ,
192+ day: day ,
193+ hour: hour ,
194+ minute: minute ,
195+ second: second ,
196+ microsecond: microsecond
197+ } = naive_datetime
198+
199+ [
200+ ?" ,
201+ Calendar.ISO . date_to_iodata ( year , month , day ) ,
202+ ?T ,
203+ Calendar.ISO . time_to_iodata ( hour , minute , second , microsecond ) ,
204+ ?"
205+ ]
206+ end
207+
208+ def encode ( value , _encoder ) do
209+ [ ?" , NaiveDateTime . to_iso8601 ( value ) , ?" ]
210+ end
211+ end
212+
213+ defimpl JSON.Encoder , for: DateTime do
214+ def encode ( % { calendar: Calendar.ISO } = datetime , _encoder ) do
215+ % {
216+ year: year ,
217+ month: month ,
218+ day: day ,
219+ hour: hour ,
220+ minute: minute ,
221+ second: second ,
222+ microsecond: microsecond ,
223+ time_zone: time_zone ,
224+ utc_offset: utc_offset ,
225+ std_offset: std_offset
226+ } = datetime
227+
228+ [
229+ ?" ,
230+ Calendar.ISO . date_to_iodata ( year , month , day ) ,
231+ ?T ,
232+ Calendar.ISO . time_to_iodata ( hour , minute , second , microsecond ) ,
233+ Calendar.ISO . offset_to_iodata ( utc_offset , std_offset , time_zone , :extended ) ,
234+ ?"
235+ ]
236+ end
237+
238+ def encode ( value , _encoder ) do
239+ [ ?" , DateTime . to_iso8601 ( value ) , ?" ]
156240 end
157241end
158242
0 commit comments