Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions morphy/lib/src/MorphyGenerator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class MorphyGenerator<TValueT extends MorphyX> extends GeneratorForAnnotationX<T
annotation.read('hidePublicConstructor').boolValue,
typesExplicit,
annotation.read('nonSealed').boolValue,
annotation.read('explicitToJson').boolValue,
));

// sb.writeln(createCopyWith(classDef, otherClasses2).replaceAll("\$", ""));
Expand Down
3 changes: 2 additions & 1 deletion morphy/lib/src/createMorphy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ String createMorphy(
bool hidePublicConstructor,
List<Interface> explicitForJson,
bool nonSealed,
bool explicitToJson,
) {
//recursively go through otherClasses and get my fieldnames &

Expand All @@ -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));
Expand Down
6 changes: 3 additions & 3 deletions morphy/lib/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -232,16 +232,16 @@ String getEquals(List<NameType> fields, String className) {
return sb.toString();
}

String createJsonHeader(String className, List<NameType> classGenerics, bool privateConstructor) {
String createJsonHeader(String className, List<NameType> 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();
Expand Down
8 changes: 4 additions & 4 deletions morphy/test/helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
5 changes: 5 additions & 0 deletions morphy_annotation/lib/src/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Morphy implements MorphyX {
final List<Type>? explicitSubTypes;

final bool generateJson;
final bool explicitToJson;

final bool hidePublicConstructor;

Expand Down Expand Up @@ -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,
});
Expand All @@ -104,12 +106,14 @@ class Morphy implements MorphyX {
class Morphy2 implements MorphyX {
final List<Type>? 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,
});
Expand All @@ -119,6 +123,7 @@ abstract class MorphyX {
List<Type>? get explicitSubTypes;

bool get generateJson;
bool get explicitToJson;
}

Object? Function(Never) getGenericToJsonFn(
Expand Down