json_convert_content.dart 14 KB

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