json_convert_content.dart
3.41 KB
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
// ignore_for_file: non_constant_identifier_names
// ignore_for_file: camel_case_types
// ignore_for_file: prefer_single_quotes
// This file is automatically generated. DO NOT EDIT, all your changes would be lost.
import 'package:one_poem/account/models/user_entity.dart';
import 'package:one_poem/category/models/category_item_entity.dart';
import 'package:one_poem/timeline/models/friend_entity.dart';
JsonConvert jsonConvert = JsonConvert();
class JsonConvert {
T? convert<T>(dynamic value) {
if (value == null) {
return null;
}
return asT<T>(value);
}
List<T?>? convertList<T>(List<dynamic>? value) {
if (value == null) {
return null;
}
try {
return value.map((dynamic e) => asT<T>(e)).toList();
} catch (e, stackTrace) {
print('asT<$T> $e $stackTrace');
return <T>[];
}
}
List<T>? convertListNotNull<T>(dynamic value) {
if (value == null) {
return null;
}
try {
return (value as List<dynamic>).map((dynamic e) => asT<T>(e)!).toList();
} catch (e, stackTrace) {
print('asT<$T> $e $stackTrace');
return <T>[];
}
}
T? asT<T extends Object?>(dynamic value) {
if (value is T) {
return value;
}
final String type = T.toString();
try {
final String valueS = value.toString();
if (type == "String") {
return valueS as T;
} else if (type == "int") {
final int? intValue = int.tryParse(valueS);
if (intValue == null) {
return double.tryParse(valueS)?.toInt() as T?;
} else {
return intValue as T;
} } else if (type == "double") {
return double.parse(valueS) as T;
} else if (type == "DateTime") {
return DateTime.parse(valueS) as T;
} else if (type == "bool") {
if (valueS == '0' || valueS == '1') {
return (valueS == '1') as T;
}
return (valueS == 'true') as T;
} else {
return JsonConvert.fromJsonAsT<T>(value);
}
} catch (e, stackTrace) {
print('asT<$T> $e $stackTrace');
return null;
}
}
//Go back to a single instance by type
static M? _fromJsonSingle<M>(Map<String, dynamic> json) {
final String type = M.toString();
if(type == (UserEntity).toString()){
return UserEntity.fromJson(json) as M;
}
if(type == (CategoryItemEntity).toString()){
return CategoryItemEntity.fromJson(json) as M;
}
if(type == (FriendEntity).toString()){
return FriendEntity.fromJson(json) as M;
}
if(type == (FriendData).toString()){
return FriendData.fromJson(json) as M;
}
print("$type not found");
return null;
}
//list is returned by type
static M? _getListChildType<M>(List<dynamic> data) {
if(<UserEntity>[] is M){
return data.map<UserEntity>((e) => UserEntity.fromJson(e)).toList() as M;
}
if(<CategoryItemEntity>[] is M){
return data.map<CategoryItemEntity>((e) => CategoryItemEntity.fromJson(e)).toList() as M;
}
if(<FriendEntity>[] is M){
return data.map<FriendEntity>((e) => FriendEntity.fromJson(e)).toList() as M;
}
if(<FriendData>[] is M){
return data.map<FriendData>((e) => FriendData.fromJson(e)).toList() as M;
}
print("${M.toString()} not found");
return null;
}
static M? fromJsonAsT<M>(dynamic json) {
if(json == null){
return null;
}
if (json is List) {
return _getListChildType<M>(json);
} else {
return _fromJsonSingle<M>(json as Map<String, dynamic>);
}
}
}