json_convert_content.dart 14 KB

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