json_convert_content.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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/newsfeed_comment_publish_entity.dart';
  9. import 'package:domain/entity/newsfeed_following_entity.dart';
  10. import 'package:domain/entity/newsfeed_foryou_entity.dart';
  11. import 'package:domain/entity/newsfeed_news_entity.dart';
  12. import 'package:domain/entity/property_news_entity.dart';
  13. import 'package:domain/entity/property_sale_rent_entity.dart';
  14. import 'package:domain/entity/server_time.dart';
  15. import 'package:domain/entity/user_me_entity.dart';
  16. JsonConvert jsonConvert = JsonConvert();
  17. typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
  18. typedef EnumConvertFunction<T> = T Function(String value);
  19. typedef ConvertExceptionHandler = void Function(Object error, StackTrace stackTrace);
  20. extension MapSafeExt<K, V> on Map<K, V> {
  21. T? getOrNull<T>(K? key) {
  22. if (!containsKey(key) || key == null) {
  23. return null;
  24. } else {
  25. return this[key] as T?;
  26. }
  27. }
  28. }
  29. class JsonConvert {
  30. static ConvertExceptionHandler? onError;
  31. JsonConvertClassCollection convertFuncMap = JsonConvertClassCollection();
  32. /// 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
  33. /// https://flutter.cn/docs/development/tools/hot-reload
  34. /// class MyApp extends StatelessWidget {
  35. /// const MyApp({Key? key})
  36. /// : super(key: key);
  37. ///
  38. /// @override
  39. /// Widget build(BuildContext context) {
  40. /// jsonConvert.reassembleConvertFuncMap();
  41. /// return MaterialApp();
  42. /// }
  43. /// }
  44. void reassembleConvertFuncMap() {
  45. bool isReleaseMode = const bool.fromEnvironment('dart.vm.product');
  46. if (!isReleaseMode) {
  47. convertFuncMap = JsonConvertClassCollection();
  48. }
  49. }
  50. T? convert<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  51. if (value == null) {
  52. return null;
  53. }
  54. if (value is T) {
  55. return value;
  56. }
  57. try {
  58. return _asT<T>(value, enumConvert: enumConvert);
  59. } catch (e, stackTrace) {
  60. debugPrint('asT<$T> $e $stackTrace');
  61. if (onError != null) {
  62. onError!(e, stackTrace);
  63. }
  64. return null;
  65. }
  66. }
  67. List<T?>? convertList<T>(List<dynamic>? value,
  68. {EnumConvertFunction? enumConvert}) {
  69. if (value == null) {
  70. return null;
  71. }
  72. try {
  73. return value.map((dynamic e) => _asT<T>(e, enumConvert: enumConvert))
  74. .toList();
  75. } catch (e, stackTrace) {
  76. debugPrint('asT<$T> $e $stackTrace');
  77. if (onError != null) {
  78. onError!(e, stackTrace);
  79. }
  80. return <T>[];
  81. }
  82. }
  83. List<T>? convertListNotNull<T>(dynamic value,
  84. {EnumConvertFunction? enumConvert}) {
  85. if (value == null) {
  86. return null;
  87. }
  88. try {
  89. return (value as List<dynamic>).map((dynamic e) =>
  90. _asT<T>(e, enumConvert: enumConvert)!).toList();
  91. } catch (e, stackTrace) {
  92. debugPrint('asT<$T> $e $stackTrace');
  93. if (onError != null) {
  94. onError!(e, stackTrace);
  95. }
  96. return <T>[];
  97. }
  98. }
  99. T? _asT<T extends Object?>(dynamic value,
  100. {EnumConvertFunction? enumConvert}) {
  101. final String type = T.toString();
  102. final String valueS = value.toString();
  103. if (enumConvert != null) {
  104. return enumConvert(valueS) as T;
  105. } else if (type == "String") {
  106. return valueS as T;
  107. } else if (type == "int") {
  108. final int? intValue = int.tryParse(valueS);
  109. if (intValue == null) {
  110. return double.tryParse(valueS)?.toInt() as T?;
  111. } else {
  112. return intValue as T;
  113. }
  114. } else if (type == "double") {
  115. return double.parse(valueS) as T;
  116. } else if (type == "DateTime") {
  117. return DateTime.parse(valueS) as T;
  118. } else if (type == "bool") {
  119. if (valueS == '0' || valueS == '1') {
  120. return (valueS == '1') as T;
  121. }
  122. return (valueS == 'true') as T;
  123. } else if (type == "Map" || type.startsWith("Map<")) {
  124. return value as T;
  125. } else {
  126. if (convertFuncMap.containsKey(type)) {
  127. if (value == null) {
  128. return null;
  129. }
  130. var covertFunc = convertFuncMap[type]!;
  131. if (covertFunc is Map<String, dynamic>) {
  132. return covertFunc(value as Map<String, dynamic>) as T;
  133. } else {
  134. return covertFunc(Map<String, dynamic>.from(value)) as T;
  135. }
  136. } else {
  137. throw UnimplementedError(
  138. '$type unimplemented,you can try running the app again');
  139. }
  140. }
  141. }
  142. //list is returned by type
  143. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  144. if (<AuthLoginEntity>[] is M) {
  145. return data.map<AuthLoginEntity>((Map<String, dynamic> e) =>
  146. AuthLoginEntity.fromJson(e)).toList() as M;
  147. }
  148. if (<CaptchaImgEntity>[] is M) {
  149. return data.map<CaptchaImgEntity>((Map<String, dynamic> e) =>
  150. CaptchaImgEntity.fromJson(e)).toList() as M;
  151. }
  152. if (<NewsfeedCommentPublishEntity>[] is M) {
  153. return data.map<NewsfeedCommentPublishEntity>((Map<String, dynamic> e) =>
  154. NewsfeedCommentPublishEntity.fromJson(e)).toList() as M;
  155. }
  156. if (<NewsfeedFollowingEntity>[] is M) {
  157. return data.map<NewsfeedFollowingEntity>((Map<String, dynamic> e) =>
  158. NewsfeedFollowingEntity.fromJson(e)).toList() as M;
  159. }
  160. if (<NewsfeedFollowingList>[] is M) {
  161. return data.map<NewsfeedFollowingList>((Map<String, dynamic> e) =>
  162. NewsfeedFollowingList.fromJson(e)).toList() as M;
  163. }
  164. if (<NewsfeedFollowingListAccount>[] is M) {
  165. return data.map<NewsfeedFollowingListAccount>((Map<String, dynamic> e) =>
  166. NewsfeedFollowingListAccount.fromJson(e)).toList() as M;
  167. }
  168. if (<NewsfeedForyouEntity>[] is M) {
  169. return data.map<NewsfeedForyouEntity>((Map<String, dynamic> e) =>
  170. NewsfeedForyouEntity.fromJson(e)).toList() as M;
  171. }
  172. if (<NewsfeedForyouList>[] is M) {
  173. return data.map<NewsfeedForyouList>((Map<String, dynamic> e) =>
  174. NewsfeedForyouList.fromJson(e)).toList() as M;
  175. }
  176. if (<NewsfeedForyouListAccount>[] is M) {
  177. return data.map<NewsfeedForyouListAccount>((Map<String, dynamic> e) =>
  178. NewsfeedForyouListAccount.fromJson(e)).toList() as M;
  179. }
  180. if (<NewsfeedNewsEntity>[] is M) {
  181. return data.map<NewsfeedNewsEntity>((Map<String, dynamic> e) =>
  182. NewsfeedNewsEntity.fromJson(e)).toList() as M;
  183. }
  184. if (<NewsfeedNewsList>[] is M) {
  185. return data.map<NewsfeedNewsList>((Map<String, dynamic> e) =>
  186. NewsfeedNewsList.fromJson(e)).toList() as M;
  187. }
  188. if (<NewsfeedNewsListAccount>[] is M) {
  189. return data.map<NewsfeedNewsListAccount>((Map<String, dynamic> e) =>
  190. NewsfeedNewsListAccount.fromJson(e)).toList() as M;
  191. }
  192. if (<PropertyNewsEntity>[] is M) {
  193. return data.map<PropertyNewsEntity>((Map<String, dynamic> e) =>
  194. PropertyNewsEntity.fromJson(e)).toList() as M;
  195. }
  196. if (<PropertyNewsList>[] is M) {
  197. return data.map<PropertyNewsList>((Map<String, dynamic> e) =>
  198. PropertyNewsList.fromJson(e)).toList() as M;
  199. }
  200. if (<PropertySaleRentEntity>[] is M) {
  201. return data.map<PropertySaleRentEntity>((Map<String, dynamic> e) =>
  202. PropertySaleRentEntity.fromJson(e)).toList() as M;
  203. }
  204. if (<PropertySaleRentList>[] is M) {
  205. return data.map<PropertySaleRentList>((Map<String, dynamic> e) =>
  206. PropertySaleRentList.fromJson(e)).toList() as M;
  207. }
  208. if (<ServerTime>[] is M) {
  209. return data.map<ServerTime>((Map<String, dynamic> e) =>
  210. ServerTime.fromJson(e)).toList() as M;
  211. }
  212. if (<UserMeEntity>[] is M) {
  213. return data.map<UserMeEntity>((Map<String, dynamic> e) =>
  214. UserMeEntity.fromJson(e)).toList() as M;
  215. }
  216. if (<UserMeHouseholds>[] is M) {
  217. return data.map<UserMeHouseholds>((Map<String, dynamic> e) =>
  218. UserMeHouseholds.fromJson(e)).toList() as M;
  219. }
  220. if (<UserMeEstates>[] is M) {
  221. return data.map<UserMeEstates>((Map<String, dynamic> e) =>
  222. UserMeEstates.fromJson(e)).toList() as M;
  223. }
  224. if (<UserMeEstatesAccounts>[] is M) {
  225. return data.map<UserMeEstatesAccounts>((Map<String, dynamic> e) =>
  226. UserMeEstatesAccounts.fromJson(e)).toList() as M;
  227. }
  228. if (<UserMeEstatesAccountsUnit>[] is M) {
  229. return data.map<UserMeEstatesAccountsUnit>((Map<String, dynamic> e) =>
  230. UserMeEstatesAccountsUnit.fromJson(e)).toList() as M;
  231. }
  232. if (<UserMeDefaultUnit>[] is M) {
  233. return data.map<UserMeDefaultUnit>((Map<String, dynamic> e) =>
  234. UserMeDefaultUnit.fromJson(e)).toList() as M;
  235. }
  236. debugPrint("$M not found");
  237. return null;
  238. }
  239. static M? fromJsonAsT<M>(dynamic json) {
  240. if (json is M) {
  241. return json;
  242. }
  243. if (json is List) {
  244. return _getListChildType<M>(
  245. json.map((dynamic e) => e as Map<String, dynamic>).toList());
  246. } else {
  247. return jsonConvert.convert<M>(json);
  248. }
  249. }
  250. }
  251. class JsonConvertClassCollection {
  252. Map<String, JsonConvertFunction> convertFuncMap = {
  253. (AuthLoginEntity).toString(): AuthLoginEntity.fromJson,
  254. (CaptchaImgEntity).toString(): CaptchaImgEntity.fromJson,
  255. (NewsfeedCommentPublishEntity).toString(): NewsfeedCommentPublishEntity
  256. .fromJson,
  257. (NewsfeedFollowingEntity).toString(): NewsfeedFollowingEntity.fromJson,
  258. (NewsfeedFollowingList).toString(): NewsfeedFollowingList.fromJson,
  259. (NewsfeedFollowingListAccount).toString(): NewsfeedFollowingListAccount
  260. .fromJson,
  261. (NewsfeedForyouEntity).toString(): NewsfeedForyouEntity.fromJson,
  262. (NewsfeedForyouList).toString(): NewsfeedForyouList.fromJson,
  263. (NewsfeedForyouListAccount).toString(): NewsfeedForyouListAccount.fromJson,
  264. (NewsfeedNewsEntity).toString(): NewsfeedNewsEntity.fromJson,
  265. (NewsfeedNewsList).toString(): NewsfeedNewsList.fromJson,
  266. (NewsfeedNewsListAccount).toString(): NewsfeedNewsListAccount.fromJson,
  267. (PropertyNewsEntity).toString(): PropertyNewsEntity.fromJson,
  268. (PropertyNewsList).toString(): PropertyNewsList.fromJson,
  269. (PropertySaleRentEntity).toString(): PropertySaleRentEntity.fromJson,
  270. (PropertySaleRentList).toString(): PropertySaleRentList.fromJson,
  271. (ServerTime).toString(): ServerTime.fromJson,
  272. (UserMeEntity).toString(): UserMeEntity.fromJson,
  273. (UserMeHouseholds).toString(): UserMeHouseholds.fromJson,
  274. (UserMeEstates).toString(): UserMeEstates.fromJson,
  275. (UserMeEstatesAccounts).toString(): UserMeEstatesAccounts.fromJson,
  276. (UserMeEstatesAccountsUnit).toString(): UserMeEstatesAccountsUnit.fromJson,
  277. (UserMeDefaultUnit).toString(): UserMeDefaultUnit.fromJson,
  278. };
  279. bool containsKey(String type) {
  280. return convertFuncMap.containsKey(type);
  281. }
  282. JsonConvertFunction? operator [](String key) {
  283. return convertFuncMap[key];
  284. }
  285. }