-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjson_object_model.dart
139 lines (121 loc) · 3.12 KB
/
json_object_model.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import "dart:convert";
import "package:equatable/equatable.dart";
/// Extending this class will ensure that your class can be converted to a JSON
/// object.
abstract class JsonObjectModel extends Equatable {
const JsonObjectModel();
/// Returns a [Map] representation of the object.
Map<String, Object?> toJson();
/// Returns a representation of [toJson] encoded as a [String] with
/// [jsonEncode].
String toRawJson() => jsonEncode(toJson());
@override
String toString() => toRawJson();
}
/// This mixin provide a default implementation of [toJson], based on the
/// [jsonKeys] property.
mixin AutoJsonMixin on JsonObjectModel {
/// Declare the keys used for the JSON. You must ensure that the length is the
/// same as the [props].
///
/// The keys will be associated to the [props] value at the same index.
Set<String> get jsonKeys;
@override
Map<String, Object?> toJson() {
assert(jsonKeys.length == props.length);
final data = <String, Object?>{};
for (int i = 0; i < jsonKeys.length; i++) {
final key = jsonKeys.elementAt(i);
final value = props.elementAt(i);
data[key] = value;
}
return data;
}
}
/// Example
const companyEntry = 'company';
const usernameEntry = 'username';
const passwordEntry = 'password';
const roleEntry = 'role';
const phoneEntry = 'phone';
const cellEntry = 'cell';
class MyUser extends JsonObjectModel {
final String company;
final String username;
final String password;
final String role;
final String phone;
final String cell;
const MyUser({
required this.company,
required this.username,
required this.password,
required this.role,
required this.phone,
required this.cell,
});
factory MyUser.fromJson(Map<String, dynamic> json) {
return MyUser(
company: json[companyEntry] as String,
username: json[usernameEntry] as String,
password: json[passwordEntry] as String,
role: json[roleEntry] as String,
phone: json[phoneEntry] as String,
cell: json[cellEntry] as String,
);
}
@override
List<Object?> get props => [
company,
username,
password,
role,
phone,
cell,
];
@override
Map<String, Object?> toJson() {
return <String, Object?>{
companyEntry: company,
usernameEntry: username,
passwordEntry: password,
roleEntry: role,
phoneEntry: phone,
cellEntry: cell,
};
}
}
class MyUser2 extends JsonObjectModel with AutoJsonMixin {
final String company;
final String username;
final String password;
final String role;
final String phone;
final String cell;
const MyUser2({
required this.company,
required this.username,
required this.password,
required this.role,
required this.phone,
required this.cell,
});
@override
Set<String> get jsonKeys => <String>{
companyEntry,
usernameEntry,
passwordEntry,
roleEntry,
phoneEntry,
cellEntry,
};
@override
List<Object?> get props => [
company,
username,
password,
role,
phone,
cell,
];
}