json_convert_content.dart 5.2 KB

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