// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file // 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. part of '_json_serializable_test_input.dart'; @ShouldGenerate(r''' JsonConverterNamedCtor _$JsonConverterNamedCtorFromJson( Map json) { return JsonConverterNamedCtor() ..value = json['value'] == null ? null : const _DurationMillisecondConverter.named() .fromJson(json['value'] as int) ..genericValue = json['genericValue'] == null ? null : _GenericConverter.named().fromJson(json['genericValue'] as int) ..keyAnnotationFirst = json['keyAnnotationFirst'] == null ? null : JsonConverterNamedCtor._fromJson(json['keyAnnotationFirst'] as int); } Map _$JsonConverterNamedCtorToJson( JsonConverterNamedCtor instance) => { 'value': instance.value == null ? null : const _DurationMillisecondConverter.named().toJson(instance.value), 'genericValue': instance.genericValue == null ? null : _GenericConverter.named().toJson(instance.genericValue), 'keyAnnotationFirst': instance.keyAnnotationFirst == null ? null : JsonConverterNamedCtor._toJson(instance.keyAnnotationFirst) }; ''') @JsonSerializable() @_DurationMillisecondConverter.named() @_GenericConverter.named() class JsonConverterNamedCtor { Duration value; E genericValue; // Field annotations have precedence over class annotations @JsonKey(fromJson: _fromJson, toJson: _toJson) Duration keyAnnotationFirst; static Duration _fromJson(int value) => null; static int _toJson(Duration object) => 42; } @ShouldGenerate(r''' JsonConvertOnField _$JsonConvertOnFieldFromJson( Map json) { return JsonConvertOnField() ..annotatedField = json['annotatedField'] == null ? null : const _DurationMillisecondConverter() .fromJson(json['annotatedField'] as int) ..annotatedWithNamedCtor = json['annotatedWithNamedCtor'] == null ? null : const _DurationMillisecondConverter.named() .fromJson(json['annotatedWithNamedCtor'] as int) ..classAnnotatedWithField = json['classAnnotatedWithField'] == null ? null : _durationConverter.fromJson(json['classAnnotatedWithField'] as int) ..genericValue = json['genericValue'] == null ? null : _GenericConverter().fromJson(json['genericValue'] as int); } Map _$JsonConvertOnFieldToJson( JsonConvertOnField instance) => { 'annotatedField': instance.annotatedField == null ? null : const _DurationMillisecondConverter() .toJson(instance.annotatedField), 'annotatedWithNamedCtor': instance.annotatedWithNamedCtor == null ? null : const _DurationMillisecondConverter.named() .toJson(instance.annotatedWithNamedCtor), 'classAnnotatedWithField': instance.classAnnotatedWithField == null ? null : _durationConverter.toJson(instance.classAnnotatedWithField), 'genericValue': instance.genericValue == null ? null : _GenericConverter().toJson(instance.genericValue) }; ''') @JsonSerializable() @_durationConverter class JsonConvertOnField { @_DurationMillisecondConverter() Duration annotatedField; @_DurationMillisecondConverter.named() Duration annotatedWithNamedCtor; Duration classAnnotatedWithField; @_GenericConverter() E genericValue; } class _GenericConverter implements JsonConverter { const _GenericConverter(); const _GenericConverter.named(); @override T fromJson(int json) => null; @override int toJson(T object) => 0; } @ShouldThrow('`JsonConverter` implementations can have no more than one type ' 'argument. `_BadConverter` has 2.') @JsonSerializable() @_BadConverter() class JsonConverterWithBadTypeArg { T value; } class _BadConverter implements JsonConverter { const _BadConverter(); @override S fromJson(int json) => null; @override int toJson(S object) => 0; } @ShouldThrow('Found more than one matching converter for `Duration`.') @JsonSerializable() @_durationConverter @_DurationMillisecondConverter() class JsonConverterDuplicateAnnotations { Duration value; } const _durationConverter = _DurationMillisecondConverter(); class _DurationMillisecondConverter implements JsonConverter { const _DurationMillisecondConverter(); const _DurationMillisecondConverter.named(); @override Duration fromJson(int json) => json == null ? null : Duration(milliseconds: json); @override int toJson(Duration object) => object?.inMilliseconds; } @ShouldThrow('Generators with constructor arguments are not supported.') @JsonSerializable() @_ConverterWithCtorParams(42) class JsonConverterCtorParams { Duration value; } class _ConverterWithCtorParams implements JsonConverter { final int param; const _ConverterWithCtorParams(this.param); @override Duration fromJson(int json) => null; @override int toJson(Duration object) => 0; }