11

I asked a question before about Dart encoding/decoding to JSON, however, the libraries that were suggested were not complete and I decided to manually handle that.

The objective is to convert these objects to a map.

class Parent extends Object {
   int id;
   String name;
   List<Child> listChild = new List<Child>();
   Map toMap() => {"id":id, "name":name, "listChild":listChild};
}

class Child extends Object {
   int id;
   String childName;
   Map toMap() => {"id":id, "childName":childName};   
}

When doing

print(JSON.encode(parent.toMap()));

I am seeing it go here, any suggestion how to make this work?

if (!stringifyJsonValue(object)) {
  checkCycle(object);
  try {
    var customJson = _toEncodable(object);
    if (!stringifyJsonValue(customJson)) {
      throw new JsonUnsupportedObjectError(object);
    }
    _removeSeen(object);
  } catch (e) {
    throw new JsonUnsupportedObjectError(object, cause : e);
  }
}
}
Community
  • 1
  • 1
javapadawan
  • 897
  • 6
  • 12
  • 29

3 Answers3

8
Map toMap() => {"id":id, "name":name: "listChild": listChild.map((c) => c.toJson().toList())};

is valid for JSON.

import 'dart:convert' show JSON;

...

String json = JSON.encode(toMap());

You can also use the toEncodeable callback - see How to convert DateTime object to json

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
5

If your class structure does not contain's any inner class then follow

class Data{

  String name;
  String type;

  Map<String, dynamic> toJson() => {
        'name': name,
        'type': type
      };
}

If your class uses inner class structure

class QuestionTag {
  String name;
  List<SubTags> listSubTags;

  Map<String, dynamic> toJson() => {
        'name': name,
        'listSubTags': listSubTags.map((tag) => tag.toJson()).toList()
      };
}

class SubTags {
  String tagName;
  String tagDesc;

  SubTags(this.tagName, this.tagDesc);

  Map<String, dynamic> toJson() => {
        'tagName': tagName,
        'tagDesc': tagDesc,
      };
}
Dhanaji Yadav
  • 1,202
  • 1
  • 14
  • 22
3

Just rename Map toMap() into Map toJson() and it will work fine. =)

void encode() {
    Parent p = new Parent();
    Child c1 = new Child();
    c1 ..id = 1 ..childName = "Alex";

    Child c2 = new Child();
    c2 ..id = 2 ..childName = "John";

    Child c3 = new Child();
    c3 ..id = 3 ..childName = "Jane";

    p ..id = 1 ..name = "Lisa" ..listChild = [c1,c2,c3];

    String json = JSON.encode(p);
    print(json);
}

class Parent extends Object {
    int id;
    String name;
    List<Child> listChild = new List<Child>();
    Map toJson() => {"id":id, "name":name, "listChild":listChild};
}

class Child extends Object {
    int id;
    String childName;
    Map toJson() => {"id":id, "childName":childName};   
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Dafen
  • 1,122
  • 1
  • 9
  • 26
  • that solved my problem. I've been struggling to find some documentation on the convert API that clearly mentions object to json conversion, can you share the link that you got this from? – javapadawan Sep 14 '14 at 16:34
  • Hi, good to hear that it helped! I think I looked at the sourcee code of json.dart to find out, but I am nur sure anymore: https://chromiumcodereview.appspot.com/10914009/patch/7001/6002 – Dafen Sep 14 '14 at 18:35
  • The documentation is on JSON.encode: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-convert.JsonCodec#id_encode . It states that any object that doesn't directly correspond to a JSON literal (num/String/bool/null/List/Map) will be sent through the toEncodable function. You can change `JSON.encode(p)` to `JSON.encode(p, (x)=>x.toMap())` and it should work for objects with a toMap method. – lrn Sep 15 '14 at 12:59