json_convert_content.dart 13 KB

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