json_convert_content.dart 4.0 KB

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