Home > Blockchain >  toJson method throws stackOverflow
toJson method throws stackOverflow

Time:07-27

from backend I'm receiving json that looks like this

    {
    "teamDto": {
        "id": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
        "teamName": "tasdasd",
        "payPerPractice": 123.0,
        "payPerGame": 123.0,
        "season": "asdasd",
        "teamMembers": [
           
            {
                "id": "4657dd50-974c-427c-8e77-0f5e1f4fd23c",
                "isOwner": true,
                "isManager": true,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "6a758edd-fbac-49b5-bed4-250c585b5c3f",
                "isOwner": false,
                "isManager": false,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "98786a8a-3915-4e3a-b832-32fc6c4d8b8d",
                "isOwner": true,
                "isManager": true,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "c3e76455-a7bb-4be4-abe7-09436f6fe3df",
                "isOwner": false,
                "isManager": false,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            }
        ],
        "sport": null
    },
    "owner": true,
    "manager": true
}

I'm aware of imperfections in my json, but thats not the problem. The problem is when i try to convert it to object, my programm throws stackoverflow. Here is my TeamDashboardDtoclass

    class TeamDashboardDto{
      late TeamDto teamDto;
      late bool isOwner;
      late bool isManager;
    
    
      TeamDashboardDto({required this.teamDto, required this.isOwner, required this.isManager});
    
      factory TeamDashboardDto.fromJson(Map<String, dynamic> json) => TeamDashboardDto.fromJson(json);
      Map<String, dynamic> toJson() => UserAndTeamDtoToJson(this);
    
    
      Map<String, dynamic> UserAndTeamDtoToJson(TeamDashboardDto instance) => <String, dynamic>{
        'teamDto': instance.teamDto,
        'owner': instance.isOwner,
        'manager': instance.isManager,
      };
    }




  and here is my TeamDto class

class TeamDto{
  String? id;
  String? teamName;
  double? payPerPractice;
  double? payPerGame;
  String? season;
  List<TeamMemberDto>? teamMembers;


  TeamDto({this.id, this.teamName, this.payPerGame, this.payPerPractice, this.season, this.teamMembers});

  factory TeamDto.fromJson(Map<String, dynamic> json) {
    return TeamDto(
      id: json['id'],
      teamName: json['teamName'],
      payPerPractice: json['payPerPractice'],
      payPerGame: json['payPerGame'],
      season:  json['season'],
     teamMembers: List<TeamMemberDto>.from(
         json['teamMembers'].map((model) => TeamMemberDto.fromJson(model)))
    );
  }


  Map<String, dynamic> toJson() => {
    'id': id,
    'teamName': teamName,
    'sport': sport,
    'payPerGame': payPerGame,
    "payPerPractice": payPerPractice,
    "season": season,
    "teamMembers": teamMembers
  };
}

I dont see why theres a problem because i have other Dto classes that contain two different dto's which one of them is TeamDto, the only difference here is that this dto class contains two booleans and another dto class instead of two dto classes.

CodePudding user response:

You have an infinite call loop here:

factory TeamDashboardDto.fromJson(Map<String, dynamic> json)
  => TeamDashboardDto.fromJson(json);

So your TeamDashboardDto.fromJson factory are calling the TeamDashboardDto.fromJson factory which are calling the TeamDashboardDto.fromJson factory... and so on since it is just calling itself.

CodePudding user response:

This code was generated by other small script.
Generation script was written in less than 5 minutes.

import 'dart:convert';

void main() {
  final json = jsonDecode(_response) as Map;
  final response = Response.fromJson(json);
  print(response.teamDto.teamMembers[0].id);
}

const _response = '''
{
    "teamDto": {
        "id": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
        "teamName": "tasdasd",
        "payPerPractice": 123.0,
        "payPerGame": 123.0,
        "season": "asdasd",
        "teamMembers": [
            {
                "id": "4657dd50-974c-427c-8e77-0f5e1f4fd23c",
                "isOwner": true,
                "isManager": true,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "6a758edd-fbac-49b5-bed4-250c585b5c3f",
                "isOwner": false,
                "isManager": false,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "98786a8a-3915-4e3a-b832-32fc6c4d8b8d",
                "isOwner": true,
                "isManager": true,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "c3e76455-a7bb-4be4-abe7-09436f6fe3df",
                "isOwner": false,
                "isManager": false,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            }
        ],
        "sport": null
    },
    "owner": true,
    "manager": true
}''';

class Response {
  Response({required this.teamDto, required this.owner, required this.manager});

  factory Response.fromJson(Map json) {
    return Response(
      teamDto: TeamDto.fromJson(json['teamDto'] as Map),
      owner: json['owner'] == null ? false : json['owner'] as bool,
      manager: json['manager'] == null ? false : json['manager'] as bool,
    );
  }

  final TeamDto teamDto;

  final bool owner;

  final bool manager;

  static List<Response> fromJsonList(List json) {
    return json.map((e) => Response.fromJson(e as Map)).toList();
  }

  Map<String, dynamic> toJson() {
    return {
      'teamDto': teamDto.toJson(),
      'owner': owner,
      'manager': manager,
    };
  }

  static List<Map<String, dynamic>> toJsonList(List<Response> list) {
    return list.map((e) => e.toJson()).toList();
  }
}

class TeamDto {
  TeamDto(
      {required this.id,
      required this.teamName,
      required this.payPerPractice,
      required this.payPerGame,
      required this.season,
      required this.teamMembers,
      required this.sport});

  factory TeamDto.fromJson(Map json) {
    return TeamDto(
      id: json['id'] == null ? '' : json['id'] as String,
      teamName: json['teamName'] == null ? '' : json['teamName'] as String,
      payPerPractice: json['payPerPractice'] == null
          ? 0.0
          : json['payPerPractice'] as double,
      payPerGame:
          json['payPerGame'] == null ? 0.0 : json['payPerGame'] as double,
      season: json['season'] == null ? '' : json['season'] as String,
      teamMembers: json['teamMembers'] == null
          ? []
          : (json['teamMembers'] as List)
              .map((e) => TeamMemberDto.fromJson(e as Map))
              .toList(),
      sport: json['sport'],
    );
  }

  final String id;

  final String teamName;

  final double payPerPractice;

  final double payPerGame;

  final String season;

  final List<TeamMemberDto> teamMembers;

  final Object? sport;

  static List<TeamDto> fromJsonList(List json) {
    return json.map((e) => TeamDto.fromJson(e as Map)).toList();
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'teamName': teamName,
      'payPerPractice': payPerPractice,
      'payPerGame': payPerGame,
      'season': season,
      'teamMembers': teamMembers.map((e) => e.toJson()).toList(),
      'sport': sport,
    };
  }

  static List<Map<String, dynamic>> toJsonList(List<TeamDto> list) {
    return list.map((e) => e.toJson()).toList();
  }
}

class TeamMemberDto {
  TeamMemberDto(
      {required this.id,
      required this.isOwner,
      required this.isManager,
      required this.teamName,
      required this.teamId,
      required this.name,
      required this.lastName});

  factory TeamMemberDto.fromJson(Map json) {
    return TeamMemberDto(
      id: json['id'] == null ? '' : json['id'] as String,
      isOwner: json['isOwner'] == null ? false : json['isOwner'] as bool,
      isManager: json['isManager'] == null ? false : json['isManager'] as bool,
      teamName: json['teamName'] == null ? '' : json['teamName'] as String,
      teamId: json['teamId'] == null ? '' : json['teamId'] as String,
      name: json['name'] as String?,
      lastName: json['lastName'] as String?,
    );
  }

  final String id;

  final bool isOwner;

  final bool isManager;

  final String teamName;

  final String teamId;

  final String? name;

  final String? lastName;

  static List<TeamMemberDto> fromJsonList(List json) {
    return json.map((e) => TeamMemberDto.fromJson(e as Map)).toList();
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'isOwner': isOwner,
      'isManager': isManager,
      'teamName': teamName,
      'teamId': teamId,
      'name': name,
      'lastName': lastName,
    };
  }

  static List<Map<String, dynamic>> toJsonList(List<TeamMemberDto> list) {
    return list.map((e) => e.toJson()).toList();
  }
}

Generation script.
Models defined in simple YAML format.
It also includes an example template that can be easily removed.

import 'dart:io';

import 'package:object_serializer/json_serializer_generator.dart';
import 'package:yaml/yaml.dart';

void main() {
  final classes = loadYaml(_classes) as Map;
  final g = JsonSerializerGenerator();
  final classesCode = g.generateClasses(classes);
  final values = {
    'classes': classesCode,
  };
  var source = g.render(_template, values);
  source = g.format(source);
  File('bin/stackoverflow.dart').writeAsStringSync(source);
}

const _classes = r'''
Response:
  fields:
    teamDto: TeamDto
    owner: bool
    manager: bool

TeamDto:
  fields:
    id: String
    teamName: String
    payPerPractice: double
    payPerGame: double
    season: String
    teamMembers: List<TeamMemberDto>
    sport: Object?

TeamMemberDto:
  fields:
    id: String
    isOwner: bool
    isManager: bool
    teamName:  String
    teamId: String
    name: String?
    lastName: String?
''';

const _template = r"""
import 'dart:convert';

void main() {
  final json = jsonDecode(_response) as Map;
  final response = Response.fromJson(json);
  print(response.teamDto.teamMembers[0].id);
}

const _response =
    '''
{
    "teamDto": {
        "id": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
        "teamName": "tasdasd",
        "payPerPractice": 123.0,
        "payPerGame": 123.0,
        "season": "asdasd",
        "teamMembers": [
            {
                "id": "4657dd50-974c-427c-8e77-0f5e1f4fd23c",
                "isOwner": true,
                "isManager": true,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "6a758edd-fbac-49b5-bed4-250c585b5c3f",
                "isOwner": false,
                "isManager": false,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "98786a8a-3915-4e3a-b832-32fc6c4d8b8d",
                "isOwner": true,
                "isManager": true,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            },
            {
                "id": "c3e76455-a7bb-4be4-abe7-09436f6fe3df",
                "isOwner": false,
                "isManager": false,
                "teamName": "tasdasd",
                "teamId": "34ec8e71-b5a9-4a4c-b461-5cbb562329b5",
                "name": null,
                "lastName": null
            }
        ],
        "sport": null
    },
    "owner": true,
    "manager": true
}''';


{{classes}}
""";

  •  Tags:  
  • dart
  • Related