json_convert_content.dart 11 KB

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