3

This question is related to this post.

I tried the following code:

import 'dart:convert';

/*server side Post class */
class Post {
  int post_id;
  String title;
  String description;
  DateTime posted_at;
  DateTime last_edited;
  String user;
  String editor;
  int up_votes;
  int down_votes;
  int total_votes;
  String links_to;
  List<String> tags = new List();

  Post.fromSQL(List sql_post) {
     //initialization code, unrelated.
  }

  Map toJson(){
    Map fromObject = {
      'post_id' : post_id,
      'title' : title,
      'description' : description,
      'posted_at' : posted_at,
      'last_edited' : last_edited,
      'user' : user,
      'editor' : editor,
      'up_votes' : up_votes,
      'dwon_votes' : down_votes,
      'total_votes' : total_votes,
      'links_to' : links_to,
      'tags' : tags
    };

    return fromObject;
    //I use the code below as a temporary solution
    //JSON.encode(fromObject, toEncodable: (date)=>date.toString());
  }
}

I have a temporary solution, but I would really like to be able to do the following.

JSON.encode(posts, toEncodable: (date)=>date.toString())

where posts is a list of Post objects. I would expect this to convert into a json list of json representations of the Post class. What I get is a list of "Instance of 'Post'" strings. So the question is, is this syntax no longer supported, or should I do something differently?

Community
  • 1
  • 1
Lukasz
  • 2,257
  • 3
  • 26
  • 44

1 Answers1

5

It seems you can only use toEncodable: OR the toJson() fallback.

If you wrap your Date in a class which provides toJson() you don't need to use toEncodable::

class JsonDateTime {
  final DateTime value;
  JsonDateTime(this.value);

  String toJson() => value != null ? value.toIso8601String() : null;
}

class Post {
  ...
  Map toJson() => {
    'post_id' : post_id,
    'title' : title,
    'description' : description,
    'posted_at' : new JsonDateTime(posted_at),
    'last_edited' : new JsonDateTime(last_edited),
    'user' : user,
    'editor' : editor,
    'up_votes' : up_votes,
    'dwon_votes' : down_votes,
    'total_votes' : total_votes,
    'links_to' : links_to,
    'tags' : tags
  };
}

or alternatively ensure that your toEncodeable: handles every not supported type:

print(JSON.encode(data, toEncodable: (value) {
  if (value is DateTime) {
    return value.toIso8601String();
  } else {
    return value.toJson();
  }
}));
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I misunderstood the documentation. I fought the `toEncodable` is run on those sub elements of the object returned by toJson() which are not encodable. I resolved it finally by changing the Map slightly as follows: `'posted_at' : posted_at.toIso8601String(), 'last_edited' : last_edited.toIso8601String(),` and removed `toEncodable` entirely. – Lukasz May 19 '15 at 13:53
  • That would be nice but doesn't seem to be the case. This is also a good solution of course. – Günter Zöchbauer May 19 '15 at 13:55
  • 1
    The `toEncodable` function is the function to use, there is no other fallback for unencodable object. The `toJson` call is just the default `toEncodable` function used if you don't provide another one. – lrn May 26 '15 at 11:49