Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for combining this library with realm #1350

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,13 @@ class JsonSerializable {
/// `includeIfNull`, that value takes precedent.
final bool? includeIfNull;

/// Whether the generator should point to the realm generated class
/// `false` is the default
///
/// If `true` the generator is going to point the generated methods to the
/// realm generated model
final bool? realmCompatible;

/// A list of [JsonConverter] to apply to this class.
///
/// Writing:
Expand Down Expand Up @@ -254,6 +261,7 @@ class JsonSerializable {
this.fieldRename,
this.ignoreUnannotated,
this.includeIfNull,
this.realmCompatible,
this.converters,
this.genericArgumentFactories,
this.createPerFieldToJson,
Expand All @@ -276,6 +284,7 @@ class JsonSerializable {
fieldRename: FieldRename.none,
ignoreUnannotated: false,
includeIfNull: true,
realmCompatible: false,
genericArgumentFactories: false,
);

Expand All @@ -297,6 +306,7 @@ class JsonSerializable {
fieldRename: fieldRename ?? defaults.fieldRename,
ignoreUnannotated: ignoreUnannotated ?? defaults.ignoreUnannotated,
includeIfNull: includeIfNull ?? defaults.includeIfNull,
realmCompatible: realmCompatible ?? defaults.realmCompatible,
genericArgumentFactories:
genericArgumentFactories ?? defaults.genericArgumentFactories,
);
Expand Down
7 changes: 6 additions & 1 deletion json_annotation/lib/src/json_serializable.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.7.2

- Support generated model classes.

## 6.7.1

- Support the latest `package:analyzer`.
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions json_serializable/build/unit_test_assets/FontManifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
Binary file not shown.
Binary file not shown.
32 changes: 27 additions & 5 deletions json_serializable/lib/src/decode_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ mixin DecodeHelper implements HelperCore {
.toList(),
unavailableReasons,
deserializeFun,
realmCompatible: config.realmCompatible,
);

final checks = _checkKeys(
Expand Down Expand Up @@ -266,8 +267,9 @@ _ConstructorData _writeConstructorInvocation(
Iterable<String> writableFields,
Map<String, String> unavailableReasons,
String Function(String paramOrFieldName, {ParameterElement ctorParam})
deserializeForField,
) {
deserializeForField, {
bool realmCompatible = false,
}) {
final className = classElement.name;

final ctor = constructorByName(classElement, constructorName);
Expand Down Expand Up @@ -304,14 +306,15 @@ _ConstructorData _writeConstructorInvocation(
}

// fields that aren't already set by the constructor and that aren't final
final remainingFieldsForInvocationBody =
writableFields.toSet().difference(usedCtorParamsAndFields);
final remainingFieldsForInvocationBody = realmCompatible
? <String>{}
: writableFields.toSet().difference(usedCtorParamsAndFields);

final constructorExtra = constructorName.isEmpty ? '' : '.$constructorName';

final buffer = StringBuffer()
..write(
'$className'
'${realmCompatible ? className.replaceFirst('_', '') : className}'
'${genericClassArguments(classElement, false)}'
'$constructorExtra(',
);
Expand All @@ -333,6 +336,25 @@ _ConstructorData _writeConstructorInvocation(
return ' ${paramElement.name}: $value,\n';
}));
}
if (realmCompatible && writableFields.isNotEmpty) {
final remainElements = classElement.fields.where(
(e) => writableFields.contains(e.name),
);

usedCtorParamsAndFields.addAll(writableFields);

buffer
..writeln()
..writeAll(remainElements.map((fieldElement) {
final content = deserializeForField(fieldElement.name);

if (fieldElement.type.isNullableType) {
return ' ${fieldElement.name}: $content,\n';
} else {
return ' $content,\n';
}
}));
}

buffer.write(')');

Expand Down
7 changes: 6 additions & 1 deletion json_serializable/lib/src/helper_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ abstract class HelperCore {
void addMember(String memberContent);

@protected
String get targetClassReference =>
String get targetClassReferenceName =>
'${element.name}${genericClassArgumentsImpl(withConstraints: false)}';

@protected
String get targetClassReference => config.realmCompatible
? targetClassReferenceName.replaceFirst('_', '')
: targetClassReferenceName;

@protected
String nameAccess(FieldElement field) => jsonKeyFor(field).name;

Expand Down
6 changes: 6 additions & 0 deletions json_serializable/lib/src/type_helpers/config_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ClassConfig {
final bool genericArgumentFactories;
final bool ignoreUnannotated;
final bool includeIfNull;
final bool realmCompatible;
final Map<String, String> ctorParamDefaults;
final List<DartObject> converters;

Expand All @@ -73,6 +74,7 @@ class ClassConfig {
required this.genericArgumentFactories,
required this.ignoreUnannotated,
required this.includeIfNull,
required this.realmCompatible,
this.converters = const [],
this.ctorParamDefaults = const {},
});
Expand All @@ -96,6 +98,8 @@ class ClassConfig {
config.explicitToJson ?? ClassConfig.defaults.explicitToJson,
includeIfNull:
config.includeIfNull ?? ClassConfig.defaults.includeIfNull,
realmCompatible:
config.realmCompatible ?? ClassConfig.defaults.realmCompatible,
genericArgumentFactories: config.genericArgumentFactories ??
ClassConfig.defaults.genericArgumentFactories,
fieldRename: config.fieldRename ?? ClassConfig.defaults.fieldRename,
Expand All @@ -120,6 +124,7 @@ class ClassConfig {
genericArgumentFactories: false,
ignoreUnannotated: false,
includeIfNull: true,
realmCompatible: false,
);

JsonSerializable toJsonSerializable() => JsonSerializable(
Expand All @@ -133,6 +138,7 @@ class ClassConfig {
ignoreUnannotated: ignoreUnannotated,
explicitToJson: explicitToJson,
includeIfNull: includeIfNull,
realmCompatible: realmCompatible,
genericArgumentFactories: genericArgumentFactories,
fieldRename: fieldRename,
disallowUnrecognizedKeys: disallowUnrecognizedKeys,
Expand Down
2 changes: 2 additions & 0 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ JsonSerializable _valueForAnnotation(ConstantReader reader) => JsonSerializable(
reader.read('genericArgumentFactories').literalValue as bool?,
ignoreUnannotated: reader.read('ignoreUnannotated').literalValue as bool?,
includeIfNull: reader.read('includeIfNull').literalValue as bool?,
realmCompatible: reader.read('realmCompatible').literalValue as bool?,
);

/// Returns a [ClassConfig] with values from the [JsonSerializable]
Expand Down Expand Up @@ -117,6 +118,7 @@ ClassConfig mergeConfig(
config.genericArgumentFactories),
ignoreUnannotated: annotation.ignoreUnannotated ?? config.ignoreUnannotated,
includeIfNull: annotation.includeIfNull ?? config.includeIfNull,
realmCompatible: annotation.realmCompatible ?? config.realmCompatible,
ctorParamDefaults: paramDefaultValueMap,
converters: converters.isNull ? const [] : converters.listValue,
);
Expand Down
2 changes: 1 addition & 1 deletion json_serializable/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: json_serializable
version: 6.7.1
version: 6.7.2
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down