Skip to content

Commit

Permalink
fix #191 适配FlutterJsonBeanFactory插件4.2.8版本
Browse files Browse the repository at this point in the history
  • Loading branch information
simplezhli committed Dec 23, 2021
1 parent ec8fd73 commit 73f2ab5
Show file tree
Hide file tree
Showing 21 changed files with 873 additions and 668 deletions.
11 changes: 8 additions & 3 deletions lib/account/models/bank_entity.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@

import 'package:azlistview/azlistview.dart';
import 'package:flutter_deer/generated/json/base/json_convert_content.dart';
import 'package:flutter_deer/generated/json/bank_entity.g.dart';
import 'package:flutter_deer/generated/json/base/json_field.dart';

class BankEntity with JsonConvert<BankEntity>, ISuspensionBean {
@JsonSerializable()
class BankEntity with ISuspensionBean {

BankEntity({this.id, this.bankName, this.firstLetter});

factory BankEntity.fromJson(Map<String, dynamic> json) => $BankEntityFromJson(json);

Map<String, dynamic> toJson() => $BankEntityToJson(this);

int? id;
String? bankName;
String? firstLetter;
Expand Down
14 changes: 11 additions & 3 deletions lib/account/models/city_entity.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@

import 'package:azlistview/azlistview.dart';
import 'package:flutter_deer/generated/json/base/json_convert_content.dart';
import 'package:flutter_deer/generated/json/base/json_field.dart';
import 'package:flutter_deer/generated/json/city_entity.g.dart';

@JsonSerializable()
class CityEntity with ISuspensionBean {

CityEntity();

factory CityEntity.fromJson(Map<String, dynamic> json) => $CityEntityFromJson(json);

Map<String, dynamic> toJson() => $CityEntityToJson(this);

class CityEntity with JsonConvert<CityEntity>, ISuspensionBean {
late String name;
late String cityCode;
late String firstCharacter;
Expand Down
2 changes: 1 addition & 1 deletion lib/account/page/bank_select_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class _BankSelectPageState extends State<BankSelectPage> {
}

void _addBank(dynamic value) {
_bankList.add(BankEntity().fromJson(value as Map<String, dynamic>));
_bankList.add(BankEntity.fromJson(value as Map<String, dynamic>));
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/account/page/city_select_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class _CitySelectPageState extends State<CitySelectPage> {
}

void _addCity(dynamic value) {
_cityList.add(CityEntity().fromJson(value as Map<String, dynamic>));
_cityList.add(CityEntity.fromJson(value as Map<String, dynamic>));
}

/// rootBundle.loadString源码修改
Expand Down
29 changes: 29 additions & 0 deletions lib/generated/json/bank_entity.g.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:flutter_deer/generated/json/base/json_convert_content.dart';
import 'package:flutter_deer/account/models/bank_entity.dart';
import 'package:azlistview/azlistview.dart';


BankEntity $BankEntityFromJson(Map<String, dynamic> json) {
final BankEntity bankEntity = BankEntity();
final int? id = jsonConvert.convert<int>(json['id']);
if (id != null) {
bankEntity.id = id;
}
final String? bankName = jsonConvert.convert<String>(json['bankName']);
if (bankName != null) {
bankEntity.bankName = bankName;
}
final String? firstLetter = jsonConvert.convert<String>(json['firstLetter']);
if (firstLetter != null) {
bankEntity.firstLetter = firstLetter;
}
return bankEntity;
}

Map<String, dynamic> $BankEntityToJson(BankEntity entity) {
final Map<String, dynamic> data = <String, dynamic>{};
data['id'] = entity.id;
data['bankName'] = entity.bankName;
data['firstLetter'] = entity.firstLetter;
return data;
}
24 changes: 0 additions & 24 deletions lib/generated/json/bank_entity_helper.dart

This file was deleted.

234 changes: 136 additions & 98 deletions lib/generated/json/base/json_convert_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,117 +4,155 @@

// This file is automatically generated. DO NOT EDIT, all your changes would be lost.
import 'package:flutter_deer/account/models/bank_entity.dart';
import 'package:flutter_deer/generated/json/bank_entity_helper.dart';
import 'package:flutter_deer/shop/models/user_entity.dart';
import 'package:flutter_deer/generated/json/user_entity_helper.dart';
import 'package:flutter_deer/generated/json/bank_entity.g.dart';
import 'package:flutter_deer/account/models/city_entity.dart';
import 'package:flutter_deer/generated/json/city_entity_helper.dart';
import 'package:flutter_deer/generated/json/city_entity.g.dart';
import 'package:flutter_deer/goods/models/goods_sort_entity.dart';
import 'package:flutter_deer/generated/json/goods_sort_entity_helper.dart';
import 'package:flutter_deer/generated/json/goods_sort_entity.g.dart';
import 'package:flutter_deer/order/models/search_entity.dart';
import 'package:flutter_deer/generated/json/search_entity_helper.dart';
import 'package:flutter_deer/generated/json/search_entity.g.dart';
import 'package:flutter_deer/shop/models/user_entity.dart';
import 'package:flutter_deer/generated/json/user_entity.g.dart';

class JsonConvert<T> {
T fromJson(Map<String, dynamic> json) {
return _getFromJson<T>(runtimeType, this, json);
}
JsonConvert jsonConvert = JsonConvert();

Map<String, dynamic> toJson() {
return _getToJson<T>(runtimeType, this);
class JsonConvert {

T? convert<T>(dynamic value) {
if (value == null) {
return null;
}
return asT<T>(value);
}

static _getFromJson<T>(Type type, data, json) {
switch (type) {
case BankEntity:
return bankEntityFromJson(data as BankEntity, json) as T;
case UserEntity:
return userEntityFromJson(data as UserEntity, json) as T;
case CityEntity:
return cityEntityFromJson(data as CityEntity, json) as T;
case GoodsSortEntity:
return goodsSortEntityFromJson(data as GoodsSortEntity, json) as T;
case SearchEntity:
return searchEntityFromJson(data as SearchEntity, json) as T;
case SearchItems:
return searchItemsFromJson(data as SearchItems, json) as T;
case SearchItemsOwner:
return searchItemsOwnerFromJson(data as SearchItemsOwner, json) as T;
case SearchItemsLicense:
return searchItemsLicenseFromJson(data as SearchItemsLicense, json) as T; }
return data as T;
List<T?>? convertList<T>(List<dynamic>? value) {
if (value == null) {
return null;
}
try {
return value.map((dynamic e) => asT<T>(e)).toList();
} catch (e, stackTrace) {
print('asT<$T> $e $stackTrace');
return <T>[];
}
}

static _getToJson<T>(Type type, data) {
switch (type) {
case BankEntity:
return bankEntityToJson(data as BankEntity);
case UserEntity:
return userEntityToJson(data as UserEntity);
case CityEntity:
return cityEntityToJson(data as CityEntity);
case GoodsSortEntity:
return goodsSortEntityToJson(data as GoodsSortEntity);
case SearchEntity:
return searchEntityToJson(data as SearchEntity);
case SearchItems:
return searchItemsToJson(data as SearchItems);
case SearchItemsOwner:
return searchItemsOwnerToJson(data as SearchItemsOwner);
case SearchItemsLicense:
return searchItemsLicenseToJson(data as SearchItemsLicense);
}
return data as T;
}
//Go back to a single instance by type
static _fromJsonSingle<M>( json) {
String type = M.toString();
List<T>? convertListNotNull<T>(dynamic value) {
if (value == null) {
return null;
}
try {
return (value as List<dynamic>).map((dynamic e) => asT<T>(e)!).toList();
} catch (e, stackTrace) {
print('asT<$T> $e $stackTrace');
return <T>[];
}
}
T? asT<T extends Object?>(dynamic value) {
if (value is T) {
return value;
}
final String type = T.toString();
try {
final String valueS = value.toString();
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 {
return JsonConvert.fromJsonAsT<T>(value);
}
} catch (e, stackTrace) {
print('asT<$T> $e $stackTrace');
return null;
}
}
//Go back to a single instance by type
static M? _fromJsonSingle<M>(Map<String, dynamic> json) {
final String type = M.toString();
if(type == (BankEntity).toString()){
return BankEntity().fromJson(json);
} else if(type == (UserEntity).toString()){
return UserEntity().fromJson(json);
} else if(type == (CityEntity).toString()){
return CityEntity().fromJson(json);
} else if(type == (GoodsSortEntity).toString()){
return GoodsSortEntity().fromJson(json);
} else if(type == (SearchEntity).toString()){
return SearchEntity().fromJson(json);
} else if(type == (SearchItems).toString()){
return SearchItems().fromJson(json);
} else if(type == (SearchItemsOwner).toString()){
return SearchItemsOwner().fromJson(json);
} else if(type == (SearchItemsLicense).toString()){
return SearchItemsLicense().fromJson(json);
}
return BankEntity.fromJson(json) as M;
}
if(type == (CityEntity).toString()){
return CityEntity.fromJson(json) as M;
}
if(type == (GoodsSortEntity).toString()){
return GoodsSortEntity.fromJson(json) as M;
}
if(type == (SearchEntity).toString()){
return SearchEntity.fromJson(json) as M;
}
if(type == (SearchItems).toString()){
return SearchItems.fromJson(json) as M;
}
if(type == (SearchItemsOwner).toString()){
return SearchItemsOwner.fromJson(json) as M;
}
if(type == (SearchItemsLicense).toString()){
return SearchItemsLicense.fromJson(json) as M;
}
if(type == (UserEntity).toString()){
return UserEntity.fromJson(json) as M;
}

print("$type not found");

return null;
}
}

//list is returned by type
static M _getListChildType<M>(List data) {
static M? _getListChildType<M>(List<dynamic> data) {
if(<BankEntity>[] is M){
return data.map<BankEntity>((e) => BankEntity().fromJson(e)).toList() as M;
} else if(<UserEntity>[] is M){
return data.map<UserEntity>((e) => UserEntity().fromJson(e)).toList() as M;
} else if(<CityEntity>[] is M){
return data.map<CityEntity>((e) => CityEntity().fromJson(e)).toList() as M;
} else if(<GoodsSortEntity>[] is M){
return data.map<GoodsSortEntity>((e) => GoodsSortEntity().fromJson(e)).toList() as M;
} else if(<SearchEntity>[] is M){
return data.map<SearchEntity>((e) => SearchEntity().fromJson(e)).toList() as M;
} else if(<SearchItems>[] is M){
return data.map<SearchItems>((e) => SearchItems().fromJson(e)).toList() as M;
} else if(<SearchItemsOwner>[] is M){
return data.map<SearchItemsOwner>((e) => SearchItemsOwner().fromJson(e)).toList() as M;
} else if(<SearchItemsLicense>[] is M){
return data.map<SearchItemsLicense>((e) => SearchItemsLicense().fromJson(e)).toList() as M;
}
throw Exception("not fond");
}
return data.map<BankEntity>((e) => BankEntity.fromJson(e)).toList() as M;
}
if(<CityEntity>[] is M){
return data.map<CityEntity>((e) => CityEntity.fromJson(e)).toList() as M;
}
if(<GoodsSortEntity>[] is M){
return data.map<GoodsSortEntity>((e) => GoodsSortEntity.fromJson(e)).toList() as M;
}
if(<SearchEntity>[] is M){
return data.map<SearchEntity>((e) => SearchEntity.fromJson(e)).toList() as M;
}
if(<SearchItems>[] is M){
return data.map<SearchItems>((e) => SearchItems.fromJson(e)).toList() as M;
}
if(<SearchItemsOwner>[] is M){
return data.map<SearchItemsOwner>((e) => SearchItemsOwner.fromJson(e)).toList() as M;
}
if(<SearchItemsLicense>[] is M){
return data.map<SearchItemsLicense>((e) => SearchItemsLicense.fromJson(e)).toList() as M;
}
if(<UserEntity>[] is M){
return data.map<UserEntity>((e) => UserEntity.fromJson(e)).toList() as M;
}

static M fromJsonAsT<M>(json) {
if (json is List) {
return _getListChildType<M>(json);
} else {
return _fromJsonSingle<M>(json) as M;
}
}
print("${M.toString()} not found");

return null;
}

static M? fromJsonAsT<M>(dynamic json) {
if(json == null){
return null;
}
if (json is List) {
return _getListChildType<M>(json);
} else {
return _fromJsonSingle<M>(json as Map<String, dynamic>);
}
}
}
4 changes: 4 additions & 0 deletions lib/generated/json/base/json_field.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

// This file is automatically generated. DO NOT EDIT, all your changes would be lost.

class JsonSerializable{
const JsonSerializable();
}

class JSONField {
//Specify the parse field name
final String? name;
Expand Down
Loading

0 comments on commit 73f2ab5

Please sign in to comment.