Skip to content
Merged
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 json_annotation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- **BREAKING** Removed `JsonSerializable.useWrappers` and associated
`$`-prefixed helpers.
- **BREAKING** Removed `JsonSerializable.generateToJsonFunction`.

## 2.3.0

Expand Down
51 changes: 2 additions & 49 deletions json_annotation/lib/src/json_serializable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,15 @@ class JsonSerializable {
/// ```
final bool createFactory;

/// If `true` (the default), code for encoding JSON is generated for this
/// class.
///
/// If `json_serializable` is configured with
/// `generate_to_json_function: true` (the default), a top-level function is
/// created that you can reference from your class.
/// If `true` (the default), A top-level function is created that you can
/// reference from your class.
///
/// ```dart
/// @JsonSerializable()
/// class Example {
/// Map<String, dynamic> toJson() => _$ExampleToJson(this);
/// }
/// ```
///
/// If `json_serializable` is configured with
/// `generate_to_json_function: false`, a private `_$ClassNameMixin` class is
/// created in the generated part file which contains a `toJson` method.
///
/// Mix in this class to the source class:
///
/// ```dart
/// @JsonSerializable()
/// class Example extends Object with _$ExampleSerializerMixin {
/// // ...
/// }
/// ```
final bool createToJson;

/// If `false` (the default), then the generated `FromJson` function will
Expand Down Expand Up @@ -144,32 +127,6 @@ class JsonSerializable {
/// fields annotated with [JsonKey].
final FieldRename fieldRename;

/// Controls how `toJson` functionality is generated for all types processed
/// by this generator.
///
/// If `true` (the default), a top-level function is created.
///
/// ```dart
/// @JsonSerializable()
/// class Example {
/// // ...
/// Map<String, dynamic> toJson() => _$ExampleToJson(this);
/// }
/// ```
///
/// If `false`, a private `_$ClassNameSerializerMixin` class is
/// created in the generated part file which contains a `toJson` method.
///
/// Mix in this class to the source class:
///
/// ```dart
/// @JsonSerializable()
/// class Example extends Object with _$ExampleSerializerMixin {
/// // ...
/// }
/// ```
final bool generateToJsonFunction;

/// Whether the generator should include fields with `null` values in the
/// serialized output.
///
Expand Down Expand Up @@ -200,7 +157,6 @@ class JsonSerializable {
this.encodeEmptyCollection,
this.explicitToJson,
this.fieldRename,
this.generateToJsonFunction,
this.includeIfNull,
this.nullable,
});
Expand All @@ -219,7 +175,6 @@ class JsonSerializable {
encodeEmptyCollection: true,
explicitToJson: false,
fieldRename: FieldRename.none,
generateToJsonFunction: true,
includeIfNull: true,
nullable: true,
);
Expand All @@ -240,8 +195,6 @@ class JsonSerializable {
encodeEmptyCollection ?? defaults.encodeEmptyCollection,
explicitToJson: explicitToJson ?? defaults.explicitToJson,
fieldRename: fieldRename ?? defaults.fieldRename,
generateToJsonFunction:
generateToJsonFunction ?? defaults.generateToJsonFunction,
includeIfNull: includeIfNull ?? defaults.includeIfNull,
nullable: nullable ?? defaults.nullable);

Expand Down
5 changes: 0 additions & 5 deletions 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.

1 change: 1 addition & 0 deletions json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 3.0.0

- **BREAKING** Removed support for `JsonSerializable.useWrappers`.
- **BREAKING** Removed support for `JsonSerializable.generateToJsonFunction`.

## 2.3.0

Expand Down
3 changes: 0 additions & 3 deletions json_serializable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ is generated:
| disallow_unrecognized_keys | [JsonSerializable.disallowUnrecognizedKeys] | |
| explicit_to_json | [JsonSerializable.explicitToJson] | |
| field_rename | [JsonSerializable.fieldRename] | |
| generate_to_json_function | [JsonSerializable.generateToJsonFunction] | |
| encode_empty_collection | [JsonSerializable.encodeEmptyCollection] | [JsonKey.encodeEmptyCollection] |
| include_if_null | [JsonSerializable.includeIfNull] | [JsonKey.includeIfNull] |
| nullable | [JsonSerializable.nullable] | [JsonKey.nullable] |
Expand All @@ -97,7 +96,6 @@ is generated:
[JsonSerializable.disallowUnrecognizedKeys]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/disallowUnrecognizedKeys.html
[JsonSerializable.explicitToJson]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/explicitToJson.html
[JsonSerializable.fieldRename]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/fieldRename.html
[JsonSerializable.generateToJsonFunction]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/generateToJsonFunction.html
[JsonSerializable.encodeEmptyCollection]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/encodeEmptyCollection.html
[JsonKey.encodeEmptyCollection]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/encodeEmptyCollection.html
[JsonSerializable.includeIfNull]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/includeIfNull.html
Expand Down Expand Up @@ -146,7 +144,6 @@ targets:
encode_empty_collection: true
explicit_to_json: false
field_rename: none
generate_to_json_function: true
include_if_null: true
nullable: true
```
Expand Down
2 changes: 0 additions & 2 deletions json_serializable/doc/doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
| disallow_unrecognized_keys | [JsonSerializable.disallowUnrecognizedKeys] | |
| explicit_to_json | [JsonSerializable.explicitToJson] | |
| field_rename | [JsonSerializable.fieldRename] | |
| generate_to_json_function | [JsonSerializable.generateToJsonFunction] | |
| encode_empty_collection | [JsonSerializable.encodeEmptyCollection] | [JsonKey.encodeEmptyCollection] |
| include_if_null | [JsonSerializable.includeIfNull] | [JsonKey.includeIfNull] |
| nullable | [JsonSerializable.nullable] | [JsonKey.nullable] |
Expand All @@ -26,7 +25,6 @@
[JsonSerializable.disallowUnrecognizedKeys]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/disallowUnrecognizedKeys.html
[JsonSerializable.explicitToJson]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/explicitToJson.html
[JsonSerializable.fieldRename]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/fieldRename.html
[JsonSerializable.generateToJsonFunction]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/generateToJsonFunction.html
[JsonSerializable.encodeEmptyCollection]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/encodeEmptyCollection.html
[JsonKey.encodeEmptyCollection]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonKey/encodeEmptyCollection.html
[JsonSerializable.includeIfNull]: https://pub.dev/documentation/json_annotation/latest/json_annotation/JsonSerializable/includeIfNull.html
Expand Down
42 changes: 4 additions & 38 deletions json_serializable/lib/src/encoder_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,16 @@ import 'type_helper.dart';
import 'type_helpers/json_converter_helper.dart';

abstract class EncodeHelper implements HelperCore {
String _fieldAccess(FieldElement field) {
var fieldAccess = field.name;
if (config.generateToJsonFunction) {
fieldAccess = '$_toJsonParamName.$fieldAccess';
}
return fieldAccess;
}

String _mixinClassName(bool withConstraints) =>
'${prefix}SerializerMixin${genericClassArgumentsImpl(withConstraints)}';
String _fieldAccess(FieldElement field) => '$_toJsonParamName.${field.name}';

Iterable<String> createToJson(Set<FieldElement> accessibleFields) sync* {
assert(config.createToJson);

final buffer = StringBuffer();

if (config.generateToJsonFunction) {
final functionName = '${prefix}ToJson${genericClassArgumentsImpl(true)}';
buffer.write('Map<String, dynamic> $functionName'
'($targetClassReference $_toJsonParamName) ');
} else {
//
// Generate the mixin class
//
buffer.writeln('abstract class ${_mixinClassName(true)} {');

// write copies of the fields - this allows the toJson method to access
// the fields of the target class
for (final field in accessibleFields) {
//TODO - handle aliased imports
buffer.writeln(' ${field.type} get ${field.name};');
}

buffer.write(' Map<String, dynamic> toJson() ');
}
final functionName = '${prefix}ToJson${genericClassArgumentsImpl(true)}';
buffer.write('Map<String, dynamic> $functionName'
'($targetClassReference $_toJsonParamName) ');

final writeNaive = accessibleFields.every(_writeJsonValueNaive);

Expand All @@ -57,11 +32,6 @@ abstract class EncodeHelper implements HelperCore {
_writeToJsonWithNullChecks(buffer, accessibleFields);
}

if (!config.generateToJsonFunction) {
// end of the mixin class
buffer.writeln('}');
}

yield buffer.toString();
}

Expand All @@ -82,8 +52,6 @@ abstract class EncodeHelper implements HelperCore {
buffer.writeln('};');
}

/// Name of the parameter used when generating top-level `toJson` functions
/// if [JsonSerializable.generateToJsonFunction] is `true`.
static const _toJsonParamName = 'instance';

void _writeToJsonWithNullChecks(
Expand All @@ -106,8 +74,6 @@ abstract class EncodeHelper implements HelperCore {
// access with `this.`.
if (safeFieldAccess == generatedLocalVarName ||
safeFieldAccess == toJsonMapHelperName) {
assert(!config.generateToJsonFunction,
'This code path should only be hit during the mixin codepath.');
safeFieldAccess = 'this.$safeFieldAccess';
}

Expand Down
4 changes: 0 additions & 4 deletions json_serializable/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ JsonSerializable _valueForAnnotation(ConstantReader reader) => JsonSerializable(
reader.read('encodeEmptyCollection').literalValue as bool,
explicitToJson: reader.read('explicitToJson').literalValue as bool,
fieldRename: _fromDartObject(reader.read('fieldRename')),
generateToJsonFunction:
reader.read('generateToJsonFunction').literalValue as bool,
includeIfNull: reader.read('includeIfNull').literalValue as bool,
nullable: reader.read('nullable').literalValue as bool,
);
Expand All @@ -106,8 +104,6 @@ JsonSerializable mergeConfig(JsonSerializable config, ConstantReader reader) {
annotation.encodeEmptyCollection ?? config.encodeEmptyCollection,
explicitToJson: annotation.explicitToJson ?? config.explicitToJson,
fieldRename: annotation.fieldRename ?? config.fieldRename,
generateToJsonFunction:
annotation.generateToJsonFunction ?? config.generateToJsonFunction,
includeIfNull: annotation.includeIfNull ?? config.includeIfNull,
nullable: annotation.nullable ?? config.nullable,
);
Expand Down
1 change: 0 additions & 1 deletion json_serializable/test/config_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ const _invalidConfig = {
'encode_empty_collection': 42,
'explicit_to_json': 42,
'field_rename': 42,
'generate_to_json_function': 42,
'include_if_null': 42,
'nullable': 42,
};
7 changes: 0 additions & 7 deletions json_serializable/test/json_serializable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// BSD-style license that can be found in the LICENSE file.

@TestOn('vm')
import 'package:json_annotation/json_annotation.dart';
import 'package:json_serializable/json_serializable.dart';
import 'package:path/path.dart' as p;
import 'package:source_gen_test/source_gen_test.dart';
Expand All @@ -19,10 +18,6 @@ void main() async {
testAnnotatedElements(
reader,
const JsonSerializableGenerator(),
additionalGenerators: const {
'mixin': JsonSerializableGenerator(
config: JsonSerializable(generateToJsonFunction: false)),
},
expectedAnnotatedTests: _expectedAnnotatedTests,
);
}
Expand Down Expand Up @@ -58,10 +53,8 @@ const _expectedAnnotatedTests = [
'FinalFieldsNotSetInCtor',
'FromDynamicCollection',
'GeneralTestClass1',
'GeneralTestClass1',
'GeneralTestClass2',
'GenericClass',
'GenericClass',
'IgnoredFieldClass',
'IgnoredFieldCtorClass',
'IncludeIfNullDisallowNullClass',
Expand Down
7 changes: 4 additions & 3 deletions json_serializable/test/kitchen_sink/simple_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import 'package:json_annotation/json_annotation.dart';

part 'simple_object.g.dart';

@JsonSerializable(anyMap: true, generateToJsonFunction: false)
class SimpleObject extends Object with _$SimpleObjectSerializerMixin {
@override
@JsonSerializable(anyMap: true)
class SimpleObject {
final int value;

SimpleObject(this.value);

factory SimpleObject.fromJson(Map json) => _$SimpleObjectFromJson(json);

Map<String, dynamic> toJson() => _$SimpleObjectToJson(this);
}
6 changes: 2 additions & 4 deletions json_serializable/test/kitchen_sink/simple_object.g.dart

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

9 changes: 4 additions & 5 deletions json_serializable/test/kitchen_sink/strict_keys_object.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import 'package:json_annotation/json_annotation.dart';

part 'strict_keys_object.g.dart';

@JsonSerializable(
disallowUnrecognizedKeys: true, anyMap: true, generateToJsonFunction: false)
class StrictKeysObject extends Object with _$StrictKeysObjectSerializerMixin {
@override
@JsonSerializable(disallowUnrecognizedKeys: true, anyMap: true)
class StrictKeysObject {
@JsonKey(required: true)
final int value;

@override
@JsonKey(name: 'custom_field', required: true)
final String customField;

StrictKeysObject(this.value, this.customField);

factory StrictKeysObject.fromJson(Map json) =>
_$StrictKeysObjectFromJson(json);

Map<String, dynamic> toJson() => _$StrictKeysObjectToJson(this);
}
11 changes: 5 additions & 6 deletions json_serializable/test/kitchen_sink/strict_keys_object.g.dart

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

1 change: 0 additions & 1 deletion json_serializable/test/shared_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ final generatorConfigNonDefaultJson =
encodeEmptyCollection: false,
explicitToJson: true,
fieldRename: FieldRename.kebab,
generateToJsonFunction: false,
includeIfNull: false,
nullable: false,
).toJson());
Loading