Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 419f661

Browse files
committedMay 16, 2024
add explicitToJson annotation
1 parent 635aaf1 commit 419f661

File tree

8 files changed

+27
-12
lines changed

8 files changed

+27
-12
lines changed
 

‎morphy/lib/src/MorphyGenerator.dart

+1
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class MorphyGenerator<TValueT extends MorphyX> extends GeneratorForAnnotationX<T
134134
annotation.read('hidePublicConstructor').boolValue,
135135
typesExplicit,
136136
annotation.read('nonSealed').boolValue,
137+
annotation.read('explicitToJson').boolValue,
137138
));
138139

139140
// sb.writeln(createCopyWith(classDef, otherClasses2).replaceAll("\$", ""));

‎morphy/lib/src/createMorphy.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ String createMorphy(
1515
bool hidePublicConstructor,
1616
List<Interface> explicitForJson,
1717
bool nonSealed,
18+
bool explicitToJson,
1819
) {
1920
//recursively go through otherClasses and get my fieldnames &
2021

@@ -26,7 +27,7 @@ String createMorphy(
2627
//move this into a helper class!
2728
if (generateJson) {
2829
sb.writeln(createJsonSingleton(classNameTrim, classGenerics));
29-
sb.writeln(createJsonHeader(className, classGenerics, hidePublicConstructor));
30+
sb.writeln(createJsonHeader(className, classGenerics, hidePublicConstructor, explicitToJson));
3031
}
3132

3233
sb.write(getClassDefinition(isAbstract: isAbstract, nonSealed: nonSealed, className: className));

‎morphy/lib/src/helpers.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -232,16 +232,16 @@ String getEquals(List<NameType> fields, String className) {
232232
return sb.toString();
233233
}
234234

235-
String createJsonHeader(String className, List<NameType> classGenerics, bool privateConstructor) {
235+
String createJsonHeader(String className, List<NameType> classGenerics, bool privateConstructor, bool explicitToJson) {
236236
var sb = StringBuffer();
237237

238238
if (!className.startsWith("\$\$")) {
239239
var jsonConstructorName = privateConstructor ? "constructor: 'forJsonDoNotUse'" : "";
240240

241241
if (classGenerics.length > 0) //
242-
sb.writeln("@JsonSerializable(explicitToJson: true, genericArgumentFactories: true, $jsonConstructorName)");
242+
sb.writeln("@JsonSerializable(explicitToJson: $explicitToJson, genericArgumentFactories: true, $jsonConstructorName)");
243243
else
244-
sb.writeln("@JsonSerializable(explicitToJson: true, $jsonConstructorName)");
244+
sb.writeln("@JsonSerializable(explicitToJson: $explicitToJson, $jsonConstructorName)");
245245
}
246246

247247
return sb.toString();

‎morphy/pubspec.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ packages:
303303
path: "../morphy_annotation"
304304
relative: true
305305
source: path
306-
version: "1.0.0"
306+
version: "1.2.0"
307307
node_preamble:
308308
dependency: transitive
309309
description:

‎morphy/pubspec.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ homepage: https://github.com/atreeon/morphy
66
environment:
77
sdk: ">=3.1.3 <4.0.0"
88

9-
#dependency_overrides:
10-
# morphy_annotation:
11-
# path: ../morphy_annotation
9+
dependency_overrides:
10+
morphy_annotation:
11+
path: ../morphy_annotation
1212

1313
dependencies:
1414
morphy_annotation: ^1.1.0

‎morphy/test/helpers_test.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -1641,22 +1641,22 @@ class C_Generics_Sing {
16411641

16421642
group("createJsonHeader", () {
16431643
test("1w non abstract, no generics, private constructor", () {
1644-
var result = createJsonHeader("\$Pet", [], true);
1644+
var result = createJsonHeader("\$Pet", [], true, true);
16451645
var expected = "@JsonSerializable(explicitToJson: true, constructor: 'forJsonDoNotUse')";
16461646

16471647
expectS(result, expected);
16481648
});
16491649

16501650
test("2w abstract", () {
1651-
var result = createJsonHeader("\$\$Pet", [], true);
1651+
var result = createJsonHeader("\$\$Pet", [], true, true);
16521652
var expected = "";
16531653

16541654
expectS(result, expected);
16551655
});
16561656

16571657
test("3w non abstract, generics, no private constructor", () {
1658-
var result = createJsonHeader("\$Pet", [NameTypeClass("name", "type", "className")], false);
1659-
var expected = "@JsonSerializable(explicitToJson: true, genericArgumentFactories: true, )";
1658+
var result = createJsonHeader("\$Pet", [NameTypeClass("name", "type", "className")], false, false);
1659+
var expected = "@JsonSerializable(explicitToJson: false, genericArgumentFactories: true, )";
16601660

16611661
expectS(result, expected);
16621662
});

‎morphy_annotation/lib/src/annotations.dart

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Morphy implements MorphyX {
1313
final List<Type>? explicitSubTypes;
1414

1515
final bool generateJson;
16+
final bool explicitToJson;
1617

1718
final bool hidePublicConstructor;
1819

@@ -96,6 +97,7 @@ class Morphy implements MorphyX {
9697
const Morphy({
9798
this.explicitSubTypes = null,
9899
this.generateJson = false,
100+
this.explicitToJson = true,
99101
this.hidePublicConstructor = false,
100102
this.nonSealed = false,
101103
});
@@ -104,12 +106,14 @@ class Morphy implements MorphyX {
104106
class Morphy2 implements MorphyX {
105107
final List<Type>? explicitSubTypes;
106108
final bool generateJson;
109+
final bool explicitToJson;
107110
final bool hidePublicConstructor;
108111
final bool nonSealed;
109112

110113
const Morphy2({
111114
this.explicitSubTypes = null,
112115
this.generateJson = false,
116+
this.explicitToJson = true,
113117
this.hidePublicConstructor = false,
114118
this.nonSealed = false,
115119
});
@@ -119,6 +123,7 @@ abstract class MorphyX {
119123
List<Type>? get explicitSubTypes;
120124

121125
bool get generateJson;
126+
bool get explicitToJson;
122127
}
123128

124129
Object? Function(Never) getGenericToJsonFn(

‎morphy_annotation/pubspec.lock

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ packages:
4141
url: "https://pub.dev"
4242
source: hosted
4343
version: "1.2.0"
44+
json_annotation:
45+
dependency: "direct main"
46+
description:
47+
name: json_annotation
48+
sha256: "1ce844379ca14835a50d2f019a3099f419082cfdd231cd86a142af94dd5c6bb1"
49+
url: "https://pub.dev"
50+
source: hosted
51+
version: "4.9.0"
4452
matcher:
4553
dependency: transitive
4654
description:

0 commit comments

Comments
 (0)
Please sign in to comment.