json_convert_content.dart 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // ignore_for_file: non_constant_identifier_names
  2. // ignore_for_file: camel_case_types
  3. // ignore_for_file: prefer_single_quotes
  4. // This file is automatically generated. DO NOT EDIT, all your changes would be lost.
  5. import 'package:flutter/material.dart' show debugPrint;
  6. import 'package:domain/entity/response/attendance_entity.dart';
  7. import 'package:domain/entity/response/check_success_entity.dart';
  8. import 'package:domain/entity/response/hotel_info_entity.dart';
  9. import 'package:domain/entity/response/id_name_entity.dart';
  10. import 'package:domain/entity/response/job_list_applied_edit_entity.dart';
  11. import 'package:domain/entity/response/job_list_applied_info_entity.dart';
  12. import 'package:domain/entity/response/job_list_applied_staff_list_entity.dart';
  13. import 'package:domain/entity/response/job_list_applied_staff_search_entity.dart';
  14. import 'package:domain/entity/response/job_list_detail_entity.dart';
  15. import 'package:domain/entity/response/job_list_entity.dart';
  16. import 'package:domain/entity/response/job_list_index_entity.dart';
  17. import 'package:domain/entity/response/job_list_remark_view_entity.dart';
  18. import 'package:domain/entity/response/labour_request_edit_index_entity.dart';
  19. import 'package:domain/entity/response/labour_request_index_entity.dart';
  20. import 'package:domain/entity/response/labour_request_list_entity.dart';
  21. import 'package:domain/entity/response/labour_request_work_flow_entity.dart';
  22. import 'package:domain/entity/server_time.dart';
  23. JsonConvert jsonConvert = JsonConvert();
  24. typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
  25. typedef EnumConvertFunction<T> = T Function(String value);
  26. typedef ConvertExceptionHandler = void Function(Object error, StackTrace stackTrace);
  27. extension MapSafeExt<K, V> on Map<K, V> {
  28. T? getOrNull<T>(K? key) {
  29. if (!containsKey(key) || key == null) {
  30. return null;
  31. } else {
  32. return this[key] as T?;
  33. }
  34. }
  35. }
  36. class JsonConvert {
  37. static ConvertExceptionHandler? onError;
  38. JsonConvertClassCollection convertFuncMap = JsonConvertClassCollection();
  39. /// When you are in the development, to generate a new model class, hot-reload doesn't find new generation model class, you can build on MaterialApp method called jsonConvert. ReassembleConvertFuncMap (); This method only works in a development environment
  40. /// https://flutter.cn/docs/development/tools/hot-reload
  41. /// class MyApp extends StatelessWidget {
  42. /// const MyApp({Key? key})
  43. /// : super(key: key);
  44. ///
  45. /// @override
  46. /// Widget build(BuildContext context) {
  47. /// jsonConvert.reassembleConvertFuncMap();
  48. /// return MaterialApp();
  49. /// }
  50. /// }
  51. void reassembleConvertFuncMap() {
  52. bool isReleaseMode = const bool.fromEnvironment('dart.vm.product');
  53. if (!isReleaseMode) {
  54. convertFuncMap = JsonConvertClassCollection();
  55. }
  56. }
  57. T? convert<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  58. if (value == null) {
  59. return null;
  60. }
  61. if (value is T) {
  62. return value;
  63. }
  64. try {
  65. return _asT<T>(value, enumConvert: enumConvert);
  66. } catch (e, stackTrace) {
  67. debugPrint('asT<$T> $e $stackTrace');
  68. if (onError != null) {
  69. onError!(e, stackTrace);
  70. }
  71. return null;
  72. }
  73. }
  74. List<T?>? convertList<T>(List<dynamic>? value, {EnumConvertFunction? enumConvert}) {
  75. if (value == null) {
  76. return null;
  77. }
  78. try {
  79. return value.map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)).toList();
  80. } catch (e, stackTrace) {
  81. debugPrint('asT<$T> $e $stackTrace');
  82. if (onError != null) {
  83. onError!(e, stackTrace);
  84. }
  85. return <T>[];
  86. }
  87. }
  88. List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  89. if (value == null) {
  90. return null;
  91. }
  92. try {
  93. return (value as List<dynamic>).map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)!).toList();
  94. } catch (e, stackTrace) {
  95. debugPrint('asT<$T> $e $stackTrace');
  96. if (onError != null) {
  97. onError!(e, stackTrace);
  98. }
  99. return <T>[];
  100. }
  101. }
  102. T? _asT<T extends Object?>(dynamic value,
  103. {EnumConvertFunction? enumConvert}) {
  104. final String type = T.toString();
  105. final String valueS = value.toString();
  106. if (enumConvert != null) {
  107. return enumConvert(valueS) as T;
  108. } else if (type == "String") {
  109. return valueS as T;
  110. } else if (type == "int") {
  111. final int? intValue = int.tryParse(valueS);
  112. if (intValue == null) {
  113. return double.tryParse(valueS)?.toInt() as T?;
  114. } else {
  115. return intValue as T;
  116. }
  117. } else if (type == "double") {
  118. return double.parse(valueS) as T;
  119. } else if (type == "DateTime") {
  120. return DateTime.parse(valueS) as T;
  121. } else if (type == "bool") {
  122. if (valueS == '0' || valueS == '1') {
  123. return (valueS == '1') as T;
  124. }
  125. return (valueS == 'true') as T;
  126. } else if (type == "Map" || type.startsWith("Map<")) {
  127. return value as T;
  128. } else {
  129. if (convertFuncMap.containsKey(type)) {
  130. if (value == null) {
  131. return null;
  132. }
  133. return convertFuncMap[type]!(value as Map<String, dynamic>) as T;
  134. } else {
  135. throw UnimplementedError('$type unimplemented,you can try running the app again');
  136. }
  137. }
  138. }
  139. //list is returned by type
  140. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  141. if (<AttendanceEntity>[] is M) {
  142. return data.map<AttendanceEntity>((Map<String, dynamic> e) => AttendanceEntity.fromJson(e)).toList() as M;
  143. }
  144. if (<AttendanceList>[] is M) {
  145. return data.map<AttendanceList>((Map<String, dynamic> e) => AttendanceList.fromJson(e)).toList() as M;
  146. }
  147. if (<CheckSuccessEntity>[] is M) {
  148. return data.map<CheckSuccessEntity>((Map<String, dynamic> e) => CheckSuccessEntity.fromJson(e)).toList() as M;
  149. }
  150. if (<HotelInfoEntity>[] is M) {
  151. return data.map<HotelInfoEntity>((Map<String, dynamic> e) => HotelInfoEntity.fromJson(e)).toList() as M;
  152. }
  153. if (<HotelInfoMenus>[] is M) {
  154. return data.map<HotelInfoMenus>((Map<String, dynamic> e) => HotelInfoMenus.fromJson(e)).toList() as M;
  155. }
  156. if (<HotelInfoMenusChildren>[] is M) {
  157. return data.map<HotelInfoMenusChildren>((Map<String, dynamic> e) => HotelInfoMenusChildren.fromJson(e)).toList() as M;
  158. }
  159. if (<IdNameEntity>[] is M) {
  160. return data.map<IdNameEntity>((Map<String, dynamic> e) => IdNameEntity.fromJson(e)).toList() as M;
  161. }
  162. if (<JobListAppliedEditEntity>[] is M) {
  163. return data.map<JobListAppliedEditEntity>((Map<String, dynamic> e) => JobListAppliedEditEntity.fromJson(e)).toList() as M;
  164. }
  165. if (<JobListAppliedEditReasonList>[] is M) {
  166. return data.map<JobListAppliedEditReasonList>((Map<String, dynamic> e) => JobListAppliedEditReasonList.fromJson(e)).toList() as M;
  167. }
  168. if (<JobListAppliedInfoEntity>[] is M) {
  169. return data.map<JobListAppliedInfoEntity>((Map<String, dynamic> e) => JobListAppliedInfoEntity.fromJson(e)).toList() as M;
  170. }
  171. if (<JobListAppliedStaffListEntity>[] is M) {
  172. return data.map<JobListAppliedStaffListEntity>((Map<String, dynamic> e) => JobListAppliedStaffListEntity.fromJson(e)).toList() as M;
  173. }
  174. if (<JobListAppliedStaffListRows>[] is M) {
  175. return data.map<JobListAppliedStaffListRows>((Map<String, dynamic> e) => JobListAppliedStaffListRows.fromJson(e)).toList() as M;
  176. }
  177. if (<JobListAppliedStaffListRowsSecurityIn>[] is M) {
  178. return data.map<JobListAppliedStaffListRowsSecurityIn>((Map<String, dynamic> e) => JobListAppliedStaffListRowsSecurityIn.fromJson(e)).toList() as M;
  179. }
  180. if (<JobListAppliedStaffListRowsSecurityOut>[] is M) {
  181. return data.map<JobListAppliedStaffListRowsSecurityOut>((Map<String, dynamic> e) => JobListAppliedStaffListRowsSecurityOut.fromJson(e)).toList() as M;
  182. }
  183. if (<JobListAppliedStaffListRowsWorkIn>[] is M) {
  184. return data.map<JobListAppliedStaffListRowsWorkIn>((Map<String, dynamic> e) => JobListAppliedStaffListRowsWorkIn.fromJson(e)).toList() as M;
  185. }
  186. if (<JobListAppliedStaffListRowsWorkOut>[] is M) {
  187. return data.map<JobListAppliedStaffListRowsWorkOut>((Map<String, dynamic> e) => JobListAppliedStaffListRowsWorkOut.fromJson(e)).toList() as M;
  188. }
  189. if (<JobListAppliedStaffSearchEntity>[] is M) {
  190. return data.map<JobListAppliedStaffSearchEntity>((Map<String, dynamic> e) => JobListAppliedStaffSearchEntity.fromJson(e)).toList() as M;
  191. }
  192. if (<JobListAppliedStaffSearchRows>[] is M) {
  193. return data.map<JobListAppliedStaffSearchRows>((Map<String, dynamic> e) => JobListAppliedStaffSearchRows.fromJson(e)).toList() as M;
  194. }
  195. if (<JobListDetailEntity>[] is M) {
  196. return data.map<JobListDetailEntity>((Map<String, dynamic> e) => JobListDetailEntity.fromJson(e)).toList() as M;
  197. }
  198. if (<JobListDetailDepartmentList>[] is M) {
  199. return data.map<JobListDetailDepartmentList>((Map<String, dynamic> e) => JobListDetailDepartmentList.fromJson(e)).toList() as M;
  200. }
  201. if (<JobListEntity>[] is M) {
  202. return data.map<JobListEntity>((Map<String, dynamic> e) => JobListEntity.fromJson(e)).toList() as M;
  203. }
  204. if (<JobListRows>[] is M) {
  205. return data.map<JobListRows>((Map<String, dynamic> e) => JobListRows.fromJson(e)).toList() as M;
  206. }
  207. if (<JobListIndexEntity>[] is M) {
  208. return data.map<JobListIndexEntity>((Map<String, dynamic> e) => JobListIndexEntity.fromJson(e)).toList() as M;
  209. }
  210. if (<JobListIndexDepartmentList>[] is M) {
  211. return data.map<JobListIndexDepartmentList>((Map<String, dynamic> e) => JobListIndexDepartmentList.fromJson(e)).toList() as M;
  212. }
  213. if (<JobListIndexStatusList>[] is M) {
  214. return data.map<JobListIndexStatusList>((Map<String, dynamic> e) => JobListIndexStatusList.fromJson(e)).toList() as M;
  215. }
  216. if (<JobListRemarkViewEntity>[] is M) {
  217. return data.map<JobListRemarkViewEntity>((Map<String, dynamic> e) => JobListRemarkViewEntity.fromJson(e)).toList() as M;
  218. }
  219. if (<LabourRequestEditIndexEntity>[] is M) {
  220. return data.map<LabourRequestEditIndexEntity>((Map<String, dynamic> e) => LabourRequestEditIndexEntity.fromJson(e)).toList() as M;
  221. }
  222. if (<LabourRequestEditIndexTemplateList>[] is M) {
  223. return data.map<LabourRequestEditIndexTemplateList>((Map<String, dynamic> e) => LabourRequestEditIndexTemplateList.fromJson(e)).toList() as M;
  224. }
  225. if (<LabourRequestEditIndexDepartmentList>[] is M) {
  226. return data.map<LabourRequestEditIndexDepartmentList>((Map<String, dynamic> e) => LabourRequestEditIndexDepartmentList.fromJson(e)).toList() as M;
  227. }
  228. if (<LabourRequestIndexEntity>[] is M) {
  229. return data.map<LabourRequestIndexEntity>((Map<String, dynamic> e) => LabourRequestIndexEntity.fromJson(e)).toList() as M;
  230. }
  231. if (<LabourRequestIndexDepartmentList>[] is M) {
  232. return data.map<LabourRequestIndexDepartmentList>((Map<String, dynamic> e) => LabourRequestIndexDepartmentList.fromJson(e)).toList() as M;
  233. }
  234. if (<LabourRequestIndexStatusList>[] is M) {
  235. return data.map<LabourRequestIndexStatusList>((Map<String, dynamic> e) => LabourRequestIndexStatusList.fromJson(e)).toList() as M;
  236. }
  237. if (<LabourRequestListEntity>[] is M) {
  238. return data.map<LabourRequestListEntity>((Map<String, dynamic> e) => LabourRequestListEntity.fromJson(e)).toList() as M;
  239. }
  240. if (<LabourRequestListRows>[] is M) {
  241. return data.map<LabourRequestListRows>((Map<String, dynamic> e) => LabourRequestListRows.fromJson(e)).toList() as M;
  242. }
  243. if (<LabourRequestWorkFlowEntity>[] is M) {
  244. return data.map<LabourRequestWorkFlowEntity>((Map<String, dynamic> e) => LabourRequestWorkFlowEntity.fromJson(e)).toList() as M;
  245. }
  246. if (<LabourRequestWorkFlowRecords>[] is M) {
  247. return data.map<LabourRequestWorkFlowRecords>((Map<String, dynamic> e) => LabourRequestWorkFlowRecords.fromJson(e)).toList() as M;
  248. }
  249. if (<ServerTime>[] is M) {
  250. return data.map<ServerTime>((Map<String, dynamic> e) => ServerTime.fromJson(e)).toList() as M;
  251. }
  252. debugPrint("$M not found");
  253. return null;
  254. }
  255. static M? fromJsonAsT<M>(dynamic json) {
  256. if (json is M) {
  257. return json;
  258. }
  259. if (json is List) {
  260. return _getListChildType<M>(json.map((dynamic e) => e as Map<String, dynamic>).toList());
  261. } else {
  262. return jsonConvert.convert<M>(json);
  263. }
  264. }
  265. }
  266. class JsonConvertClassCollection {
  267. Map<String, JsonConvertFunction> convertFuncMap = {
  268. (AttendanceEntity).toString(): AttendanceEntity.fromJson,
  269. (AttendanceList).toString(): AttendanceList.fromJson,
  270. (CheckSuccessEntity).toString(): CheckSuccessEntity.fromJson,
  271. (HotelInfoEntity).toString(): HotelInfoEntity.fromJson,
  272. (HotelInfoMenus).toString(): HotelInfoMenus.fromJson,
  273. (HotelInfoMenusChildren).toString(): HotelInfoMenusChildren.fromJson,
  274. (IdNameEntity).toString(): IdNameEntity.fromJson,
  275. (JobListAppliedEditEntity).toString(): JobListAppliedEditEntity.fromJson,
  276. (JobListAppliedEditReasonList).toString(): JobListAppliedEditReasonList.fromJson,
  277. (JobListAppliedInfoEntity).toString(): JobListAppliedInfoEntity.fromJson,
  278. (JobListAppliedStaffListEntity).toString(): JobListAppliedStaffListEntity.fromJson,
  279. (JobListAppliedStaffListRows).toString(): JobListAppliedStaffListRows.fromJson,
  280. (JobListAppliedStaffListRowsSecurityIn).toString(): JobListAppliedStaffListRowsSecurityIn.fromJson,
  281. (JobListAppliedStaffListRowsSecurityOut).toString(): JobListAppliedStaffListRowsSecurityOut.fromJson,
  282. (JobListAppliedStaffListRowsWorkIn).toString(): JobListAppliedStaffListRowsWorkIn.fromJson,
  283. (JobListAppliedStaffListRowsWorkOut).toString(): JobListAppliedStaffListRowsWorkOut.fromJson,
  284. (JobListAppliedStaffSearchEntity).toString(): JobListAppliedStaffSearchEntity.fromJson,
  285. (JobListAppliedStaffSearchRows).toString(): JobListAppliedStaffSearchRows.fromJson,
  286. (JobListDetailEntity).toString(): JobListDetailEntity.fromJson,
  287. (JobListDetailDepartmentList).toString(): JobListDetailDepartmentList.fromJson,
  288. (JobListEntity).toString(): JobListEntity.fromJson,
  289. (JobListRows).toString(): JobListRows.fromJson,
  290. (JobListIndexEntity).toString(): JobListIndexEntity.fromJson,
  291. (JobListIndexDepartmentList).toString(): JobListIndexDepartmentList.fromJson,
  292. (JobListIndexStatusList).toString(): JobListIndexStatusList.fromJson,
  293. (JobListRemarkViewEntity).toString(): JobListRemarkViewEntity.fromJson,
  294. (LabourRequestEditIndexEntity).toString(): LabourRequestEditIndexEntity.fromJson,
  295. (LabourRequestEditIndexTemplateList).toString(): LabourRequestEditIndexTemplateList.fromJson,
  296. (LabourRequestEditIndexDepartmentList).toString(): LabourRequestEditIndexDepartmentList.fromJson,
  297. (LabourRequestIndexEntity).toString(): LabourRequestIndexEntity.fromJson,
  298. (LabourRequestIndexDepartmentList).toString(): LabourRequestIndexDepartmentList.fromJson,
  299. (LabourRequestIndexStatusList).toString(): LabourRequestIndexStatusList.fromJson,
  300. (LabourRequestListEntity).toString(): LabourRequestListEntity.fromJson,
  301. (LabourRequestListRows).toString(): LabourRequestListRows.fromJson,
  302. (LabourRequestWorkFlowEntity).toString(): LabourRequestWorkFlowEntity.fromJson,
  303. (LabourRequestWorkFlowRecords).toString(): LabourRequestWorkFlowRecords.fromJson,
  304. (ServerTime).toString(): ServerTime.fromJson,
  305. };
  306. bool containsKey(String type) {
  307. return convertFuncMap.containsKey(type);
  308. }
  309. JsonConvertFunction? operator [](String key) {
  310. return convertFuncMap[key];
  311. }
  312. }