json_convert_content.dart 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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/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 (<FeedbackDetailEntity>[] is M) {
  156. return data.map<FeedbackDetailEntity>((Map<String, dynamic> e) => FeedbackDetailEntity.fromJson(e)).toList() as M;
  157. }
  158. if (<FeedbackDetailReplies>[] is M) {
  159. return data.map<FeedbackDetailReplies>((Map<String, dynamic> e) => FeedbackDetailReplies.fromJson(e)).toList() as M;
  160. }
  161. if (<FeedbackListEntity>[] is M) {
  162. return data.map<FeedbackListEntity>((Map<String, dynamic> e) => FeedbackListEntity.fromJson(e)).toList() as M;
  163. }
  164. if (<FeedbackItemEntity>[] is M) {
  165. return data.map<FeedbackItemEntity>((Map<String, dynamic> e) => FeedbackItemEntity.fromJson(e)).toList() as M;
  166. }
  167. if (<IdNameEntity>[] is M) {
  168. return data.map<IdNameEntity>((Map<String, dynamic> e) =>
  169. IdNameEntity.fromJson(e)).toList() as M;
  170. }
  171. if (<NewsfeedCommentPublishEntity>[] is M) {
  172. return data.map<NewsfeedCommentPublishEntity>((Map<String, dynamic> e) =>
  173. NewsfeedCommentPublishEntity.fromJson(e)).toList() as M;
  174. }
  175. if (<NewsfeedFollowingEntity>[] is M) {
  176. return data.map<NewsfeedFollowingEntity>((Map<String, dynamic> e) =>
  177. NewsfeedFollowingEntity.fromJson(e)).toList() as M;
  178. }
  179. if (<NewsfeedFollowingList>[] is M) {
  180. return data.map<NewsfeedFollowingList>((Map<String, dynamic> e) =>
  181. NewsfeedFollowingList.fromJson(e)).toList() as M;
  182. }
  183. if (<NewsfeedFollowingListAccount>[] is M) {
  184. return data.map<NewsfeedFollowingListAccount>((Map<String, dynamic> e) =>
  185. NewsfeedFollowingListAccount.fromJson(e)).toList() as M;
  186. }
  187. if (<NewsfeedForyouEntity>[] is M) {
  188. return data.map<NewsfeedForyouEntity>((Map<String, dynamic> e) =>
  189. NewsfeedForyouEntity.fromJson(e)).toList() as M;
  190. }
  191. if (<NewsfeedForyouList>[] is M) {
  192. return data.map<NewsfeedForyouList>((Map<String, dynamic> e) =>
  193. NewsfeedForyouList.fromJson(e)).toList() as M;
  194. }
  195. if (<NewsfeedForyouListAccount>[] is M) {
  196. return data.map<NewsfeedForyouListAccount>((Map<String, dynamic> e) =>
  197. NewsfeedForyouListAccount.fromJson(e)).toList() as M;
  198. }
  199. if (<NewsfeedNewsEntity>[] is M) {
  200. return data.map<NewsfeedNewsEntity>((Map<String, dynamic> e) =>
  201. NewsfeedNewsEntity.fromJson(e)).toList() as M;
  202. }
  203. if (<NewsfeedNewsList>[] is M) {
  204. return data.map<NewsfeedNewsList>((Map<String, dynamic> e) =>
  205. NewsfeedNewsList.fromJson(e)).toList() as M;
  206. }
  207. if (<NewsfeedNewsListAccount>[] is M) {
  208. return data.map<NewsfeedNewsListAccount>((Map<String, dynamic> e) =>
  209. NewsfeedNewsListAccount.fromJson(e)).toList() as M;
  210. }
  211. if (<PropertyNewsEntity>[] is M) {
  212. return data.map<PropertyNewsEntity>((Map<String, dynamic> e) =>
  213. PropertyNewsEntity.fromJson(e)).toList() as M;
  214. }
  215. if (<PropertyNewsList>[] is M) {
  216. return data.map<PropertyNewsList>((Map<String, dynamic> e) =>
  217. PropertyNewsList.fromJson(e)).toList() as M;
  218. }
  219. if (<PropertySaleRentEntity>[] is M) {
  220. return data.map<PropertySaleRentEntity>((Map<String, dynamic> e) =>
  221. PropertySaleRentEntity.fromJson(e)).toList() as M;
  222. }
  223. if (<PropertySaleRentList>[] is M) {
  224. return data.map<PropertySaleRentList>((Map<String, dynamic> e) =>
  225. PropertySaleRentList.fromJson(e)).toList() as M;
  226. }
  227. if (<ServerTime>[] is M) {
  228. return data.map<ServerTime>((Map<String, dynamic> e) =>
  229. ServerTime.fromJson(e)).toList() as M;
  230. }
  231. if (<UserMeEntity>[] is M) {
  232. return data.map<UserMeEntity>((Map<String, dynamic> e) =>
  233. UserMeEntity.fromJson(e)).toList() as M;
  234. }
  235. if (<UserMeHouseholds>[] is M) {
  236. return data.map<UserMeHouseholds>((Map<String, dynamic> e) =>
  237. UserMeHouseholds.fromJson(e)).toList() as M;
  238. }
  239. if (<UserMeEstates>[] is M) {
  240. return data.map<UserMeEstates>((Map<String, dynamic> e) =>
  241. UserMeEstates.fromJson(e)).toList() as M;
  242. }
  243. if (<UserMeEstatesAccounts>[] is M) {
  244. return data.map<UserMeEstatesAccounts>((Map<String, dynamic> e) =>
  245. UserMeEstatesAccounts.fromJson(e)).toList() as M;
  246. }
  247. if (<UserMeEstatesAccountsUnit>[] is M) {
  248. return data.map<UserMeEstatesAccountsUnit>((Map<String, dynamic> e) =>
  249. UserMeEstatesAccountsUnit.fromJson(e)).toList() as M;
  250. }
  251. if (<UserMeDefaultUnit>[] is M) {
  252. return data.map<UserMeDefaultUnit>((Map<String, dynamic> e) =>
  253. UserMeDefaultUnit.fromJson(e)).toList() as M;
  254. }
  255. if (<UserMeInformation>[] is M) {
  256. return data.map<UserMeInformation>((Map<String, dynamic> e) =>
  257. UserMeInformation.fromJson(e)).toList() as M;
  258. }
  259. debugPrint("$M not found");
  260. return null;
  261. }
  262. static M? fromJsonAsT<M>(dynamic json) {
  263. if (json is M) {
  264. return json;
  265. }
  266. if (json is List) {
  267. return _getListChildType<M>(
  268. json.map((dynamic e) => e as Map<String, dynamic>).toList());
  269. } else {
  270. return jsonConvert.convert<M>(json);
  271. }
  272. }
  273. }
  274. class JsonConvertClassCollection {
  275. Map<String, JsonConvertFunction> convertFuncMap = {
  276. (AuthLoginEntity).toString(): AuthLoginEntity.fromJson,
  277. (CaptchaImgEntity).toString(): CaptchaImgEntity.fromJson,
  278. (FeedbackDetailEntity).toString(): FeedbackDetailEntity.fromJson,
  279. (FeedbackDetailReplies).toString(): FeedbackDetailReplies.fromJson,
  280. (FeedbackListEntity).toString(): FeedbackListEntity.fromJson,
  281. (FeedbackItemEntity).toString(): FeedbackItemEntity.fromJson,
  282. (IdNameEntity).toString(): IdNameEntity.fromJson,
  283. (NewsfeedCommentPublishEntity).toString(): NewsfeedCommentPublishEntity
  284. .fromJson,
  285. (NewsfeedFollowingEntity).toString(): NewsfeedFollowingEntity.fromJson,
  286. (NewsfeedFollowingList).toString(): NewsfeedFollowingList.fromJson,
  287. (NewsfeedFollowingListAccount).toString(): NewsfeedFollowingListAccount
  288. .fromJson,
  289. (NewsfeedForyouEntity).toString(): NewsfeedForyouEntity.fromJson,
  290. (NewsfeedForyouList).toString(): NewsfeedForyouList.fromJson,
  291. (NewsfeedForyouListAccount).toString(): NewsfeedForyouListAccount.fromJson,
  292. (NewsfeedNewsEntity).toString(): NewsfeedNewsEntity.fromJson,
  293. (NewsfeedNewsList).toString(): NewsfeedNewsList.fromJson,
  294. (NewsfeedNewsListAccount).toString(): NewsfeedNewsListAccount.fromJson,
  295. (PropertyNewsEntity).toString(): PropertyNewsEntity.fromJson,
  296. (PropertyNewsList).toString(): PropertyNewsList.fromJson,
  297. (PropertySaleRentEntity).toString(): PropertySaleRentEntity.fromJson,
  298. (PropertySaleRentList).toString(): PropertySaleRentList.fromJson,
  299. (ServerTime).toString(): ServerTime.fromJson,
  300. (UserMeEntity).toString(): UserMeEntity.fromJson,
  301. (UserMeHouseholds).toString(): UserMeHouseholds.fromJson,
  302. (UserMeEstates).toString(): UserMeEstates.fromJson,
  303. (UserMeEstatesAccounts).toString(): UserMeEstatesAccounts.fromJson,
  304. (UserMeEstatesAccountsUnit).toString(): UserMeEstatesAccountsUnit.fromJson,
  305. (UserMeDefaultUnit).toString(): UserMeDefaultUnit.fromJson,
  306. (UserMeInformation).toString(): UserMeInformation.fromJson,
  307. };
  308. bool containsKey(String type) {
  309. return convertFuncMap.containsKey(type);
  310. }
  311. JsonConvertFunction? operator [](String key) {
  312. return convertFuncMap[key];
  313. }
  314. }