How to convert an object containing DateTime fields to JSON in Dart? -
i try convert object json.
var obj = { "dt": new datetime.now() }; var s = stringify(obj);
the runtime throws exception: "calling tojson method on object failed."
that's expected since datetime class doesn't have tojson method. should in case?
javascript's json.stringify
function has optional argument replacer allows me provide own way of serialization of object if object has no tojson method. there similar facility in dart or maybe can extend somehow datetime class own tojson method?
zdeslav vojkovic's answer outdated now.
the json.encode()
method in dart:convert
has optional toencodable
method invoked objects not natively serializable json. it's user provide closure returns appropriate serialization of datetime.
Comments
Post a Comment