json_convert_content.dart 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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:fuwei/data/base_model_entity.dart';
  7. import 'package:fuwei/data/continent_entity.dart';
  8. import 'package:fuwei/data/country_detail_entity.dart';
  9. import 'package:fuwei/data/country_entity.dart';
  10. import 'package:fuwei/data/web_config_entity.dart';
  11. JsonConvert jsonConvert = JsonConvert();
  12. typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
  13. typedef EnumConvertFunction<T> = T Function(String value);
  14. class JsonConvert {
  15. static final Map<String, JsonConvertFunction> convertFuncMap = {
  16. (BaseModelEntity).toString(): BaseModelEntity.fromJson,
  17. (ContinentEntity).toString(): ContinentEntity.fromJson,
  18. (CountryDetailEntity).toString(): CountryDetailEntity.fromJson,
  19. (CountryDetailCountryVisitVisaUrl).toString():
  20. CountryDetailCountryVisitVisaUrl.fromJson,
  21. (CountryEntity).toString(): CountryEntity.fromJson,
  22. (WebConfigEntity).toString(): WebConfigEntity.fromJson,
  23. };
  24. T? convert<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
  25. if (value == null) {
  26. return null;
  27. }
  28. if (value is T) {
  29. return value;
  30. }
  31. try {
  32. return _asT<T>(value, enumConvert: enumConvert);
  33. } catch (e, stackTrace) {
  34. debugPrint('asT<$T> $e $stackTrace');
  35. return null;
  36. }
  37. }
  38. List<T?>? convertList<T>(List<dynamic>? value,
  39. {EnumConvertFunction? enumConvert}) {
  40. if (value == null) {
  41. return null;
  42. }
  43. try {
  44. return value
  45. .map((dynamic e) => _asT<T>(e, enumConvert: enumConvert))
  46. .toList();
  47. } catch (e, stackTrace) {
  48. debugPrint('asT<$T> $e $stackTrace');
  49. return <T>[];
  50. }
  51. }
  52. List<T>? convertListNotNull<T>(dynamic value,
  53. {EnumConvertFunction? enumConvert}) {
  54. if (value == null) {
  55. return null;
  56. }
  57. try {
  58. return (value as List<dynamic>)
  59. .map((dynamic e) => _asT<T>(e, enumConvert: enumConvert)!)
  60. .toList();
  61. } catch (e, stackTrace) {
  62. debugPrint('asT<$T> $e $stackTrace');
  63. return <T>[];
  64. }
  65. }
  66. T? _asT<T extends Object?>(dynamic value,
  67. {EnumConvertFunction? enumConvert}) {
  68. final String type = T.toString();
  69. final String valueS = value.toString();
  70. if (enumConvert != null) {
  71. return enumConvert(valueS) as T;
  72. } else if (type == "String") {
  73. return valueS as T;
  74. } else if (type == "int") {
  75. final int? intValue = int.tryParse(valueS);
  76. if (intValue == null) {
  77. return double.tryParse(valueS)?.toInt() as T?;
  78. } else {
  79. return intValue as T;
  80. }
  81. } else if (type == "double") {
  82. return double.parse(valueS) as T;
  83. } else if (type == "DateTime") {
  84. return DateTime.parse(valueS) as T;
  85. } else if (type == "bool") {
  86. if (valueS == '0' || valueS == '1') {
  87. return (valueS == '1') as T;
  88. }
  89. return (valueS == 'true') as T;
  90. } else if (type == "Map" || type.startsWith("Map<")) {
  91. return value as T;
  92. } else {
  93. if (convertFuncMap.containsKey(type)) {
  94. return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T;
  95. } else {
  96. throw UnimplementedError('$type unimplemented');
  97. }
  98. }
  99. }
  100. //list is returned by type
  101. static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
  102. if (<BaseModelEntity>[] is M) {
  103. return data
  104. .map<BaseModelEntity>(
  105. (Map<String, dynamic> e) => BaseModelEntity.fromJson(e))
  106. .toList() as M;
  107. }
  108. if (<ContinentEntity>[] is M) {
  109. return data
  110. .map<ContinentEntity>(
  111. (Map<String, dynamic> e) => ContinentEntity.fromJson(e))
  112. .toList() as M;
  113. }
  114. if (<CountryDetailEntity>[] is M) {
  115. return data
  116. .map<CountryDetailEntity>(
  117. (Map<String, dynamic> e) => CountryDetailEntity.fromJson(e))
  118. .toList() as M;
  119. }
  120. if (<CountryDetailCountryVisitVisaUrl>[] is M) {
  121. return data
  122. .map<CountryDetailCountryVisitVisaUrl>((Map<String, dynamic> e) =>
  123. CountryDetailCountryVisitVisaUrl.fromJson(e))
  124. .toList() as M;
  125. }
  126. if (<CountryEntity>[] is M) {
  127. return data
  128. .map<CountryEntity>(
  129. (Map<String, dynamic> e) => CountryEntity.fromJson(e))
  130. .toList() as M;
  131. }
  132. if (<WebConfigEntity>[] is M) {
  133. return data
  134. .map<WebConfigEntity>(
  135. (Map<String, dynamic> e) => WebConfigEntity.fromJson(e))
  136. .toList() as M;
  137. }
  138. debugPrint("${M.toString()} not found");
  139. return null;
  140. }
  141. static M? fromJsonAsT<M>(dynamic json) {
  142. if (json is List) {
  143. return _getListChildType<M>(
  144. json.map((e) => e as Map<String, dynamic>).toList());
  145. } else {
  146. return jsonConvert.convert<M>(json);
  147. }
  148. }
  149. }