json_convert_content.dart 9.1 KB

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