Skip to content

Commit

Permalink
Support handling classDecl fromJson(String s) in JsonDecodable
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Sep 26, 2024
1 parent 319bdf4 commit b39d072
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/json/lib/json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ mixin _FromJson on _Shared {
.firstWhereOrNull((c) => c.identifier.name == 'fromJson')
?.identifier;
if (fromJson != null) {
// to support fromJson(String s) or others not Map
final fromJsonFirstParam = constructors
.firstWhereOrNull((c) => c.identifier.name == 'fromJson')
?.positionalParameters
.firstOrNull;

if (fromJsonFirstParam?.type != null) {
return RawCode.fromParts([
if (nullCheck != null) nullCheck,
fromJson,
'(',
jsonReference,
' as ',
fromJsonFirstParam!.type.code,
')',
]);
}

return RawCode.fromParts([
if (nullCheck != null) nullCheck,
fromJson,
Expand Down

0 comments on commit b39d072

Please sign in to comment.