diff --git a/json_annotation/lib/src/json_serializable.dart b/json_annotation/lib/src/json_serializable.dart index 71a6e4550..7776227e0 100644 --- a/json_annotation/lib/src/json_serializable.dart +++ b/json_annotation/lib/src/json_serializable.dart @@ -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, ); diff --git a/json_annotation/lib/src/json_serializable.g.dart b/json_annotation/lib/src/json_serializable.g.dart index 9d02a187c..7295fbfa3 100644 --- a/json_annotation/lib/src/json_serializable.g.dart +++ b/json_annotation/lib/src/json_serializable.g.dart @@ -26,7 +26,8 @@ JsonSerializable _$JsonSerializableFromJson(Map json) => 'field_rename', 'generic_argument_factories', 'ignore_unannotated', - 'include_if_null' + 'include_if_null', + 'realm_compatible' ], ); final val = JsonSerializable( @@ -46,6 +47,8 @@ JsonSerializable _$JsonSerializableFromJson(Map json) => ignoreUnannotated: $checkedConvert('ignore_unannotated', (v) => v as bool?), includeIfNull: $checkedConvert('include_if_null', (v) => v as bool?), + realmCompatible: + $checkedConvert('realm_compatible', (v) => v as bool?), genericArgumentFactories: $checkedConvert('generic_argument_factories', (v) => v as bool?), createPerFieldToJson: @@ -63,6 +66,7 @@ JsonSerializable _$JsonSerializableFromJson(Map json) => 'fieldRename': 'field_rename', 'ignoreUnannotated': 'ignore_unannotated', 'includeIfNull': 'include_if_null', + 'realmCompatible': 'realm_compatible', 'genericArgumentFactories': 'generic_argument_factories', 'createPerFieldToJson': 'create_per_field_to_json' }, @@ -83,6 +87,7 @@ Map _$JsonSerializableToJson(JsonSerializable instance) => 'generic_argument_factories': instance.genericArgumentFactories, 'ignore_unannotated': instance.ignoreUnannotated, 'include_if_null': instance.includeIfNull, + 'realm_compatible': instance.realmCompatible, }; const _$FieldRenameEnumMap = { diff --git a/json_serializable/CHANGELOG.md b/json_serializable/CHANGELOG.md index 49a067ba7..dcc386ba6 100644 --- a/json_serializable/CHANGELOG.md +++ b/json_serializable/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.7.2 + +- Support generated model classes. + ## 6.7.1 - Support the latest `package:analyzer`. diff --git a/json_serializable/build/test_cache/build/c075001b96339384a97db4862b8ab8db.cache.dill.track.dill b/json_serializable/build/test_cache/build/c075001b96339384a97db4862b8ab8db.cache.dill.track.dill new file mode 100644 index 000000000..ff33f82b8 Binary files /dev/null and b/json_serializable/build/test_cache/build/c075001b96339384a97db4862b8ab8db.cache.dill.track.dill differ diff --git a/json_serializable/build/unit_test_assets/AssetManifest.bin b/json_serializable/build/unit_test_assets/AssetManifest.bin new file mode 100644 index 000000000..86d111f09 Binary files /dev/null and b/json_serializable/build/unit_test_assets/AssetManifest.bin differ diff --git a/json_serializable/build/unit_test_assets/AssetManifest.json b/json_serializable/build/unit_test_assets/AssetManifest.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/json_serializable/build/unit_test_assets/AssetManifest.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/json_serializable/build/unit_test_assets/FontManifest.json b/json_serializable/build/unit_test_assets/FontManifest.json new file mode 100644 index 000000000..0637a088a --- /dev/null +++ b/json_serializable/build/unit_test_assets/FontManifest.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/json_serializable/build/unit_test_assets/NOTICES.Z b/json_serializable/build/unit_test_assets/NOTICES.Z new file mode 100644 index 000000000..fe979f33e Binary files /dev/null and b/json_serializable/build/unit_test_assets/NOTICES.Z differ diff --git a/json_serializable/build/unit_test_assets/shaders/ink_sparkle.frag b/json_serializable/build/unit_test_assets/shaders/ink_sparkle.frag new file mode 100644 index 000000000..73f984f02 Binary files /dev/null and b/json_serializable/build/unit_test_assets/shaders/ink_sparkle.frag differ diff --git a/json_serializable/lib/src/decode_helper.dart b/json_serializable/lib/src/decode_helper.dart index a4a76d1ff..930f18d17 100644 --- a/json_serializable/lib/src/decode_helper.dart +++ b/json_serializable/lib/src/decode_helper.dart @@ -66,6 +66,7 @@ mixin DecodeHelper implements HelperCore { .toList(), unavailableReasons, deserializeFun, + realmCompatible: config.realmCompatible, ); final checks = _checkKeys( @@ -266,8 +267,9 @@ _ConstructorData _writeConstructorInvocation( Iterable writableFields, Map 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 + ? {} + : 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(')'); diff --git a/json_serializable/lib/src/helper_core.dart b/json_serializable/lib/src/helper_core.dart index 5a8248c66..0cea1a123 100644 --- a/json_serializable/lib/src/helper_core.dart +++ b/json_serializable/lib/src/helper_core.dart @@ -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; diff --git a/json_serializable/lib/src/type_helpers/config_types.dart b/json_serializable/lib/src/type_helpers/config_types.dart index c4bdc260a..d87015c8d 100644 --- a/json_serializable/lib/src/type_helpers/config_types.dart +++ b/json_serializable/lib/src/type_helpers/config_types.dart @@ -56,6 +56,7 @@ class ClassConfig { final bool genericArgumentFactories; final bool ignoreUnannotated; final bool includeIfNull; + final bool realmCompatible; final Map ctorParamDefaults; final List 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, diff --git a/json_serializable/lib/src/utils.dart b/json_serializable/lib/src/utils.dart index 941b7492a..76a4e095d 100644 --- a/json_serializable/lib/src/utils.dart +++ b/json_serializable/lib/src/utils.dart @@ -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, ); diff --git a/json_serializable/pubspec.yaml b/json_serializable/pubspec.yaml index cfb3dd8a8..e12a8f9e8 100644 --- a/json_serializable/pubspec.yaml +++ b/json_serializable/pubspec.yaml @@ -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.