Dart - How to get Json object from Complex object

2019-09-01 08:42发布

问题:

On Continuation to following SO Question/Answer :- How to get JSON serialized string using Dart Serialization I want to serialize complex object values in JSON, how can I achieve that using:-

Map toJson() { }

My class heirarchy is :-

class Container {
  Containerthis.DPC_ID);   
  String get Id => DPC_ID;
  String get LSDetailUrl => "http://www.indiavotes.com/pc/detail/$DPC_ID/$DB_STATEID/15";

  List<GoogleMaps.LatLng> Coordinates;
  List<LSElectionResult> ElectionData;

  var DPC_ID;
  // how to extend following method to serialize complex inner objects too?
  Map toJson() {
     return {"id": DPC_ID, "ElecData2009": ElectionData};
   }
}
class LSElectionResult {  

  String get WinnerName => DWIN_NM;
  String get WinnerParty => DWIN_PRT;
}

Here the first collection - GoogleMaps.LatLng is external class, but I want to serialize it too. But second collection member ElectionData is of my own class LSElectionResult, If I write Map toJson() implementation for LSElectionResult, will it be called automatically if I call Container.toJson() ?

I am going to compile this to JS.