From 00e90879d6b9272f1e74a7bab8d536640a3869a5 Mon Sep 17 00:00:00 2001 From: zmtzawqlp Date: Sun, 4 Apr 2021 14:44:36 +0800 Subject: [PATCH] Fix clone method --- Flutter/json_to_dart/CHANGELOG.md | 7 ++++ .../json_to_dart/lib/models/dart_object.dart | 35 ++++++++++++++----- .../json_to_dart/lib/utils/dart_helper.dart | 2 +- Flutter/json_to_dart/pubspec.yaml | 2 +- 4 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 Flutter/json_to_dart/CHANGELOG.md diff --git a/Flutter/json_to_dart/CHANGELOG.md b/Flutter/json_to_dart/CHANGELOG.md new file mode 100644 index 0000000..09e76e3 --- /dev/null +++ b/Flutter/json_to_dart/CHANGELOG.md @@ -0,0 +1,7 @@ +## 1.1.0 + +* Add clone method + +## 1.0.0 + +* First Release diff --git a/Flutter/json_to_dart/lib/models/dart_object.dart b/Flutter/json_to_dart/lib/models/dart_object.dart index 496a3fa..ca654c1 100644 --- a/Flutter/json_to_dart/lib/models/dart_object.dart +++ b/Flutter/json_to_dart/lib/models/dart_object.dart @@ -253,7 +253,6 @@ class DartObject extends DartProperty { (ConfigSetting().nullsafety && item.nullable), ); final bool isGetSet = fss.startsWith('{'); - if (item is DartObject) { className = item.className; @@ -319,11 +318,32 @@ class DartObject extends DartProperty { [typeString, name, lowName])); fromJsonSb.writeLine(setString); - final bool nonNullAble = ConfigSetting().nullsafety && !item.nullable; + // String setNameTemp = setName; + + // if (className != null) { + // String toJson = '=> e.toJson()'; + // dynamic value = item.value; + // String typeString = className; + // while (value is List) { + // toJson = '=> e.map(($typeString e) $toJson)'; + // typeString = 'List<$typeString>'; + // if (value.isNotEmpty) { + // value = value.first; + // } else { + // break; + // } + // } + // toJson = toJson.replaceFirst('=>', ''); + // toJson = toJson.replaceFirst('e', ''); + // toJson = toJson.trim(); + + // final bool nonNullAble = ConfigSetting().nullsafety && !item.nullable; + // setNameTemp += '${nonNullAble ? '' : '?'}$toJson'; + // } + toJsonSb.writeLine(stringFormat(DartHelper.toJsonSetString, [ item.key, - setName + - (item is DartObject ? '${nonNullAble ? '' : '?'}.toJson()' : '') + setName, ])); } @@ -362,7 +382,8 @@ class DartObject extends DartProperty { sb.writeLine(propertySb.toString()); sb.writeLine(DartHelper.classToString); sb.writeLine(toJsonSb.toString()); - sb.writeLine(stringFormat(DartHelper.classToClone, [className])); + sb.writeLine(stringFormat(DartHelper.classToClone, + [className, if (ConfigSetting().nullsafety) '!' else ''])); } sb.writeLine(DartHelper.classFooter); @@ -408,8 +429,6 @@ class DartObject extends DartProperty { @override List get props => [ className, - nullable, - propertyAccessorType, - type, + properties, ]; } diff --git a/Flutter/json_to_dart/lib/utils/dart_helper.dart b/Flutter/json_to_dart/lib/utils/dart_helper.dart index 1d3260b..8f13c4e 100644 --- a/Flutter/json_to_dart/lib/utils/dart_helper.dart +++ b/Flutter/json_to_dart/lib/utils/dart_helper.dart @@ -101,7 +101,7 @@ import 'dart:convert';'''; ' \n@override\nString toString() {\n return jsonEncode(this);\n }'; static const String classToClone = - '\n{0} clone() => {0}.fromJson(toJson());\n'; + '\n{0} clone() => {0}.fromJson(asT>(jsonDecode(jsonEncode(this))){1});\n'; static DartType converDartType(Type type) { if (type == int) { diff --git a/Flutter/json_to_dart/pubspec.yaml b/Flutter/json_to_dart/pubspec.yaml index 856a3c2..1d60bf7 100644 --- a/Flutter/json_to_dart/pubspec.yaml +++ b/Flutter/json_to_dart/pubspec.yaml @@ -11,7 +11,7 @@ description: The tool to convert json to dart code. # In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion. # Read more about iOS versioning at # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -version: 1.0.0+1 +version: 1.1.0 environment: sdk: '>=2.12.0 <3.0.0'