Skip to content

Pass field metadata to type helpers #71

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

Closed
wants to merge 1 commit into from
Closed
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
57 changes: 34 additions & 23 deletions json_serializable/lib/src/json_serializable_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -417,52 +417,61 @@ void $toJsonMapHelperName(String key, dynamic value) {
String _serializeField(FieldElement field, bool classIncludeNullable,
{String accessOverride}) {
accessOverride ??= field.name;

_TypeHelperContext context = _getHelperContext(field);

try {
return _serialize(
return context.serialize(
field.type, accessOverride, _nullable(field, classIncludeNullable));
} on UnsupportedTypeError catch (e) {
throw _createInvalidGenerationError('toJson', field, e);
}
}

/// [expression] may be just the name of the field or it may an expression
/// representing the serialization of a value.
String _serialize(DartType targetType, String expression, bool nullable) =>
_allHelpers
.map((h) =>
h.serialize(targetType, expression, nullable, _helperContext))
.firstWhere((r) => r != null,
orElse: () => throw new UnsupportedTypeError(
targetType, expression, _notSupportedWithTypeHelpersMsg));

String _deserializeForField(FieldElement field, bool classSupportNullable,
{ParameterElement ctorParam}) {
var jsonKey = _safeNameAccess(field);

var targetType = ctorParam?.type ?? field.type;

_TypeHelperContext context = _getHelperContext(field);

try {
return _deserialize(
return context.deserialize(
targetType, 'json[$jsonKey]', _nullable(field, classSupportNullable));
} on UnsupportedTypeError catch (e) {
throw _createInvalidGenerationError('fromJson', field, e);
}
}

String _deserialize(DartType targetType, String expression, bool nullable) =>
_allHelpers
.map((th) =>
th.deserialize(targetType, expression, nullable, _helperContext))
.firstWhere((r) => r != null,
orElse: () => throw new UnsupportedTypeError(
targetType, expression, _notSupportedWithTypeHelpersMsg));
_TypeHelperContext _getHelperContext(FieldElement field) {
_TypeHelperContext helperContext;

/// [expression] may be just the name of the field or it may an expression
/// representing the serialization of a value.
String serialize(DartType targetType, String expression, bool nullable) =>
_allHelpers
.map((h) =>
h.serialize(targetType, expression, nullable, helperContext))
.firstWhere((r) => r != null,
orElse: () => throw new UnsupportedTypeError(
targetType, expression, _notSupportedWithTypeHelpersMsg));

String deserialize(DartType targetType, String expression, bool nullable) =>
_allHelpers
.map((th) =>
th.deserialize(targetType, expression, nullable, helperContext))
.firstWhere((r) => r != null,
orElse: () => throw new UnsupportedTypeError(
targetType, expression, _notSupportedWithTypeHelpersMsg));

helperContext = new _TypeHelperContext(serialize, deserialize, useWrappers, field.metadata);
return helperContext;
}

_TypeHelperContext get _helperContext => _typeHelperContextExpando[this] ??=
new _TypeHelperContext(_serialize, _deserialize, useWrappers);
}

final _typeHelperContextExpando = new Expando<_TypeHelperContext>();

typedef String _TypeHelperGenerator(
DartType fieldType, String expression, bool nullable);

Expand All @@ -472,7 +481,9 @@ class _TypeHelperContext implements SerializeContext, DeserializeContext {
@override
final bool useWrappers;

_TypeHelperContext(this._serialize, this._deserialize, this.useWrappers);
List<ElementAnnotation> metadata;

_TypeHelperContext(this._serialize, this._deserialize, this.useWrappers, this.metadata);

@override
String serialize(DartType fieldType, String expression, bool nullable) =>
Expand Down
3 changes: 3 additions & 0 deletions json_serializable/lib/src/type_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:source_gen/source_gen.dart' show TypeChecker;

Expand All @@ -18,10 +19,12 @@ List<DartType> typeArgumentsOf(DartType type, TypeChecker checker) {
abstract class SerializeContext {
bool get useWrappers;
String serialize(DartType fieldType, String expression, bool nullable);
List<ElementAnnotation> metadata;
}

abstract class DeserializeContext {
String deserialize(DartType fieldType, String expression, bool nullable);
List<ElementAnnotation> metadata;
}

abstract class TypeHelper {
Expand Down