Skip to content

Update example for latest release #150

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

Merged
merged 1 commit into from
May 10, 2018
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
4 changes: 2 additions & 2 deletions example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ dependencies to your `pubspec.yaml`.

```yaml
dependencies:
json_annotation: ^0.2.3
json_annotation: ^0.2.4

dev_dependencies:
build_runner: ^0.8.0
json_serializable: ^0.5.0
json_serializable: ^0.5.2
```

Annotate your code with classes defined in
Expand Down
19 changes: 18 additions & 1 deletion example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,28 @@ class Order extends Object with _$OrderSerializerMixin {
bool isRushed;
Item item;

Order();
@JsonKey(
name: 'prep-time',
fromJson: _durationFromMillseconds,
toJson: _durationToMilliseconds)
Duration prepTime;

@JsonKey(fromJson: _dateTimeFromEpochUs, toJson: _dateTimeToEpochUs)
final DateTime date;

Order(this.date);

factory Order.fromJson(Map<String, dynamic> json) => _$OrderFromJson(json);
}

Duration _durationFromMillseconds(int milliseconds) =>
new Duration(milliseconds: milliseconds);
int _durationToMilliseconds(Duration duration) => duration.inMilliseconds;

DateTime _dateTimeFromEpochUs(int us) =>
new DateTime.fromMicrosecondsSinceEpoch(us);
int _dateTimeToEpochUs(DateTime dateTime) => dateTime.microsecondsSinceEpoch;

@JsonSerializable(createToJson: false)
class Item {
int count;
Expand Down
13 changes: 11 additions & 2 deletions example/lib/example.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,25 @@ abstract class _$PersonSerializerMixin {
}
}

Order _$OrderFromJson(Map<String, dynamic> json) => new Order()
Order _$OrderFromJson(Map<String, dynamic> json) => new Order(
json['date'] == null ? null : _dateTimeFromEpochUs(json['date'] as int))
..count = json['count'] as int
..itemNumber = json['itemNumber'] as int
..isRushed = json['isRushed'] as bool
..item = json['item'] == null
? null
: new Item.fromJson(json['item'] as Map<String, dynamic>);
: new Item.fromJson(json['item'] as Map<String, dynamic>)
..prepTime = json['prep-time'] == null
? null
: _durationFromMillseconds(json['prep-time'] as int);

abstract class _$OrderSerializerMixin {
int get count;
int get itemNumber;
bool get isRushed;
Item get item;
Duration get prepTime;
DateTime get date;
Map<String, dynamic> toJson() {
var val = <String, dynamic>{};

Expand All @@ -75,6 +81,9 @@ abstract class _$OrderSerializerMixin {
writeNotNull('itemNumber', itemNumber);
writeNotNull('isRushed', isRushed);
writeNotNull('item', item);
writeNotNull('prep-time',
prepTime == null ? null : _durationToMilliseconds(prepTime));
writeNotNull('date', date == null ? null : _dateTimeToEpochUs(date));
return val;
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ environment:
sdk: '>=2.0.0-dev.32 <2.0.0'

dependencies:
json_annotation: ^0.2.3
json_annotation: ^0.2.4

dev_dependencies:
build_runner: ^0.8.0
json_serializable: ^0.5.0
json_serializable: ^0.5.2

# Used by tests. Not required to use `json_serializable`.
path: ^1.5.1
Expand Down
4 changes: 3 additions & 1 deletion example/test/example_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import 'package:test/test.dart';

void main() {
test('JsonSerializable', () {
final person = new Person('Inigo', 'Montoya', new DateTime(1560, 5, 5));
final person = new Person('Inigo', 'Montoya', new DateTime(1560, 5, 5))
..orders = [new Order(new DateTime.now())];

final personJson = _encode(person);

Expand All @@ -19,6 +20,7 @@ void main() {
expect(person.firstName, person2.firstName);
expect(person.lastName, person2.lastName);
expect(person.dateOfBirth, person2.dateOfBirth);
expect(person.orders.single.date, person2.orders.single.date);

expect(_encode(person2), equals(personJson));
});
Expand Down
4 changes: 2 additions & 2 deletions example/test/readme_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ void _expect(String fileName) {

final _pubspecContent = r'''
dependencies:
json_annotation: ^0.2.3
json_annotation: ^0.2.4

dev_dependencies:
build_runner: ^0.8.0
json_serializable: ^0.5.0
json_serializable: ^0.5.2
''';