Encoding Dart objects to JSON, can we use JSON.stringify?

58 views
Skip to first unread message

Kasper Peulen

unread,
Jan 20, 2016, 12:15:45 PM1/20/16
to Dart Web Development
Hi all,


that exposes JSON.stringify to Dart. To my surprise it seems to work on modern browsers. For example 

import 'package:json_interop/json_interop.dart' as JSON;

void main() {
var myObj = new MyClass("Kasper", 2, [1, 2, 3]);
var str = JSON.stringify(myObj);
print(str); // '{"name":"Kasper","number":1,"list":[1,2,3]}'
}

class MyClass {
final String name;
final int number;
final List list;

MyClass(this.name, this.number, this.list);
}

This simple example works on Chrome, Firefox and Safari.

It doesn't work in Dartium. My question is, is that a good alternative to dartson?

Is it a bug that it doesn't work on Dartium? Or is this expected? I would love it if it would be easier to convert dart object to json.


Reply all
Reply to author
Forward
0 new messages