State of Serialization and Deserialization of JSON in Dart State of Serialization and Deserialization of JSON in Dart dart dart

State of Serialization and Deserialization of JSON in Dart


For now, the best option is probably to use the Smoke library.

It's a subset of the Mirrors functionality but has both a Mirrors-based and a Codegen-based implementation. It's written by the PolymerDart team, so it's as close to "Official" as we're going to get.

While developing, it'll use the Mirrors-based encoding/decoding; but for publishing you can create a small transformer that will generate code.

Seth Ladd created a code sample here, which I extended slightly to support child-objects:

abstract class Serializable {  static fromJson(Type t, Map json) {    var typeMirror = reflectType(t);    T obj = typeMirror.newInstance(new Symbol(""), const[]).reflectee;    json.forEach((k, v) {      if (v is Map) {        var d = smoke.getDeclaration(t, smoke.nameToSymbol(k));        smoke.write(obj, smoke.nameToSymbol(k), Serializable.fromJson(d.type, v));      } else {        smoke.write(obj, smoke.nameToSymbol(k), v);      }    });    return obj;  }  Map toJson() {    var options = new smoke.QueryOptions(includeProperties: false);    var res = smoke.query(runtimeType, options);    var map = {};    res.forEach((r) => map[smoke.symbolToName(r.name)] = smoke.read(this, r.name));    return map;  }}

Currently, there is no support to get generic type information (eg. to support List) in Smoke; however I've raised a case about this here:

https://code.google.com/p/dart/issues/detail?id=20584

Until this issue is implemented, a "good" implementation of what you want is not really feasible; but I'm hopeful it'll be implemented soon; because doing something as basic as JSON serialisation kinda hinges on it!

Alan Knight is also working on a Serialisation package, however I found it to lack support for things as simple as converting datetimes to strings, and the solution seemed rather verbose for something so basic.

For now, in my own project, I've gone with codegenning our json serialisation (in the form of toMap and fromMap methods) since we would already have C# versions of our classes for the server side. If time allows, I'd like to tidy those code up and make a NuGet package (it supports nested objects, arrays, excluding properties, etc.).


I realize this is an old question, but if you're coming here in 2019+ there is an excellent code-generation solution I've found:

https://pub.dartlang.org/packages/json_serializable

Judging by the git-hub repository I'd say this is the "official" solution as well.

It works using the annotations @JsonSerializable and @JsonKey to control serialization. In my experience it seems to flawlessly handle inheritance and the like as well. It automatically generates serialization and decentralization functions when you build the project.


Currently, you can use redstone_mapper to convert between Dart objects and JSON. This package is a plugin to the Redstone.dart framework, but can be used without it. There's also other options available on Pub