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
3 changes: 2 additions & 1 deletion json_serializable/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 6.7.0-dev
## 6.7.0

- Support `Record` types.
- Require Dart 3.0
- Require `analyzer: ^5.12.0`

Expand Down
7 changes: 4 additions & 3 deletions json_serializable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ Out of the box, `json_serializable` supports many common types in the
[dart:core](https://api.dart.dev/stable/dart-core/dart-core-library.html)
library:
[`BigInt`], [`bool`], [`DateTime`], [`double`], [`Duration`], [`Enum`], [`int`],
[`Iterable`], [`List`], [`Map`], [`num`], [`Object`], [`Set`], [`String`],
[`Uri`]
[`Iterable`], [`List`], [`Map`], [`num`], [`Object`], [`Record`], [`Set`],
[`String`], [`Uri`]

The collection types –
[`Iterable`], [`List`], [`Map`], [`Set`]
[`Iterable`], [`List`], [`Map`], [`Record`], [`Set`]
– can contain values of all the above types.

For [`Map`], the key value must be one of
Expand Down Expand Up @@ -313,6 +313,7 @@ targets:
[`Map`]: https://api.dart.dev/stable/dart-core/Map-class.html
[`num`]: https://api.dart.dev/stable/dart-core/num-class.html
[`Object`]: https://api.dart.dev/stable/dart-core/Object-class.html
[`Record`]: https://api.dart.dev/stable/dart-core/Record-class.html
[`Set`]: https://api.dart.dev/stable/dart-core/Set-class.html
[`String`]: https://api.dart.dev/stable/dart-core/String-class.html
[`Uri`]: https://api.dart.dev/stable/dart-core/Uri-class.html
1 change: 1 addition & 0 deletions json_serializable/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ builders:
- .type_map.dart
- .type_num.dart
- .type_object.dart
- .type_record.dart
- .type_set.dart
- .type_string.dart
- .type_uri.dart
Expand Down
4 changes: 3 additions & 1 deletion json_serializable/lib/src/enum_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ Map<FieldElement, Object?>? _enumMap(
DartType targetType, {
bool nullWithNoAnnotation = false,
}) {
final annotation = _jsonEnumChecker.firstAnnotationOf(targetType.element!);
final targetTypeElement = targetType.element;
if (targetTypeElement == null) return null;
final annotation = _jsonEnumChecker.firstAnnotationOf(targetTypeElement);
final jsonEnum = _fromAnnotation(annotation);

final enumFields = iterateEnumFields(targetType);
Expand Down
3 changes: 2 additions & 1 deletion json_serializable/lib/src/helper_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ $converterOrKeyInstructions
} else if (field.type != error.type) {
message = '$message because of type `${typeToCode(error.type)}`';
} else {
final element = error.type.element?.name;
todo = '''
To support the type `${error.type.element!.name}` you can:
To support the type `${element ?? error.type}` you can:
$converterOrKeyInstructions''';
}

Expand Down
2 changes: 1 addition & 1 deletion json_serializable/lib/src/json_key_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ KeyConfig _from(FieldElement element, ClassConfig classAnnotation) {
// literal, which is NOT supported!
badType = 'Function';
} else if (!reader.isLiteral) {
badType = dartObject.type!.element!.name;
badType = dartObject.type!.element?.name;
}

if (badType != null) {
Expand Down
2 changes: 2 additions & 0 deletions json_serializable/lib/src/settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'type_helpers/iterable_helper.dart';
import 'type_helpers/json_converter_helper.dart';
import 'type_helpers/json_helper.dart';
import 'type_helpers/map_helper.dart';
import 'type_helpers/record_helper.dart';
import 'type_helpers/uri_helper.dart';
import 'type_helpers/value_helper.dart';

Expand All @@ -24,6 +25,7 @@ class Settings {
IterableHelper(),
MapHelper(),
EnumHelper(),
RecordHelper(),
ValueHelper(),
];

Expand Down
124 changes: 124 additions & 0 deletions json_serializable/lib/src/type_helpers/record_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import 'package:analyzer/dart/element/type.dart';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: copyright

import 'package:source_helper/source_helper.dart';

import '../type_helper.dart';
import '../utils.dart';

class RecordHelper extends TypeHelper<TypeHelperContextWithConfig> {
const RecordHelper();

@override
Object? deserialize(
DartType targetType,
String expression,
TypeHelperContextWithConfig context,
bool defaultProvided,
) {
if (targetType is! RecordType) return null;

final items = <Object>[];

const paramName = r'$jsonValue';

var index = 1;
for (var field in targetType.positionalFields) {
final indexer = escapeDartString('\$$index');
items.add(
context.deserialize(field.type, '$paramName[$indexer]')!,
);
index++;
}
for (var field in targetType.namedFields) {
final indexer = escapeDartString(field.name);
items.add(
'${field.name}:'
'${context.deserialize(field.type, '$paramName[$indexer]')!}',
);
}

if (items.isEmpty) {
return '()';
}

context.addMember(
_recordConvertImpl(
nullable: targetType.isNullableType, anyMap: context.config.anyMap),
);

final recordLiteral = '(${items.map((e) => '$e,').join()})';

final helperName = _recordConvertName(
nullable: targetType.isNullableType,
anyMap: context.config.anyMap,
);

return '''
$helperName(
$expression,
($paramName) => $recordLiteral,
)''';
}

@override
Object? serialize(
DartType targetType,
String expression,
TypeHelperContextWithConfig context,
) {
if (targetType is! RecordType) return null;

final maybeBang = targetType.isNullableType ? '!' : '';

final items = <Object>[];

var index = 1;
for (var field in targetType.positionalFields) {
final indexer = escapeDartString('\$$index');
items.add(
'$indexer:'
'${context.serialize(field.type, '$expression$maybeBang.\$$index')!}',
);
index++;
}
for (var field in targetType.namedFields) {
final indexer = escapeDartString(field.name);
items.add(
'$indexer:${context.serialize(
field.type,
'$expression$maybeBang.${field.name}',
)!}',
);
}

final mapValue = '{${items.map((e) => '$e,').join()}}';

return targetType.isNullableType
? ifNullOrElse(
expression,
'null',
mapValue,
)
: mapValue;
}
}

String _recordConvertName({required bool nullable, required bool anyMap}) =>
'_\$recordConvert${anyMap ? 'Any' : ''}${nullable ? 'Nullable' : ''}';

String _recordConvertImpl({required bool nullable, required bool anyMap}) {
final name = _recordConvertName(nullable: nullable, anyMap: anyMap);

var expression =
'convert(value as ${anyMap ? 'Map' : 'Map<String, dynamic>'})';
if (nullable) {
expression = ifNullOrElse('value', 'null', expression);
}

return '''
\$Rec${nullable ? '?' : ''} $name<\$Rec>(
Object? value,
\$Rec Function(Map) convert,
) =>
$expression;
''';
}
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.0-dev
version: 6.7.0
description: >-
Automatically generate code for converting to and from JSON by annotating
Dart classes.
Expand Down
2 changes: 2 additions & 0 deletions json_serializable/test/kitchen_sink/kitchen_sink.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ class KitchenSink implements k.KitchenSink {
_validatedPropertyNo42 = value;
}

k.RecordSample? recordField;

bool operator ==(Object other) => k.sinkEquals(this, other);

static Object? _trickyValueAccessor(Map json, String key) {
Expand Down
23 changes: 22 additions & 1 deletion json_serializable/test/kitchen_sink/kitchen_sink.g.dart

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

Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ class KitchenSink implements k.KitchenSink {
_validatedPropertyNo42 = value;
}

k.RecordSample? recordField;

bool operator ==(Object other) => k.sinkEquals(this, other);

static Object? _trickyValueAccessor(Map json, String key) {
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ class KitchenSink implements k.KitchenSink {
_validatedPropertyNo42 = value;
}

k.RecordSample? recordField;

bool operator ==(Object other) => k.sinkEquals(this, other);

static Object? _trickyValueAccessor(Map json, String key) {
Expand Down

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

Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class KitchenSink implements k.KitchenSink {
_validatedPropertyNo42 = value;
}

k.RecordSample? recordField;

bool operator ==(Object other) => k.sinkEquals(this, other);

static Object? _trickyValueAccessor(Map json, String key) {
Expand Down
Loading