4

I'm trying to serialize an object to JSON and then back from JSON to object again.

This is the code snippet that is supposed to give me proper JSON:

 LoginRequest req = new LoginRequest();
    req.username = username;
    req.password = password;
    req.created = 123456;
    req.test = "KOTS";
    print(req.toString());

What I'm seeing in the console is this:

{} (:1)

In pubspec.yaml I'm importing json_object as a dependency:

environment:
  sdk: '>=1.0.0 <2.0.0'
dependencies:
  ...
  json_object: any

I have a base message class that extends JsonObject:

import 'package:json_object/json_object.dart';

class Message extends JsonObject {

  int created = new DateTime.now().millisecondsSinceEpoch;

}

and then I have a LoginRequest that extends Message:

import 'Message.dart';

class LoginRequest extends Message {

  String _username;
  String _password;
  String test;

  String get username => _username;
  set username(String username) {
    _username = username.trim();  
  }

  String get password => _password;
  set password(String password) {
    _password = password.trim(); 
  }

}

I was thinking that only the base class will be converting to Json, so I wrote another test case:

Message msg = new Message();
msg.created = 123456;
print(msg.toString());

This is also printing:

{} (:1)

Calling objectToJson does the same:

objectToJson(msg).then((jsonStr) => print(jsonStr));
objectToJson(req).then((jsonStr) => print(jsonStr));

Outputs:

{}
{}

Removing extends JsonObject causes the above code to spew a stack trace:

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&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10     BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12     _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13     BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (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&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#9      BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#10     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#11     _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#12     BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (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&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10     BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12     _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13     BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (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&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#9      BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#10     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#11     _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#12     BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

Importing dart:convert and using JSON.encode does the same:

import 'dart:convert' show JSON;

...

  print(JSON.encode(msg));
  print(JSON.encode(req));

Outputs:

{}
{}

If I remove the extends JsonObject, then it throws a stack trace:

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&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10     BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12     _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13     BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (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&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#9      BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#10     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#11     _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#12     BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (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&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#10     BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#11     _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#12     _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#13     BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

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?

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.

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.

Update:

Based on Robert's answer, it seems doing the boilerplate is unavoidable:

Message.dart

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);
  }

}

LoginRequest.dart

import 'Message.dart';

class LoginRequest extends Message {

  String _username;
  String _password;
  String test;

  String get username => _username;
  set username(String username) {
    _username = username.trim();  
  }

  String get password => _password;
  set password(String password) {
    _password = password.trim(); 
  }

  Map toJson() { 
    Map map = super.toJson();
    map["username"] = this.username;
    map["password"] = this.password;
    return map;
  }  

}

Test 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)

toString I only need to implement once on the Message class, toJson will need to go on every class.

4
  • Using JsonObject / json_object looks like a lot of overhead. Better write your own function or mirrors based serialized. Commented Jan 2, 2015 at 8:56
  • Is mirrors supported in dart2js? Last time I checked people commented that it's not fully implemented in dart2js which means I can't use it in the frontend. Commented Jan 2, 2015 at 9:01
  • And also see this: stackoverflow.com/questions/20024298/… Commented Jan 2, 2015 at 9:14
  • I saw that post, but that was posted in 2013, over a year ago hence the reason I'm asking it again hoping it has been fixed since then. Commented Jan 2, 2015 at 9:18

1 Answer 1

2

I think your problem is that you use print(req.toString());. Have you tried to do this:

objectToJson(req).then((jsonStr) => print(jsonStr)); Maybe this gives you a json string.

I personally feel like you actually SHOULD provide a toJson/toObject method because you have full control over what is serialized and how it is serialized (e.g. exclude private fields, null values, ...).

// EDIT

Because you use extends JsonObject toString() returns an empty map.

// EDIT

https://code.google.com/p/dart/issues/detail?id=6490 doesn't look like it is supported well. But then you can't use the json_object package anyway.

Exception: Uncaught Error: Class '_LocalClassMirror' has no instance getter 'getters'. This should be because the package requires experimental mirror features.

Using JSON.encode you cannot put an object into there. See https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:convert.JsonCodec#id_encode :

Either you specify the second parameter or implement .toJson().

Sign up to request clarification or add additional context in comments.

4 Comments

Already tried that as shown in my updated question, still outputs {}
Try to remove extends JsonObject please.
Doing that throws a stack trace, I've updated my question with the stack trace included.
Implementing toJson seems to be the cleanest solution even though it'll require a bit of boilerplate. I've updated my question with the working toJson implementation, thanks Robert!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.