From b39d072c0de204e646842c12c3a471b8875f6c7a Mon Sep 17 00:00:00 2001 From: Morlay Date: Sat, 7 Sep 2024 09:54:32 +0800 Subject: [PATCH] Support handling classDecl fromJson(String s) in JsonDecodable --- pkg/json/lib/json.dart | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pkg/json/lib/json.dart b/pkg/json/lib/json.dart index bcf237f7a7aa..475adf8995ef 100644 --- a/pkg/json/lib/json.dart +++ b/pkg/json/lib/json.dart @@ -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,