You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
dart:convert - serialize DateTime to ISO 8601 / RFC 3339 #16843
What steps will reproduce the problem?
=======================================
Run this:
import 'dart:convert';
void main() {
var dt = new DateTime.now();
var json = JSON.encode(dt);
print(json);
}
What is the expected output? What do you see instead?
=====================================================
Expected: current date/time, ISO 8601 formatted.
E.g. "2014-02-14T163300"
Throws "Converting object to an encodable object failed" instead.
What version of the product are you using? On what operating system?
====================================================================
Dart SDK version 1.0.0.3_r30188 on Windows and Ubuntu.
Please provide any additional information below.
================================================
ECMA-404 (JSON) unfortunately does not define a default serialization format for DateTime. However, ISO 8601 has become the de facto standard for DateTime serialization in JSON, widely adapted even on competing platforms. For example, even JSON.NET has moved away from a proprietary Microsoft/ODATA serialization format to ISO 8601: JamesNK/Newtonsoft.Json@fd760e3
I suggest to close the gap and also make it the default JSON serialization and deserialization format in dart:convert.
This will prevent Dart developers from coming up with their own serialization formats for DateTime, which would increase friction and decrease interoperability.
Activity
kevmoo commentedon Feb 15, 2014
The issue: we want to ensure a perfect round-trip for any value we encode.
Encoding a DateTime to a ISO 8601 String is great in theory, but on the way back it would be decoded into a String, not a DateTime
Added Duplicate label.
Marked as being merged into #16628.
kevmoo commentedon Feb 15, 2014
For encoding, please use the toEncodable named paramater on JSON.encode to convert the DateTime as needed.
Added Library-Convert label.