json_convert_content.dart 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/auth_login_entity.dart';
  7. import 'package:domain/entity/captcha_img_entity.dart';
  8. import 'package:domain/entity/feedback_detail_entity.dart';
  9. import 'package:domain/entity/feedback_list_entity.dart';
  10. import 'package:domain/entity/id_name_entity.dart';
  11. import 'package:domain/entity/property_news_entity.dart';
  12. import 'package:domain/entity/property_sale_entity.dart';
  13. import 'package:domain/entity/server_time.dart';
  14. import 'package:domain/entity/user_me_entity.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. var covertFunc = convertFuncMap[type]!;
  126. if (covertFunc is Map<String, dynamic>) {
  127. return covertFunc(value as Map<String, dynamic>) as T;
  128. } else {
  129. return covertFunc(Map<String, dynamic>.from(value)) as T;
  130. }
  131. } else {
  132. throw UnimplementedError('$type unimplemented,you can try running the app again');
  133. }
  134. }
  135. }
  136. //list is returned by type
  137. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  138. if (<AuthLoginEntity>[] is M) {
  139. return data.map<AuthLoginEntity>((Map<String, dynamic> e) => AuthLoginEntity.fromJson(e)).toList() as M;
  140. }
  141. if (<CaptchaImgEntity>[] is M) {
  142. return data.map<CaptchaImgEntity>((Map<String, dynamic> e) => CaptchaImgEntity.fromJson(e)).toList() as M;
  143. }
  144. if (<FeedbackDetailEntity>[] is M) {
  145. return data.map<FeedbackDetailEntity>((Map<String, dynamic> e) => FeedbackDetailEntity.fromJson(e)).toList() as M;
  146. }
  147. if (<FeedbackDetailReplies>[] is M) {
  148. return data.map<FeedbackDetailReplies>((Map<String, dynamic> e) => FeedbackDetailReplies.fromJson(e)).toList() as M;
  149. }
  150. if (<FeedbackListEntity>[] is M) {
  151. return data.map<FeedbackListEntity>((Map<String, dynamic> e) => FeedbackListEntity.fromJson(e)).toList() as M;
  152. }
  153. if (<FeedbackItemEntity>[] is M) {
  154. return data.map<FeedbackItemEntity>((Map<String, dynamic> e) => FeedbackItemEntity.fromJson(e)).toList() as M;
  155. }
  156. if (<IdNameEntity>[] is M) {
  157. return data.map<IdNameEntity>((Map<String, dynamic> e) => IdNameEntity.fromJson(e)).toList() as M;
  158. }
  159. if (<PropertyNewsEntity>[] is M) {
  160. return data.map<PropertyNewsEntity>((Map<String, dynamic> e) => PropertyNewsEntity.fromJson(e)).toList() as M;
  161. }
  162. if (<PropertySaleEntity>[] is M) {
  163. return data.map<PropertySaleEntity>((Map<String, dynamic> e) => PropertySaleEntity.fromJson(e)).toList() as M;
  164. }
  165. if (<ServerTime>[] is M) {
  166. return data.map<ServerTime>((Map<String, dynamic> e) => ServerTime.fromJson(e)).toList() as M;
  167. }
  168. if (<UserMeEntity>[] is M) {
  169. return data.map<UserMeEntity>((Map<String, dynamic> e) => UserMeEntity.fromJson(e)).toList() as M;
  170. }
  171. if (<UserMeHouseholds>[] is M) {
  172. return data.map<UserMeHouseholds>((Map<String, dynamic> e) => UserMeHouseholds.fromJson(e)).toList() as M;
  173. }
  174. if (<UserMeEstates>[] is M) {
  175. return data.map<UserMeEstates>((Map<String, dynamic> e) => UserMeEstates.fromJson(e)).toList() as M;
  176. }
  177. if (<UserMeEstatesAccounts>[] is M) {
  178. return data.map<UserMeEstatesAccounts>((Map<String, dynamic> e) => UserMeEstatesAccounts.fromJson(e)).toList() as M;
  179. }
  180. if (<UserMeEstatesAccountsUnit>[] is M) {
  181. return data.map<UserMeEstatesAccountsUnit>((Map<String, dynamic> e) => UserMeEstatesAccountsUnit.fromJson(e)).toList() as M;
  182. }
  183. if (<UserMeDefaultUnit>[] is M) {
  184. return data.map<UserMeDefaultUnit>((Map<String, dynamic> e) => UserMeDefaultUnit.fromJson(e)).toList() as M;
  185. }
  186. if (<UserMeInformation>[] is M) {
  187. return data.map<UserMeInformation>((Map<String, dynamic> e) => UserMeInformation.fromJson(e)).toList() as M;
  188. }
  189. debugPrint("$M not found");
  190. return null;
  191. }
  192. static M? fromJsonAsT<M>(dynamic json) {
  193. if (json is M) {
  194. return json;
  195. }
  196. if (json is List) {
  197. return _getListChildType<M>(json.map((dynamic e) => e as Map<String, dynamic>).toList());
  198. } else {
  199. return jsonConvert.convert<M>(json);
  200. }
  201. }
  202. }
  203. class JsonConvertClassCollection {
  204. Map<String, JsonConvertFunction> convertFuncMap = {
  205. (AuthLoginEntity).toString(): AuthLoginEntity.fromJson,
  206. (CaptchaImgEntity).toString(): CaptchaImgEntity.fromJson,
  207. (FeedbackDetailEntity).toString(): FeedbackDetailEntity.fromJson,
  208. (FeedbackDetailReplies).toString(): FeedbackDetailReplies.fromJson,
  209. (FeedbackListEntity).toString(): FeedbackListEntity.fromJson,
  210. (FeedbackItemEntity).toString(): FeedbackItemEntity.fromJson,
  211. (IdNameEntity).toString(): IdNameEntity.fromJson,
  212. (PropertyNewsEntity).toString(): PropertyNewsEntity.fromJson,
  213. (PropertySaleEntity).toString(): PropertySaleEntity.fromJson,
  214. (ServerTime).toString(): ServerTime.fromJson,
  215. (UserMeEntity).toString(): UserMeEntity.fromJson,
  216. (UserMeHouseholds).toString(): UserMeHouseholds.fromJson,
  217. (UserMeEstates).toString(): UserMeEstates.fromJson,
  218. (UserMeEstatesAccounts).toString(): UserMeEstatesAccounts.fromJson,
  219. (UserMeEstatesAccountsUnit).toString(): UserMeEstatesAccountsUnit.fromJson,
  220. (UserMeDefaultUnit).toString(): UserMeDefaultUnit.fromJson,
  221. (UserMeInformation).toString(): UserMeInformation.fromJson,
  222. };
  223. bool containsKey(String type) {
  224. return convertFuncMap.containsKey(type);
  225. }
  226. JsonConvertFunction? operator [](String key) {
  227. return convertFuncMap[key];
  228. }
  229. }