json_convert_content.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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, {EnumConvertFunction? enumConvert}) {
  59. if (value == null) {
  60. return null;
  61. }
  62. try {
  63. return value.map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)).toList();
  64. } catch (e, stackTrace) {
  65. debugPrint('asT<$T> $e $stackTrace');
  66. if (onError != null) {
  67. onError!(e, stackTrace);
  68. }
  69. return <T>[];
  70. }
  71. }
  72. List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  73. if (value == null) {
  74. return null;
  75. }
  76. try {
  77. return (value as List<dynamic>).map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)!).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. T? _asT<T extends Object?>(dynamic value,
  87. {EnumConvertFunction? enumConvert}) {
  88. final String type = T.toString();
  89. final String valueS = value.toString();
  90. if (enumConvert != null) {
  91. return enumConvert(valueS) as T;
  92. } else if (type == "String") {
  93. return valueS as T;
  94. } else if (type == "int") {
  95. final int? intValue = int.tryParse(valueS);
  96. if (intValue == null) {
  97. return double.tryParse(valueS)?.toInt() as T?;
  98. } else {
  99. return intValue as T;
  100. }
  101. } else if (type == "double") {
  102. return double.parse(valueS) as T;
  103. } else if (type == "DateTime") {
  104. return DateTime.parse(valueS) as T;
  105. } else if (type == "bool") {
  106. if (valueS == '0' || valueS == '1') {
  107. return (valueS == '1') as T;
  108. }
  109. return (valueS == 'true') as T;
  110. } else if (type == "Map" || type.startsWith("Map<")) {
  111. return value as T;
  112. } else {
  113. if (convertFuncMap.containsKey(type)) {
  114. if (value == null) {
  115. return null;
  116. }
  117. var covertFunc = convertFuncMap[type]!;
  118. if (covertFunc is Map<String, dynamic>) {
  119. return covertFunc(value as Map<String, dynamic>) as T;
  120. } else {
  121. return covertFunc(Map<String, dynamic>.from(value)) as T;
  122. }
  123. } else {
  124. throw UnimplementedError('$type unimplemented,you can try running the app again');
  125. }
  126. }
  127. }
  128. //list is returned by type
  129. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  130. if (<ServerTime>[] is M) {
  131. return data.map<ServerTime>((Map<String, dynamic> e) => ServerTime.fromJson(e)).toList() as M;
  132. }
  133. debugPrint("$M not found");
  134. return null;
  135. }
  136. static M? fromJsonAsT<M>(dynamic json) {
  137. if (json is M) {
  138. return json;
  139. }
  140. if (json is List) {
  141. return _getListChildType<M>(json.map((dynamic e) => e as Map<String, dynamic>).toList());
  142. } else {
  143. return jsonConvert.convert<M>(json);
  144. }
  145. }
  146. }
  147. class JsonConvertClassCollection {
  148. Map<String, JsonConvertFunction> convertFuncMap = {
  149. (ServerTime).toString(): ServerTime.fromJson,
  150. };
  151. bool containsKey(String type) {
  152. return convertFuncMap.containsKey(type);
  153. }
  154. JsonConvertFunction? operator [](String key) {
  155. return convertFuncMap[key];
  156. }
  157. }