json_serializable.dart icon indicating copy to clipboard operation
json_serializable.dart copied to clipboard

Either support converting to converter arg types or explicitly disallow non-JSON argument types

Open kevmoo opened this issue 4 years ago • 2 comments

See https://github.com/google/json_serializable.dart/issues/795

Converter takes type of List<String>.

generates

LoginState _$LoginStateFromJson(Map<String, dynamic> json) {
  return LoginState(
    cookies:
        const _CookiesJsonConverter().fromJson(json['cookies'] as List<String>),
  );
}

Of course the as List<String> will fail always.

We could/should do better here!

Annoying, but this is not a blocker. User can just change the converter to do the right thing.

kevmoo avatar Feb 20 '21 01:02 kevmoo

@kevmoo, can you provide guidance to where this code be fixed? Below is what is generated for me, but if I manually fix the code to "AppNodesJsonConverter().fromJson((json['childNodes'] ?? [])as List)" seems to fix the issue:

_$_Node _$_$_NodeFromJson(Map<String, dynamic> json) {
  return _$_Node(
    json['name'] as String,
    childNodes:
        const AppNodesJsonConverter().fromJson(json['childNodes'] as List) ??
            [],
  );
}

pratikpparikh avatar Feb 25 '21 22:02 pratikpparikh

It's a bit gnarly. I'll have to dig in there!

sadly, I'm blocked by another issue - https://github.com/dart-lang/sdk/issues/45127

kevmoo avatar Feb 25 '21 23:02 kevmoo