forked from google/json_serializable.dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.dart
87 lines (63 loc) · 2.12 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) 2015, 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.
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable()
class Person {
final String firstName;
@JsonKey(includeIfNull: false)
final String? middleName;
final String lastName;
@JsonKey(name: 'date-of-birth')
final DateTime dateOfBirth;
@JsonKey(name: 'last-order')
final DateTime? lastOrder;
List<Order> orders;
Person(
this.firstName,
this.lastName,
this.dateOfBirth, {
this.middleName,
this.lastOrder,
List<Order>? orders,
}) : orders = orders ?? <Order>[];
factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);
Map<String, dynamic> toJson() => _$PersonToJson(this);
}
@JsonSerializable(includeIfNull: false)
class Order {
int? count;
int? itemNumber;
bool? isRushed;
Item? item;
@JsonKey(
name: 'prep-time',
fromJson: _durationFromMilliseconds,
toJson: _durationToMilliseconds)
Duration? prepTime;
@JsonKey(fromJson: _dateTimeFromEpochUs, toJson: _dateTimeToEpochUs)
final DateTime date;
Order(this.date);
factory Order.fromJson(Map<String, dynamic> json) => _$OrderFromJson(json);
Map<String, dynamic> toJson() => _$OrderToJson(this);
static Duration? _durationFromMilliseconds(int? milliseconds) =>
milliseconds == null ? null : Duration(milliseconds: milliseconds);
static int? _durationToMilliseconds(Duration? duration) =>
duration?.inMilliseconds;
static DateTime _dateTimeFromEpochUs(int us) =>
DateTime.fromMicrosecondsSinceEpoch(us);
static int? _dateTimeToEpochUs(DateTime? dateTime) =>
dateTime?.microsecondsSinceEpoch;
}
@JsonSerializable()
class Item {
int? count;
int? itemNumber;
bool? isRushed;
Item();
factory Item.fromJson(Map<String, dynamic> json) => _$ItemFromJson(json);
Map<String, dynamic> toJson() => _$ItemToJson(this);
}
@JsonLiteral('data.json')
Map get glossaryData => _$glossaryDataJsonLiteral;