json_convert_content.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/labour_request_add_option_entity.dart';
  11. import 'package:domain/entity/response/labour_request_edit_index_entity.dart';
  12. import 'package:domain/entity/response/labour_request_index_entity.dart';
  13. import 'package:domain/entity/response/labour_request_list_entity.dart';
  14. import 'package:domain/entity/server_time.dart';
  15. JsonConvert jsonConvert = JsonConvert();
  16. typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
  17. typedef EnumConvertFunction<T> = T Function(String value);
  18. typedef ConvertExceptionHandler = void Function(Object error, StackTrace stackTrace);
  19. extension MapSafeExt<K, V> on Map<K, V> {
  20. T? getOrNull<T>(K? key) {
  21. if (!containsKey(key) || key == null) {
  22. return null;
  23. } else {
  24. return this[key] as T?;
  25. }
  26. }
  27. }
  28. class JsonConvert {
  29. static ConvertExceptionHandler? onError;
  30. JsonConvertClassCollection convertFuncMap = JsonConvertClassCollection();
  31. /// 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
  32. /// https://flutter.cn/docs/development/tools/hot-reload
  33. /// class MyApp extends StatelessWidget {
  34. /// const MyApp({Key? key})
  35. /// : super(key: key);
  36. ///
  37. /// @override
  38. /// Widget build(BuildContext context) {
  39. /// jsonConvert.reassembleConvertFuncMap();
  40. /// return MaterialApp();
  41. /// }
  42. /// }
  43. void reassembleConvertFuncMap() {
  44. bool isReleaseMode = const bool.fromEnvironment('dart.vm.product');
  45. if (!isReleaseMode) {
  46. convertFuncMap = JsonConvertClassCollection();
  47. }
  48. }
  49. T? convert<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  50. if (value == null) {
  51. return null;
  52. }
  53. if (value is T) {
  54. return value;
  55. }
  56. try {
  57. return _asT<T>(value, enumConvert: enumConvert);
  58. } catch (e, stackTrace) {
  59. debugPrint('asT<$T> $e $stackTrace');
  60. if (onError != null) {
  61. onError!(e, stackTrace);
  62. }
  63. return null;
  64. }
  65. }
  66. List<T?>? convertList<T>(List<dynamic>? value, {EnumConvertFunction? enumConvert}) {
  67. if (value == null) {
  68. return null;
  69. }
  70. try {
  71. return value.map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)).toList();
  72. } catch (e, stackTrace) {
  73. debugPrint('asT<$T> $e $stackTrace');
  74. if (onError != null) {
  75. onError!(e, stackTrace);
  76. }
  77. return <T>[];
  78. }
  79. }
  80. List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  81. if (value == null) {
  82. return null;
  83. }
  84. try {
  85. return (value as List<dynamic>).map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)!).toList();
  86. } catch (e, stackTrace) {
  87. debugPrint('asT<$T> $e $stackTrace');
  88. if (onError != null) {
  89. onError!(e, stackTrace);
  90. }
  91. return <T>[];
  92. }
  93. }
  94. T? _asT<T extends Object?>(dynamic value,
  95. {EnumConvertFunction? enumConvert}) {
  96. final String type = T.toString();
  97. final String valueS = value.toString();
  98. if (enumConvert != null) {
  99. return enumConvert(valueS) as T;
  100. } else if (type == "String") {
  101. return valueS as T;
  102. } else if (type == "int") {
  103. final int? intValue = int.tryParse(valueS);
  104. if (intValue == null) {
  105. return double.tryParse(valueS)?.toInt() as T?;
  106. } else {
  107. return intValue as T;
  108. }
  109. } else if (type == "double") {
  110. return double.parse(valueS) as T;
  111. } else if (type == "DateTime") {
  112. return DateTime.parse(valueS) as T;
  113. } else if (type == "bool") {
  114. if (valueS == '0' || valueS == '1') {
  115. return (valueS == '1') as T;
  116. }
  117. return (valueS == 'true') as T;
  118. } else if (type == "Map" || type.startsWith("Map<")) {
  119. return value as T;
  120. } else {
  121. if (convertFuncMap.containsKey(type)) {
  122. if (value == null) {
  123. return null;
  124. }
  125. return convertFuncMap[type]!(value as Map<String, dynamic>) as T;
  126. } else {
  127. throw UnimplementedError('$type unimplemented,you can try running the app again');
  128. }
  129. }
  130. }
  131. //list is returned by type
  132. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  133. if (<AttendanceEntity>[] is M) {
  134. return data.map<AttendanceEntity>((Map<String, dynamic> e) => AttendanceEntity.fromJson(e)).toList() as M;
  135. }
  136. if (<AttendanceList>[] is M) {
  137. return data.map<AttendanceList>((Map<String, dynamic> e) => AttendanceList.fromJson(e)).toList() as M;
  138. }
  139. if (<CheckSuccessEntity>[] is M) {
  140. return data.map<CheckSuccessEntity>((Map<String, dynamic> e) => CheckSuccessEntity.fromJson(e)).toList() as M;
  141. }
  142. if (<HotelInfoEntity>[] is M) {
  143. return data.map<HotelInfoEntity>((Map<String, dynamic> e) => HotelInfoEntity.fromJson(e)).toList() as M;
  144. }
  145. if (<HotelInfoMenus>[] is M) {
  146. return data.map<HotelInfoMenus>((Map<String, dynamic> e) => HotelInfoMenus.fromJson(e)).toList() as M;
  147. }
  148. if (<HotelInfoMenusChildren>[] is M) {
  149. return data.map<HotelInfoMenusChildren>((Map<String, dynamic> e) => HotelInfoMenusChildren.fromJson(e)).toList() as M;
  150. }
  151. if (<IdNameEntity>[] is M) {
  152. return data.map<IdNameEntity>((Map<String, dynamic> e) => IdNameEntity.fromJson(e)).toList() as M;
  153. }
  154. if (<LabourRequestAddOptionEntity>[] is M) {
  155. return data.map<LabourRequestAddOptionEntity>((Map<String, dynamic> e) => LabourRequestAddOptionEntity.fromJson(e)).toList() as M;
  156. }
  157. if (<LabourRequestAddOptionTemplateList>[] is M) {
  158. return data.map<LabourRequestAddOptionTemplateList>((Map<String, dynamic> e) => LabourRequestAddOptionTemplateList.fromJson(e)).toList() as M;
  159. }
  160. if (<LabourRequestAddOptionDepartmentList>[] is M) {
  161. return data.map<LabourRequestAddOptionDepartmentList>((Map<String, dynamic> e) => LabourRequestAddOptionDepartmentList.fromJson(e)).toList() as M;
  162. }
  163. if (<LabourRequestEditIndexEntity>[] is M) {
  164. return data.map<LabourRequestEditIndexEntity>((Map<String, dynamic> e) => LabourRequestEditIndexEntity.fromJson(e)).toList() as M;
  165. }
  166. if (<LabourRequestEditIndexTemplateList>[] is M) {
  167. return data.map<LabourRequestEditIndexTemplateList>((Map<String, dynamic> e) => LabourRequestEditIndexTemplateList.fromJson(e)).toList() as M;
  168. }
  169. if (<LabourRequestEditIndexDepartmentList>[] is M) {
  170. return data.map<LabourRequestEditIndexDepartmentList>((Map<String, dynamic> e) => LabourRequestEditIndexDepartmentList.fromJson(e)).toList() as M;
  171. }
  172. if (<LabourRequestIndexEntity>[] is M) {
  173. return data.map<LabourRequestIndexEntity>((Map<String, dynamic> e) => LabourRequestIndexEntity.fromJson(e)).toList() as M;
  174. }
  175. if (<LabourRequestIndexDepartmentList>[] is M) {
  176. return data.map<LabourRequestIndexDepartmentList>((Map<String, dynamic> e) => LabourRequestIndexDepartmentList.fromJson(e)).toList() as M;
  177. }
  178. if (<LabourRequestIndexStatusList>[] is M) {
  179. return data.map<LabourRequestIndexStatusList>((Map<String, dynamic> e) => LabourRequestIndexStatusList.fromJson(e)).toList() as M;
  180. }
  181. if (<LabourRequestListEntity>[] is M) {
  182. return data.map<LabourRequestListEntity>((Map<String, dynamic> e) => LabourRequestListEntity.fromJson(e)).toList() as M;
  183. }
  184. if (<LabourRequestListRows>[] is M) {
  185. return data.map<LabourRequestListRows>((Map<String, dynamic> e) => LabourRequestListRows.fromJson(e)).toList() as M;
  186. }
  187. if (<ServerTime>[] is M) {
  188. return data.map<ServerTime>((Map<String, dynamic> e) => ServerTime.fromJson(e)).toList() as M;
  189. }
  190. debugPrint("$M not found");
  191. return null;
  192. }
  193. static M? fromJsonAsT<M>(dynamic json) {
  194. if (json is M) {
  195. return json;
  196. }
  197. if (json is List) {
  198. return _getListChildType<M>(json.map((dynamic e) => e as Map<String, dynamic>).toList());
  199. } else {
  200. return jsonConvert.convert<M>(json);
  201. }
  202. }
  203. }
  204. class JsonConvertClassCollection {
  205. Map<String, JsonConvertFunction> convertFuncMap = {
  206. (AttendanceEntity).toString(): AttendanceEntity.fromJson,
  207. (AttendanceList).toString(): AttendanceList.fromJson,
  208. (CheckSuccessEntity).toString(): CheckSuccessEntity.fromJson,
  209. (HotelInfoEntity).toString(): HotelInfoEntity.fromJson,
  210. (HotelInfoMenus).toString(): HotelInfoMenus.fromJson,
  211. (HotelInfoMenusChildren).toString(): HotelInfoMenusChildren.fromJson,
  212. (IdNameEntity).toString(): IdNameEntity.fromJson,
  213. (LabourRequestAddOptionEntity).toString(): LabourRequestAddOptionEntity.fromJson,
  214. (LabourRequestAddOptionTemplateList).toString(): LabourRequestAddOptionTemplateList.fromJson,
  215. (LabourRequestAddOptionDepartmentList).toString(): LabourRequestAddOptionDepartmentList.fromJson,
  216. (LabourRequestEditIndexEntity).toString(): LabourRequestEditIndexEntity.fromJson,
  217. (LabourRequestEditIndexTemplateList).toString(): LabourRequestEditIndexTemplateList.fromJson,
  218. (LabourRequestEditIndexDepartmentList).toString(): LabourRequestEditIndexDepartmentList.fromJson,
  219. (LabourRequestIndexEntity).toString(): LabourRequestIndexEntity.fromJson,
  220. (LabourRequestIndexDepartmentList).toString(): LabourRequestIndexDepartmentList.fromJson,
  221. (LabourRequestIndexStatusList).toString(): LabourRequestIndexStatusList.fromJson,
  222. (LabourRequestListEntity).toString(): LabourRequestListEntity.fromJson,
  223. (LabourRequestListRows).toString(): LabourRequestListRows.fromJson,
  224. (ServerTime).toString(): ServerTime.fromJson,
  225. };
  226. bool containsKey(String type) {
  227. return convertFuncMap.containsKey(type);
  228. }
  229. JsonConvertFunction? operator [](String key) {
  230. return convertFuncMap[key];
  231. }
  232. }