-
Notifications
You must be signed in to change notification settings - Fork 418
Add support for generic classes #169
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// 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. | ||
|
||
// ignore_for_file: annotate_overrides | ||
|
||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'generic_class.g.dart'; | ||
|
||
@JsonSerializable() | ||
class GenericClass<T extends num, S> extends Object | ||
with _$GenericClassSerializerMixin<T, S> { | ||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup. Will document as such. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
Object fieldObject; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
dynamic fieldDynamic; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
int fieldInt; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
T fieldT; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
S fieldS; | ||
|
||
GenericClass(); | ||
|
||
factory GenericClass.fromJson(Map<String, dynamic> json) => | ||
_$GenericClassFromJson<T, S>(json); | ||
} | ||
|
||
T _dataFromJson<T, S, U>(Map<String, dynamic> input, [S other1, U, other2]) => | ||
input['value'] as T; | ||
|
||
Map<String, dynamic> _dataToJson<T, S, U>(T input, [S other1, U, other2]) => | ||
{'value': input}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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. | ||
|
||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
|
||
part of 'generic_class.dart'; | ||
|
||
// ************************************************************************** | ||
// Generator: JsonSerializableGenerator | ||
// ************************************************************************** | ||
|
||
GenericClass<T, S> _$GenericClassFromJson<T extends num, S>( | ||
Map<String, dynamic> json) => | ||
new GenericClass<T, S>() | ||
..fieldObject = json['fieldObject'] == null | ||
? null | ||
: _dataFromJson(json['fieldObject'] as Map<String, dynamic>) | ||
..fieldDynamic = json['fieldDynamic'] == null | ||
? null | ||
: _dataFromJson(json['fieldDynamic'] as Map<String, dynamic>) | ||
..fieldInt = json['fieldInt'] == null | ||
? null | ||
: _dataFromJson(json['fieldInt'] as Map<String, dynamic>) | ||
..fieldT = json['fieldT'] == null | ||
? null | ||
: _dataFromJson(json['fieldT'] as Map<String, dynamic>) | ||
..fieldS = json['fieldS'] == null | ||
? null | ||
: _dataFromJson(json['fieldS'] as Map<String, dynamic>); | ||
|
||
abstract class _$GenericClassSerializerMixin<T extends num, S> { | ||
Object get fieldObject; | ||
dynamic get fieldDynamic; | ||
int get fieldInt; | ||
T get fieldT; | ||
S get fieldS; | ||
Map<String, dynamic> toJson() => <String, dynamic>{ | ||
'fieldObject': fieldObject == null ? null : _dataToJson(fieldObject), | ||
'fieldDynamic': fieldDynamic == null ? null : _dataToJson(fieldDynamic), | ||
'fieldInt': fieldInt == null ? null : _dataToJson(fieldInt), | ||
'fieldT': fieldT == null ? null : _dataToJson(fieldT), | ||
'fieldS': fieldS == null ? null : _dataToJson(fieldS) | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 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. | ||
|
||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
|
||
// ************************************************************************** | ||
// Generator: _WrappedGenerator | ||
// ************************************************************************** | ||
|
||
// ignore_for_file: annotate_overrides | ||
|
||
import 'package:json_annotation/json_annotation.dart'; | ||
|
||
part 'generic_class.wrapped.g.dart'; | ||
|
||
@JsonSerializable() | ||
class GenericClass<T extends num, S> extends Object | ||
with _$GenericClassSerializerMixin<T, S> { | ||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
Object fieldObject; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
dynamic fieldDynamic; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
int fieldInt; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
T fieldT; | ||
|
||
@JsonKey(fromJson: _dataFromJson, toJson: _dataToJson) | ||
S fieldS; | ||
|
||
GenericClass(); | ||
|
||
factory GenericClass.fromJson(Map<String, dynamic> json) => | ||
_$GenericClassFromJson<T, S>(json); | ||
} | ||
|
||
T _dataFromJson<T, S, U>(Map<String, dynamic> input, [S other1, U, other2]) => | ||
input['value'] as T; | ||
|
||
Map<String, dynamic> _dataToJson<T, S, U>(T input, [S other1, U, other2]) => | ||
{'value': input}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// 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. | ||
|
||
// GENERATED CODE - DO NOT MODIFY BY HAND | ||
|
||
part of 'generic_class.wrapped.dart'; | ||
|
||
// ************************************************************************** | ||
// Generator: JsonSerializableGenerator | ||
// ************************************************************************** | ||
|
||
GenericClass<T, S> _$GenericClassFromJson<T extends num, S>( | ||
Map<String, dynamic> json) => | ||
new GenericClass<T, S>() | ||
..fieldObject = json['fieldObject'] == null | ||
? null | ||
: _dataFromJson(json['fieldObject'] as Map<String, dynamic>) | ||
..fieldDynamic = json['fieldDynamic'] == null | ||
? null | ||
: _dataFromJson(json['fieldDynamic'] as Map<String, dynamic>) | ||
..fieldInt = json['fieldInt'] == null | ||
? null | ||
: _dataFromJson(json['fieldInt'] as Map<String, dynamic>) | ||
..fieldT = json['fieldT'] == null | ||
? null | ||
: _dataFromJson(json['fieldT'] as Map<String, dynamic>) | ||
..fieldS = json['fieldS'] == null | ||
? null | ||
: _dataFromJson(json['fieldS'] as Map<String, dynamic>); | ||
|
||
abstract class _$GenericClassSerializerMixin<T extends num, S> { | ||
Object get fieldObject; | ||
dynamic get fieldDynamic; | ||
int get fieldInt; | ||
T get fieldT; | ||
S get fieldS; | ||
Map<String, dynamic> toJson() => new _$GenericClassJsonMapWrapper<T, S>(this); | ||
} | ||
|
||
class _$GenericClassJsonMapWrapper<T extends num, S> extends $JsonMapWrapper { | ||
final _$GenericClassSerializerMixin<T, S> _v; | ||
_$GenericClassJsonMapWrapper(this._v); | ||
|
||
@override | ||
Iterable<String> get keys => | ||
const ['fieldObject', 'fieldDynamic', 'fieldInt', 'fieldT', 'fieldS']; | ||
|
||
@override | ||
dynamic operator [](Object key) { | ||
if (key is String) { | ||
switch (key) { | ||
case 'fieldObject': | ||
return _v.fieldObject == null ? null : _dataToJson(_v.fieldObject); | ||
case 'fieldDynamic': | ||
return _v.fieldDynamic == null ? null : _dataToJson(_v.fieldDynamic); | ||
case 'fieldInt': | ||
return _v.fieldInt == null ? null : _dataToJson(_v.fieldInt); | ||
case 'fieldT': | ||
return _v.fieldT == null ? null : _dataToJson(_v.fieldT); | ||
case 'fieldS': | ||
return _v.fieldS == null ? null : _dataToJson(_v.fieldS); | ||
} | ||
} | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with
writeln
you're going to get an extra newline and this already ends in a newline. Do we need to?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly, dartfmt handles most of this. Not going to modify this in this CL