json_convert_content.dart 4.5 KB

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