json_convert_content.dart 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/response/id_name_entity.dart';
  7. import 'package:domain/entity/response/user_profile.dart';
  8. import 'package:domain/entity/response/verify_code.dart';
  9. import 'package:domain/entity/server_time.dart';
  10. JsonConvert jsonConvert = JsonConvert();
  11. typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
  12. typedef EnumConvertFunction<T> = T Function(String value);
  13. typedef ConvertExceptionHandler = void Function(Object error, StackTrace stackTrace);
  14. extension MapSafeExt<K, V> on Map<K, V> {
  15. T? getOrNull<T>(K? key) {
  16. if (!containsKey(key) || key == null) {
  17. return null;
  18. } else {
  19. return this[key] as T?;
  20. }
  21. }
  22. }
  23. class JsonConvert {
  24. static ConvertExceptionHandler? onError;
  25. JsonConvertClassCollection convertFuncMap = JsonConvertClassCollection();
  26. /// 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
  27. /// https://flutter.cn/docs/development/tools/hot-reload
  28. /// class MyApp extends StatelessWidget {
  29. /// const MyApp({Key? key})
  30. /// : super(key: key);
  31. ///
  32. /// @override
  33. /// Widget build(BuildContext context) {
  34. /// jsonConvert.reassembleConvertFuncMap();
  35. /// return MaterialApp();
  36. /// }
  37. /// }
  38. void reassembleConvertFuncMap() {
  39. bool isReleaseMode = const bool.fromEnvironment('dart.vm.product');
  40. if (!isReleaseMode) {
  41. convertFuncMap = JsonConvertClassCollection();
  42. }
  43. }
  44. T? convert<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  45. if (value == null) {
  46. return null;
  47. }
  48. if (value is T) {
  49. return value;
  50. }
  51. try {
  52. return _asT<T>(value, enumConvert: enumConvert);
  53. } catch (e, stackTrace) {
  54. debugPrint('asT<$T> $e $stackTrace');
  55. if (onError != null) {
  56. onError!(e, stackTrace);
  57. }
  58. return null;
  59. }
  60. }
  61. List<T?>? convertList<T>(List<dynamic>? value, {EnumConvertFunction? enumConvert}) {
  62. if (value == null) {
  63. return null;
  64. }
  65. try {
  66. return value.map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)).toList();
  67. } catch (e, stackTrace) {
  68. debugPrint('asT<$T> $e $stackTrace');
  69. if (onError != null) {
  70. onError!(e, stackTrace);
  71. }
  72. return <T>[];
  73. }
  74. }
  75. List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  76. if (value == null) {
  77. return null;
  78. }
  79. try {
  80. return (value as List<dynamic>).map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)!).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. T? _asT<T extends Object?>(dynamic value,
  90. {EnumConvertFunction? enumConvert}) {
  91. final String type = T.toString();
  92. final String valueS = value.toString();
  93. if (enumConvert != null) {
  94. return enumConvert(valueS) as T;
  95. } else if (type == "String") {
  96. return valueS as T;
  97. } else if (type == "int") {
  98. final int? intValue = int.tryParse(valueS);
  99. if (intValue == null) {
  100. return double.tryParse(valueS)?.toInt() as T?;
  101. } else {
  102. return intValue as T;
  103. }
  104. } else if (type == "double") {
  105. return double.parse(valueS) as T;
  106. } else if (type == "DateTime") {
  107. return DateTime.parse(valueS) as T;
  108. } else if (type == "bool") {
  109. if (valueS == '0' || valueS == '1') {
  110. return (valueS == '1') as T;
  111. }
  112. return (valueS == 'true') as T;
  113. } else if (type == "Map" || type.startsWith("Map<")) {
  114. return value as T;
  115. } else {
  116. if (convertFuncMap.containsKey(type)) {
  117. if (value == null) {
  118. return null;
  119. }
  120. return convertFuncMap[type]!(value as Map<String, dynamic>) as T;
  121. } else {
  122. throw UnimplementedError('$type unimplemented,you can try running the app again');
  123. }
  124. }
  125. }
  126. //list is returned by type
  127. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  128. if (<IdNameEntity>[] is M) {
  129. return data.map<IdNameEntity>((Map<String, dynamic> e) => IdNameEntity.fromJson(e)).toList() as M;
  130. }
  131. if (<UserProfile>[] is M) {
  132. return data.map<UserProfile>((Map<String, dynamic> e) => UserProfile.fromJson(e)).toList() as M;
  133. }
  134. if (<VerifyCode>[] is M) {
  135. return data.map<VerifyCode>((Map<String, dynamic> e) => VerifyCode.fromJson(e)).toList() as M;
  136. }
  137. if (<ServerTime>[] is M) {
  138. return data.map<ServerTime>((Map<String, dynamic> e) => ServerTime.fromJson(e)).toList() as M;
  139. }
  140. debugPrint("$M not found");
  141. return null;
  142. }
  143. static M? fromJsonAsT<M>(dynamic json) {
  144. if (json is M) {
  145. return json;
  146. }
  147. if (json is List) {
  148. return _getListChildType<M>(json.map((dynamic e) => e as Map<String, dynamic>).toList());
  149. } else {
  150. return jsonConvert.convert<M>(json);
  151. }
  152. }
  153. }
  154. class JsonConvertClassCollection {
  155. Map<String, JsonConvertFunction> convertFuncMap = {
  156. (IdNameEntity).toString(): IdNameEntity.fromJson,
  157. (UserProfile).toString(): UserProfile.fromJson,
  158. (VerifyCode).toString(): VerifyCode.fromJson,
  159. (ServerTime).toString(): ServerTime.fromJson,
  160. };
  161. bool containsKey(String type) {
  162. return convertFuncMap.containsKey(type);
  163. }
  164. JsonConvertFunction? operator [](String key) {
  165. return convertFuncMap[key];
  166. }
  167. }