Search code examples
jsonflutterjson-deserializationjsonserializer

json_serializable fails to deserialize


I am trying to add json support to my flutter project but has some hard time getting it right.

I love flutter but when it comes to json I wish for gson.

I have created a small project that exemplifies my problem.

Please se https://bitbucket.org/oakstair/json_lab

I get the error type 'Match' is not a subtype of type 'Map' in type cast when trying to run the simple to/from json test.

There is obviously something that I miss here!

Thanks in advance from a stormy Stockholm!

import 'package:json_annotation/json_annotation.dart';

part 'json_lab.g.dart';

@JsonSerializable()
class Match {
  int home;
  int away;
  double homePoints;
  double awayPoints;

  Match(this.home, this.away, {this.homePoints, this.awayPoints});

  factory Match.fromJson(Map<String, dynamic> json) => _$MatchFromJson(json);
  Map<String, dynamic> toJson() => _$MatchToJson(this);
}

@JsonSerializable()
class Tournament {

  List<String> participants; // Teams or Players.
  List<List<Match>> table = new List<List<Match>>();

  Tournament(this.participants, {this.table});

  factory Tournament.fromJson(Map<String, dynamic> json) => _$TournamentFromJson(json);
  Map<String, dynamic> toJson() => _$TournamentToJson(this);
}

Solution

  • I just committed a solution to this problem to the repo.

    I had to add explicitToJson.

    @JsonSerializable(explicitToJson: true)