title: Add JSON serializer to every model class?
body: <p>When it comes to JSON encoding in Dart, per <a href="https://plus.google.com/+dartlang/posts/Ru4ngLqXCaE" rel="noreferrer">Seth Ladd's accouncement</a> the finally approved now official way to go is <code>dart:convert</code> + <code>JSON.Encode</code>. </p>
  
  <p>Let's say we have a bunch of model classes (<a href="http://maxhorstmann.net/2013/09/22/plain-old-dart-objects/" rel="noreferrer">PODO</a>s) such as:</p>
  
  <pre><code>class Customer
  {
    int Id;
    String Name;
  }
  </code></pre>
  
  <p>Now, I'd love to be able to just JSON-encode my domain objects like this:</p>
  
  <pre><code>var customer = new Customer()
    ..Id = 17
    ..Name = "John";
  var json = JSON.encode(customer);
  </code></pre>
  
  <p>Unfortunately, this won't work...</p>
  
  <pre><code>Uncaught Error: Converting object to an encodable object failed.
  Stack Trace: 
  #0      _JsonStringifier.stringifyValue (dart:convert/json.dart:416)
  #1      _JsonStringifier.stringify (dart:convert/json.dart:336)
  #2      JsonEncoder.convert (dart:convert/json.dart:177)
  ....
  </code></pre>
  
  <p>... unless we explicitly tell <code>dart:convert</code> how to encode:</p>
  
  <pre><code>class Customer
  {
    int Id;
    String Name;
  
    Map toJson() { 
      Map map = new Map();
      map["Id"] = Id;
      map["Name"] = Name;
      return map;
    }  
  }
  </code></pre>
  
  <p>Do I really have to add a <code>toJson</code> method to every single one of my model classes, or is there a better way?</p>
  
  <p><strong>EDIT</strong>: this is the simple serialization I'm looking for:</p>
  
  <pre><code>{
      "Id": 17,
      "Name": "John"
  }
  </code></pre>
  
  <p>Compare to <code>ToJson</code> in <a href="https://github.com/ServiceStack/ServiceStack.Text" rel="noreferrer">ServiceStack.Text</a>, for instance.</p>
  
  <p>Dart's <a href="http://www.dartdocs.org/documentation/serialization/latest" rel="noreferrer">serialization</a> library (see Matt B's answer below) seems like a step in the right direction. However, this ...</p>
  
  <pre><code>var serialization = new Serialization()
    ..addRuleFor(Customer); 
  var json = JSON.encode(serialization.write(customer, format: new SimpleJsonFormat()));
  </code></pre>
  
  <p>... produces just an array with the values (no keys):</p>
  
  <pre><code>[17,"John"]
  </code></pre>
  
  <p>Using the default <a href="http://www.dartdocs.org/documentation/serialization/latest/index.html#serialization/serialization.SimpleMapFormat@id_SimpleMapFormat-" rel="noreferrer">SimpleMapFormat</a> on the other hand generates this <a href="http://jsfiddle.net/2AcGK/" rel="noreferrer">complex</a> representation.</p>
  
  <p>Still haven't found what I'm looking for...</p>
  
  <p><strong>EDIT 2</strong>: Adding some context: I'm building a RESTful web service in Dart, and I'm looking for a JSON serialization which can easily be consumed by any client, not just another Dart client. For instance, querying the Stack Exchange API for this very question will create <a href="https://api.stackexchange.com/2.1/questions/20024298?order=desc&amp;sort=activity&amp;site=stackoverflow">this JSON response</a>. This is the serialization format I'm looking for. - Or, look at typical JSON responses returned by the <a href="https://dev.twitter.com/docs/api/1.1" rel="noreferrer">Twitter REST API</a> or the <a href="https://developers.facebook.com/docs/graph-api/" rel="noreferrer">Facebook Graph API</a>.</p>
  
  <p><strong>EDIT 3</strong>: I wrote a small <a href="http://maxhorstmann.net/2014/01/31/why-dart-should-learn-json-while-its-still-young/" rel="noreferrer">blog post</a> about this. See also the <a href="https://news.ycombinator.com/item?id=7159926" rel="noreferrer">discussion</a> on Hacker News.</p>