json_convert_content.dart 6.5 KB

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