Skip to content
Open
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
10 changes: 10 additions & 0 deletions json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
@@ -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:
@@ -254,6 +261,7 @@ class JsonSerializable {
this.fieldRename,
this.ignoreUnannotated,
this.includeIfNull,
this.realmCompatible,
this.converters,
this.genericArgumentFactories,
this.createPerFieldToJson,
@@ -276,6 +284,7 @@ class JsonSerializable {
fieldRename: FieldRename.none,
ignoreUnannotated: false,
includeIfNull: true,
realmCompatible: false,
genericArgumentFactories: false,
);

@@ -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,
);
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`.
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
@@ -66,6 +66,7 @@ mixin DecodeHelper implements HelperCore {
.toList(),
unavailableReasons,
deserializeFun,
realmCompatible: config.realmCompatible,
);

final checks = _checkKeys(
@@ -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);
@@ -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(',
);
@@ -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(')');

7 changes: 6 additions & 1 deletion json_serializable/lib/src/helper_core.dart
Original file line number Diff line number Diff line change
@@ -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;

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
@@ -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;

@@ -73,6 +74,7 @@ class ClassConfig {
required this.genericArgumentFactories,
required this.ignoreUnannotated,
required this.includeIfNull,
required this.realmCompatible,
this.converters = const [],
this.ctorParamDefaults = const {},
});
@@ -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,
@@ -120,6 +124,7 @@ class ClassConfig {
genericArgumentFactories: false,
ignoreUnannotated: false,
includeIfNull: true,
realmCompatible: false,
);

JsonSerializable toJsonSerializable() => JsonSerializable(
@@ -133,6 +138,7 @@ class ClassConfig {
ignoreUnannotated: ignoreUnannotated,
explicitToJson: explicitToJson,
includeIfNull: includeIfNull,
realmCompatible: realmCompatible,
genericArgumentFactories: genericArgumentFactories,
fieldRename: fieldRename,
disallowUnrecognizedKeys: disallowUnrecognizedKeys,
2 changes: 2 additions & 0 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -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]
@@ -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,
);
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.