title: Dart Object -> JSON String failing to convert to JSON
body: <p>I'm trying to serialize an object to JSON and then back from JSON to object again.</p>
  
  <p>This is the code snippet that is supposed to give me proper JSON:</p>
  
  <pre><code> LoginRequest req = new LoginRequest();
      req.username = username;
      req.password = password;
      req.created = 123456;
      req.test = "KOTS";
      print(req.toString());
  </code></pre>
  
  <p>What I'm seeing in the console is this:</p>
  
  <pre><code>{} (:1)
  </code></pre>
  
  <p>In pubspec.yaml I'm importing json_object as a dependency:</p>
  
  <pre><code>environment:
    sdk: '&gt;=1.0.0 &lt;2.0.0'
  dependencies:
    ...
    json_object: any
  </code></pre>
  
  <p>I have a base message class that extends JsonObject:</p>
  
  <pre><code>import 'package:json_object/json_object.dart';
  
  class Message extends JsonObject {
  
    int created = new DateTime.now().millisecondsSinceEpoch;
  
  }
  </code></pre>
  
  <p>and then I have a LoginRequest that extends Message:</p>
  
  <pre><code>import 'Message.dart';
  
  class LoginRequest extends Message {
  
    String _username;
    String _password;
    String test;
  
    String get username =&gt; _username;
    set username(String username) {
      _username = username.trim();  
    }
  
    String get password =&gt; _password;
    set password(String password) {
      _password = password.trim(); 
    }
  
  }
  </code></pre>
  
  <p>I was thinking that only the base class will be converting to Json, so I wrote another test case:</p>
  
  <pre><code>Message msg = new Message();
  msg.created = 123456;
  print(msg.toString());
  </code></pre>
  
  <p>This is also printing:</p>
  
  <pre><code>{} (:1)
  </code></pre>
  
  <p>Calling objectToJson does the same:</p>
  
  <pre><code>objectToJson(msg).then((jsonStr) =&gt; print(jsonStr));
  objectToJson(req).then((jsonStr) =&gt; print(jsonStr));
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>{}
  {}
  </code></pre>
  
  <p>Removing extends JsonObject causes the above code to spew a stack trace:</p>
  
  <pre><code>Exception: Uncaught Error: Converting object to an encodable object failed.
  Stack Trace:
  #0      _JsonStringifier.writeObject (dart:convert/json.dart:660)
  #1      _JsonStringStringifier.printOn (dart:convert/json.dart:831)
  #2      _JsonStringStringifier.stringify (dart:convert/json.dart:813)
  #3      JsonEncoder.convert (dart:convert/json.dart:243)
  #4      JsonCodec.encode (dart:convert/json.dart:141)
  #5      login (package:falm/login-dialog.dart:47:22)
  #6      Function.apply (dart:core-patch/function_patch.dart:28)
  #7      GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
  #8      invoke (package:smoke/smoke.dart:43:41)
  #9      HtmlElement&amp;Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
  #10     BindingDelegate&amp;PolymerEventBindings.getEventHandler.&lt;anonymous closure&gt; (package:polymer/src/events.dart:82:32)
  #11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
  #12     _RootZone.bindUnaryCallback.&lt;anonymous closure&gt; (dart:async/zone.dart:1122)
  #13     BindingDelegate&amp;PolymerEventBindings.prepareEventBinding.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:polymer/src/events.dart:101:67)
  
  Clicked Login (:1)
  Exception: Uncaught Error: Class '_LocalClassMirror' has no instance getter 'getters'.
  
  NoSuchMethodError: method not found: 'getters'
  Receiver: Instance of '_LocalClassMirror'
  Arguments: []
  Stack Trace:
  #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
  #1      _serializeObject (package:json_object/src/mirror_based_serializer.dart:127:16)
  #2      objectToSerializable (package:json_object/src/mirror_based_serializer.dart:53:21)
  #3      objectToJson (package:json_object/src/mirror_based_serializer.dart:22:23)
  #4      login (package:falm/login-dialog.dart:43:17)
  #5      Function.apply (dart:core-patch/function_patch.dart:28)
  #6      GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
  #7      invoke (package:smoke/smoke.dart:43:41)
  #8      HtmlElement&amp;Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
  #9      BindingDelegate&amp;PolymerEventBindings.getEventHandler.&lt;anonymous closure&gt; (package:polymer/src/events.dart:82:32)
  #10     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
  #11     _RootZone.bindUnaryCallback.&lt;anonymous closure&gt; (dart:async/zone.dart:1122)
  #12     BindingDelegate&amp;PolymerEventBindings.prepareEventBinding.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:polymer/src/events.dart:101:67)
  
  Clicked Login (:1)
  Exception: Uncaught Error: Converting object to an encodable object failed.
  Stack Trace:
  #0      _JsonStringifier.writeObject (dart:convert/json.dart:660)
  #1      _JsonStringStringifier.printOn (dart:convert/json.dart:831)
  #2      _JsonStringStringifier.stringify (dart:convert/json.dart:813)
  #3      JsonEncoder.convert (dart:convert/json.dart:243)
  #4      JsonCodec.encode (dart:convert/json.dart:141)
  #5      login (package:falm/login-dialog.dart:47:22)
  #6      Function.apply (dart:core-patch/function_patch.dart:28)
  #7      GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
  #8      invoke (package:smoke/smoke.dart:43:41)
  #9      HtmlElement&amp;Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
  #10     BindingDelegate&amp;PolymerEventBindings.getEventHandler.&lt;anonymous closure&gt; (package:polymer/src/events.dart:82:32)
  #11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
  #12     _RootZone.bindUnaryCallback.&lt;anonymous closure&gt; (dart:async/zone.dart:1122)
  #13     BindingDelegate&amp;PolymerEventBindings.prepareEventBinding.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:polymer/src/events.dart:101:67)
  
  Clicked Login (:1)
  Exception: Uncaught Error: Class '_LocalClassMirror' has no instance getter 'getters'.
  
  NoSuchMethodError: method not found: 'getters'
  Receiver: Instance of '_LocalClassMirror'
  Arguments: []
  Stack Trace:
  #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
  #1      _serializeObject (package:json_object/src/mirror_based_serializer.dart:127:16)
  #2      objectToSerializable (package:json_object/src/mirror_based_serializer.dart:53:21)
  #3      objectToJson (package:json_object/src/mirror_based_serializer.dart:22:23)
  #4      login (package:falm/login-dialog.dart:44:17)
  #5      Function.apply (dart:core-patch/function_patch.dart:28)
  #6      GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
  #7      invoke (package:smoke/smoke.dart:43:41)
  #8      HtmlElement&amp;Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
  #9      BindingDelegate&amp;PolymerEventBindings.getEventHandler.&lt;anonymous closure&gt; (package:polymer/src/events.dart:82:32)
  #10     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
  #11     _RootZone.bindUnaryCallback.&lt;anonymous closure&gt; (dart:async/zone.dart:1122)
  #12     BindingDelegate&amp;PolymerEventBindings.prepareEventBinding.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:polymer/src/events.dart:101:67)
  </code></pre>
  
  <p>Importing dart:convert and using JSON.encode does the same:</p>
  
  <pre><code>import 'dart:convert' show JSON;
  
  ...
  
    print(JSON.encode(msg));
    print(JSON.encode(req));
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>{}
  {}
  </code></pre>
  
  <p>If I remove the extends JsonObject, then it throws a stack trace:</p>
  
  <pre><code>Exception: Uncaught Error: Converting object to an encodable object failed.
  Stack Trace:
  #0      _JsonStringifier.writeObject (dart:convert/json.dart:660)
  #1      _JsonStringStringifier.printOn (dart:convert/json.dart:831)
  #2      _JsonStringStringifier.stringify (dart:convert/json.dart:813)
  #3      JsonEncoder.convert (dart:convert/json.dart:243)
  #4      JsonCodec.encode (dart:convert/json.dart:141)
  #5      login (package:falm/login-dialog.dart:47:22)
  #6      Function.apply (dart:core-patch/function_patch.dart:28)
  #7      GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
  #8      invoke (package:smoke/smoke.dart:43:41)
  #9      HtmlElement&amp;Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
  #10     BindingDelegate&amp;PolymerEventBindings.getEventHandler.&lt;anonymous closure&gt; (package:polymer/src/events.dart:82:32)
  #11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
  #12     _RootZone.bindUnaryCallback.&lt;anonymous closure&gt; (dart:async/zone.dart:1122)
  #13     BindingDelegate&amp;PolymerEventBindings.prepareEventBinding.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:polymer/src/events.dart:101:67)
  
  Clicked Login (:1)
  Exception: Uncaught Error: Class '_LocalClassMirror' has no instance getter 'getters'.
  
  NoSuchMethodError: method not found: 'getters'
  Receiver: Instance of '_LocalClassMirror'
  Arguments: []
  Stack Trace:
  #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
  #1      _serializeObject (package:json_object/src/mirror_based_serializer.dart:127:16)
  #2      objectToSerializable (package:json_object/src/mirror_based_serializer.dart:53:21)
  #3      objectToJson (package:json_object/src/mirror_based_serializer.dart:22:23)
  #4      login (package:falm/login-dialog.dart:43:17)
  #5      Function.apply (dart:core-patch/function_patch.dart:28)
  #6      GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
  #7      invoke (package:smoke/smoke.dart:43:41)
  #8      HtmlElement&amp;Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
  #9      BindingDelegate&amp;PolymerEventBindings.getEventHandler.&lt;anonymous closure&gt; (package:polymer/src/events.dart:82:32)
  #10     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
  #11     _RootZone.bindUnaryCallback.&lt;anonymous closure&gt; (dart:async/zone.dart:1122)
  #12     BindingDelegate&amp;PolymerEventBindings.prepareEventBinding.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:polymer/src/events.dart:101:67)
  
  Clicked Login (:1)
  Exception: Uncaught Error: Converting object to an encodable object failed.
  Stack Trace:
  #0      _JsonStringifier.writeObject (dart:convert/json.dart:660)
  #1      _JsonStringStringifier.printOn (dart:convert/json.dart:831)
  #2      _JsonStringStringifier.stringify (dart:convert/json.dart:813)
  #3      JsonEncoder.convert (dart:convert/json.dart:243)
  #4      JsonCodec.encode (dart:convert/json.dart:141)
  #5      login (package:falm/login-dialog.dart:47:22)
  #6      Function.apply (dart:core-patch/function_patch.dart:28)
  #7      GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
  #8      invoke (package:smoke/smoke.dart:43:41)
  #9      HtmlElement&amp;Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
  #10     BindingDelegate&amp;PolymerEventBindings.getEventHandler.&lt;anonymous closure&gt; (package:polymer/src/events.dart:82:32)
  #11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
  #12     _RootZone.bindUnaryCallback.&lt;anonymous closure&gt; (dart:async/zone.dart:1122)
  #13     BindingDelegate&amp;PolymerEventBindings.prepareEventBinding.&lt;anonymous closure&gt;.&lt;anonymous closure&gt; (package:polymer/src/events.dart:101:67)
  </code></pre>
  
  <p>Is JsonObject still the right way to go for serializing objects to JSON and deserializing JSON to objects? (I see in the code copyright 2013 which is ancient by now). If so, am I missing something in my classes?</p>
  
  <p>In the dart cookbook there are examples where every class has its own toJson method and where all the values are manually copied into a map, this is cumbersome, if my whole app is JSON-driven, then I'll be spending most of my time writing boilerplate toJson / fromJson methods - this is exactly what I'm trying to get away from, hence the reason for choosing dart.</p>
  
  <p>Furthermore I see examples on StackOverflow where mirrors are being used followed by comments that mirrors aren't fully supported in dart2js - since this is a browser based application, it is crucial that it can compile to javascript.</p>
  
  <p><strong>Update</strong>:</p>
  
  <p>Based on Robert's answer, it seems doing the boilerplate is unavoidable:</p>
  
  <p>Message.dart</p>
  
  <pre><code>import 'dart:convert' show JSON;
  
  class Message {
  
    int created = new DateTime.now().millisecondsSinceEpoch;
  
    Map toJson() { 
      Map map = new Map();
      map["created"] = this.created;
      return map;
    }  
  
    String toString(){
      return JSON.encode(this);
    }
  
  }
  </code></pre>
  
  <p>LoginRequest.dart</p>
  
  <pre><code>import 'Message.dart';
  
  class LoginRequest extends Message {
  
    String _username;
    String _password;
    String test;
  
    String get username =&gt; _username;
    set username(String username) {
      _username = username.trim();  
    }
  
    String get password =&gt; _password;
    set password(String password) {
      _password = password.trim(); 
    }
  
    Map toJson() { 
      Map map = super.toJson();
      map["username"] = this.username;
      map["password"] = this.password;
      return map;
    }  
  
  }
  </code></pre>
  
  <p>Test code:</p>
  
  <pre><code>LoginRequest req = new LoginRequest();
  req.username = username;
  req.password = password;
  req.created = 123456;
  req.test = "KOTS";
  print(req);
  // outputs: {"created":123456,"username":"asdfasdf","password":"adfasdf"} (:1)
  
  Message msg = new Message();
  msg.created = 123456;
  print(msg);
  // outputs: {"created":123456} (:1)
  </code></pre>
  
  <p>toString I only need to implement once on the Message class, toJson will need to go on every class.</p>