123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- // ignore_for_file: non_constant_identifier_names
- // ignore_for_file: camel_case_types
- // ignore_for_file: prefer_single_quotes
- // This file is automatically generated. DO NOT EDIT, all your changes would be lost.
- import 'package:flutter/material.dart' show debugPrint;
- import 'package:hengyi/data/base_model_entity.dart';
- import 'package:hengyi/data/continent_entity.dart';
- import 'package:hengyi/data/country_detail_entity.dart';
- import 'package:hengyi/data/country_entity.dart';
- JsonConvert jsonConvert = JsonConvert();
- typedef JsonConvertFunction<T> = T Function(Map<String, dynamic> json);
- typedef EnumConvertFunction<T> = T Function(String value);
- class JsonConvert {
- static final Map<String, JsonConvertFunction> convertFuncMap = {
- (BaseModelEntity).toString(): BaseModelEntity.fromJson,
- (ContinentEntity).toString(): ContinentEntity.fromJson,
- (CountryDetailEntity).toString(): CountryDetailEntity.fromJson,
- (CountryEntity).toString(): CountryEntity.fromJson,
- };
- T? convert<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
- if (value == null) {
- return null;
- }
- if (value is T) {
- return value;
- }
- try {
- return _asT<T>(value, enumConvert: enumConvert);
- } catch (e, stackTrace) {
- debugPrint('asT<$T> $e $stackTrace');
- return null;
- }
- }
- List<T?>? convertList<T>(List<dynamic>? value, {EnumConvertFunction? enumConvert}) {
- if (value == null) {
- return null;
- }
- try {
- return value.map((dynamic e) => _asT<T>(e,enumConvert: enumConvert)).toList();
- } catch (e, stackTrace) {
- debugPrint('asT<$T> $e $stackTrace');
- return <T>[];
- }
- }
- List<T>? convertListNotNull<T>(dynamic value, {EnumConvertFunction? enumConvert}) {
- if (value == null) {
- return null;
- }
- try {
- return (value as List<dynamic>).map((dynamic e) => _asT<T>(e,enumConvert: enumConvert)!).toList();
- } catch (e, stackTrace) {
- debugPrint('asT<$T> $e $stackTrace');
- return <T>[];
- }
- }
- T? _asT<T extends Object?>(dynamic value,
- {EnumConvertFunction? enumConvert}) {
- final String type = T.toString();
- final String valueS = value.toString();
- if (enumConvert != null) {
- return enumConvert(valueS) as T;
- } else if (type == "String") {
- return valueS as T;
- } else if (type == "int") {
- final int? intValue = int.tryParse(valueS);
- if (intValue == null) {
- return double.tryParse(valueS)?.toInt() as T?;
- } else {
- return intValue as T;
- }
- } else if (type == "double") {
- return double.parse(valueS) as T;
- } else if (type == "DateTime") {
- return DateTime.parse(valueS) as T;
- } else if (type == "bool") {
- if (valueS == '0' || valueS == '1') {
- return (valueS == '1') as T;
- }
- return (valueS == 'true') as T;
- } else if (type == "Map" || type.startsWith("Map<")) {
- return value as T;
- } else {
- if (convertFuncMap.containsKey(type)) {
- return convertFuncMap[type]!(Map<String, dynamic>.from(value)) as T;
- } else {
- throw UnimplementedError('$type unimplemented');
- }
- }
- }
- //list is returned by type
- static M? _getListChildType<M>(List<Map<String, dynamic>> data) {
- if(<BaseModelEntity>[] is M){
- return data.map<BaseModelEntity>((Map<String, dynamic> e) => BaseModelEntity.fromJson(e)).toList() as M;
- }
- if(<ContinentEntity>[] is M){
- return data.map<ContinentEntity>((Map<String, dynamic> e) => ContinentEntity.fromJson(e)).toList() as M;
- }
- if(<CountryDetailEntity>[] is M){
- return data.map<CountryDetailEntity>((Map<String, dynamic> e) => CountryDetailEntity.fromJson(e)).toList() as M;
- }
- if(<CountryEntity>[] is M){
- return data.map<CountryEntity>((Map<String, dynamic> e) => CountryEntity.fromJson(e)).toList() as M;
- }
- debugPrint("${M.toString()} not found");
-
- return null;
- }
- static M? fromJsonAsT<M>(dynamic json) {
- if (json is List) {
- return _getListChildType<M>(json.map((e) => e as Map<String, dynamic>).toList());
- } else {
- return jsonConvert.convert<M>(json);
- }
- }
- }
|