json_convert_content.dart 7.7 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/auth_login_entity.dart';
  7. import 'package:domain/entity/captcha_img_entity.dart';
  8. import 'package:domain/entity/property_news_entity.dart';
  9. import 'package:domain/entity/property_sale_entity.dart';
  10. import 'package:domain/entity/server_time.dart';
  11. import 'package:domain/entity/user_me_entity.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,
  64. {EnumConvertFunction? enumConvert}) {
  65. if (value == null) {
  66. return null;
  67. }
  68. try {
  69. return value.map((dynamic e) => _asT<T>(e, enumConvert: enumConvert))
  70. .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,
  80. {EnumConvertFunction? enumConvert}) {
  81. if (value == null) {
  82. return null;
  83. }
  84. try {
  85. return (value as List<dynamic>).map((dynamic e) =>
  86. _asT<T>(e, enumConvert: enumConvert)!).toList();
  87. } catch (e, stackTrace) {
  88. debugPrint('asT<$T> $e $stackTrace');
  89. if (onError != null) {
  90. onError!(e, stackTrace);
  91. }
  92. return <T>[];
  93. }
  94. }
  95. T? _asT<T extends Object?>(dynamic value,
  96. {EnumConvertFunction? enumConvert}) {
  97. final String type = T.toString();
  98. final String valueS = value.toString();
  99. if (enumConvert != null) {
  100. return enumConvert(valueS) as T;
  101. } else if (type == "String") {
  102. return valueS as T;
  103. } else if (type == "int") {
  104. final int? intValue = int.tryParse(valueS);
  105. if (intValue == null) {
  106. return double.tryParse(valueS)?.toInt() as T?;
  107. } else {
  108. return intValue as T;
  109. }
  110. } else if (type == "double") {
  111. return double.parse(valueS) as T;
  112. } else if (type == "DateTime") {
  113. return DateTime.parse(valueS) as T;
  114. } else if (type == "bool") {
  115. if (valueS == '0' || valueS == '1') {
  116. return (valueS == '1') as T;
  117. }
  118. return (valueS == 'true') as T;
  119. } else if (type == "Map" || type.startsWith("Map<")) {
  120. return value as T;
  121. } else {
  122. if (convertFuncMap.containsKey(type)) {
  123. if (value == null) {
  124. return null;
  125. }
  126. var covertFunc = convertFuncMap[type]!;
  127. if (covertFunc is Map<String, dynamic>) {
  128. return covertFunc(value as Map<String, dynamic>) as T;
  129. } else {
  130. return covertFunc(Map<String, dynamic>.from(value)) as T;
  131. }
  132. } else {
  133. throw UnimplementedError(
  134. '$type unimplemented,you can try running the app again');
  135. }
  136. }
  137. }
  138. //list is returned by type
  139. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  140. if (<AuthLoginEntity>[] is M) {
  141. return data.map<AuthLoginEntity>((Map<String, dynamic> e) =>
  142. AuthLoginEntity.fromJson(e)).toList() as M;
  143. }
  144. if (<CaptchaImgEntity>[] is M) {
  145. return data.map<CaptchaImgEntity>((Map<String, dynamic> e) =>
  146. CaptchaImgEntity.fromJson(e)).toList() as M;
  147. }
  148. if (<PropertyNewsEntity>[] is M) {
  149. return data.map<PropertyNewsEntity>((Map<String, dynamic> e) =>
  150. PropertyNewsEntity.fromJson(e)).toList() as M;
  151. }
  152. if (<PropertySaleEntity>[] is M) {
  153. return data.map<PropertySaleEntity>((Map<String, dynamic> e) =>
  154. PropertySaleEntity.fromJson(e)).toList() as M;
  155. }
  156. if (<ServerTime>[] is M) {
  157. return data.map<ServerTime>((Map<String, dynamic> e) =>
  158. ServerTime.fromJson(e)).toList() as M;
  159. }
  160. if (<UserMeEntity>[] is M) {
  161. return data.map<UserMeEntity>((Map<String, dynamic> e) =>
  162. UserMeEntity.fromJson(e)).toList() as M;
  163. }
  164. if (<UserMeHouseholds>[] is M) {
  165. return data.map<UserMeHouseholds>((Map<String, dynamic> e) =>
  166. UserMeHouseholds.fromJson(e)).toList() as M;
  167. }
  168. if (<UserMeEstates>[] is M) {
  169. return data.map<UserMeEstates>((Map<String, dynamic> e) =>
  170. UserMeEstates.fromJson(e)).toList() as M;
  171. }
  172. if (<UserMeEstatesAccounts>[] is M) {
  173. return data.map<UserMeEstatesAccounts>((Map<String, dynamic> e) =>
  174. UserMeEstatesAccounts.fromJson(e)).toList() as M;
  175. }
  176. if (<UserMeEstatesAccountsUnit>[] is M) {
  177. return data.map<UserMeEstatesAccountsUnit>((Map<String, dynamic> e) =>
  178. UserMeEstatesAccountsUnit.fromJson(e)).toList() as M;
  179. }
  180. if (<UserMeDefaultUnit>[] is M) {
  181. return data.map<UserMeDefaultUnit>((Map<String, dynamic> e) =>
  182. UserMeDefaultUnit.fromJson(e)).toList() as M;
  183. }
  184. debugPrint("$M not found");
  185. return null;
  186. }
  187. static M? fromJsonAsT<M>(dynamic json) {
  188. if (json is M) {
  189. return json;
  190. }
  191. if (json is List) {
  192. return _getListChildType<M>(
  193. json.map((dynamic e) => e as Map<String, dynamic>).toList());
  194. } else {
  195. return jsonConvert.convert<M>(json);
  196. }
  197. }
  198. }
  199. class JsonConvertClassCollection {
  200. Map<String, JsonConvertFunction> convertFuncMap = {
  201. (AuthLoginEntity).toString(): AuthLoginEntity.fromJson,
  202. (CaptchaImgEntity).toString(): CaptchaImgEntity.fromJson,
  203. (PropertyNewsEntity).toString(): PropertyNewsEntity.fromJson,
  204. (PropertySaleEntity).toString(): PropertySaleEntity.fromJson,
  205. (ServerTime).toString(): ServerTime.fromJson,
  206. (UserMeEntity).toString(): UserMeEntity.fromJson,
  207. (UserMeHouseholds).toString(): UserMeHouseholds.fromJson,
  208. (UserMeEstates).toString(): UserMeEstates.fromJson,
  209. (UserMeEstatesAccounts).toString(): UserMeEstatesAccounts.fromJson,
  210. (UserMeEstatesAccountsUnit).toString(): UserMeEstatesAccountsUnit.fromJson,
  211. (UserMeDefaultUnit).toString(): UserMeDefaultUnit.fromJson,
  212. };
  213. bool containsKey(String type) {
  214. return convertFuncMap.containsKey(type);
  215. }
  216. JsonConvertFunction? operator [](String key) {
  217. return convertFuncMap[key];
  218. }
  219. }