From 71b8363b3a25310f02c7363de945615eecf29276 Mon Sep 17 00:00:00 2001 From: Michael Tsang Date: Thu, 16 May 2024 15:15:47 +0100 Subject: [PATCH] add explicitToJson annotation --- morphy/lib/src/MorphyGenerator.dart | 1 + morphy/lib/src/createMorphy.dart | 3 ++- morphy/lib/src/helpers.dart | 6 +++--- morphy/test/helpers_test.dart | 8 ++++---- morphy_annotation/lib/src/annotations.dart | 5 +++++ 5 files changed, 15 insertions(+), 8 deletions(-) diff --git a/morphy/lib/src/MorphyGenerator.dart b/morphy/lib/src/MorphyGenerator.dart index c2fbac8..a299bca 100644 --- a/morphy/lib/src/MorphyGenerator.dart +++ b/morphy/lib/src/MorphyGenerator.dart @@ -134,6 +134,7 @@ class MorphyGenerator extends GeneratorForAnnotationX explicitForJson, bool nonSealed, + bool explicitToJson, ) { //recursively go through otherClasses and get my fieldnames & @@ -26,7 +27,7 @@ String createMorphy( //move this into a helper class! if (generateJson) { sb.writeln(createJsonSingleton(classNameTrim, classGenerics)); - sb.writeln(createJsonHeader(className, classGenerics, hidePublicConstructor)); + sb.writeln(createJsonHeader(className, classGenerics, hidePublicConstructor, explicitToJson)); } sb.write(getClassDefinition(isAbstract: isAbstract, nonSealed: nonSealed, className: className)); diff --git a/morphy/lib/src/helpers.dart b/morphy/lib/src/helpers.dart index c35993b..d54c02c 100644 --- a/morphy/lib/src/helpers.dart +++ b/morphy/lib/src/helpers.dart @@ -232,16 +232,16 @@ String getEquals(List fields, String className) { return sb.toString(); } -String createJsonHeader(String className, List classGenerics, bool privateConstructor) { +String createJsonHeader(String className, List classGenerics, bool privateConstructor, bool explicitToJson) { var sb = StringBuffer(); if (!className.startsWith("\$\$")) { var jsonConstructorName = privateConstructor ? "constructor: 'forJsonDoNotUse'" : ""; if (classGenerics.length > 0) // - sb.writeln("@JsonSerializable(explicitToJson: true, genericArgumentFactories: true, $jsonConstructorName)"); + sb.writeln("@JsonSerializable(explicitToJson: $explicitToJson, genericArgumentFactories: true, $jsonConstructorName)"); else - sb.writeln("@JsonSerializable(explicitToJson: true, $jsonConstructorName)"); + sb.writeln("@JsonSerializable(explicitToJson: $explicitToJson, $jsonConstructorName)"); } return sb.toString(); diff --git a/morphy/test/helpers_test.dart b/morphy/test/helpers_test.dart index a5cecf6..67a687e 100644 --- a/morphy/test/helpers_test.dart +++ b/morphy/test/helpers_test.dart @@ -1641,22 +1641,22 @@ class C_Generics_Sing { group("createJsonHeader", () { test("1w non abstract, no generics, private constructor", () { - var result = createJsonHeader("\$Pet", [], true); + var result = createJsonHeader("\$Pet", [], true, true); var expected = "@JsonSerializable(explicitToJson: true, constructor: 'forJsonDoNotUse')"; expectS(result, expected); }); test("2w abstract", () { - var result = createJsonHeader("\$\$Pet", [], true); + var result = createJsonHeader("\$\$Pet", [], true, true); var expected = ""; expectS(result, expected); }); test("3w non abstract, generics, no private constructor", () { - var result = createJsonHeader("\$Pet", [NameTypeClass("name", "type", "className")], false); - var expected = "@JsonSerializable(explicitToJson: true, genericArgumentFactories: true, )"; + var result = createJsonHeader("\$Pet", [NameTypeClass("name", "type", "className")], false, false); + var expected = "@JsonSerializable(explicitToJson: false, genericArgumentFactories: true, )"; expectS(result, expected); }); diff --git a/morphy_annotation/lib/src/annotations.dart b/morphy_annotation/lib/src/annotations.dart index 55e9946..94a18f2 100644 --- a/morphy_annotation/lib/src/annotations.dart +++ b/morphy_annotation/lib/src/annotations.dart @@ -13,6 +13,7 @@ class Morphy implements MorphyX { final List? explicitSubTypes; final bool generateJson; + final bool explicitToJson; final bool hidePublicConstructor; @@ -96,6 +97,7 @@ class Morphy implements MorphyX { const Morphy({ this.explicitSubTypes = null, this.generateJson = false, + this.explicitToJson = true, this.hidePublicConstructor = false, this.nonSealed = false, }); @@ -104,12 +106,14 @@ class Morphy implements MorphyX { class Morphy2 implements MorphyX { final List? explicitSubTypes; final bool generateJson; + final bool explicitToJson; final bool hidePublicConstructor; final bool nonSealed; const Morphy2({ this.explicitSubTypes = null, this.generateJson = false, + this.explicitToJson = true, this.hidePublicConstructor = false, this.nonSealed = false, }); @@ -119,6 +123,7 @@ abstract class MorphyX { List? get explicitSubTypes; bool get generateJson; + bool get explicitToJson; } Object? Function(Never) getGenericToJsonFn(