import 'dart:convert';
import 'dart:io';
Future<void> main() async {
String filePath = "D:\\FlutterProject\\suzhou_bridge_2023\\lib\\services"
"\\frequent_inspection\\frequent_inspection_disease_data_test.dart";
var file = File(filePath);
List<String> classNames = [];
Stream<List<int>> inputStream = file.openRead();
List<String> trueLines = [];
var lines = utf8.decoder
.bind(inputStream)
.transform(const LineSplitter());
try {
await for (final line in lines) {
trueLines.add(line);
if(RegExp(r'class [a-zA-Z0-9]* extends Object').hasMatch(line)){
classNames.add(line.split(" ")[1]);
}
}
// print(classNames);
// print(trueLines);
String fileStr = "import 'dart:core';\nimport 'package:json_annotation/json_annotation.dart';\n\n";
for(var className in classNames){
fileStr = fileStr+_getClassAndPara(className,trueLines);
}
// print(fileStr);
await file.delete();
try{
File file2 = File(filePath);
await file2.writeAsString(fileStr);
}catch (e) {
// print(e);
}
} catch (e) {
// print(e);
}
}
String _getClassAndPara(String classN,List<String> trueLines){
int startMark = 0;
int endMark = 0;
List<String> paras = [];
for(int i=0;i<trueLines.length;i++){
if(trueLines[i].contains(classN) && trueLines[i-1].contains("@JsonSerializable()")){
startMark = i-2;
// startMark = i-1;
break;
}
}
for(int i=startMark;i<trueLines.length;i++){
if(trueLines[i].contains("}")){
endMark = i;
break;
}
}
// print(startMark);
// print(endMark);
for(int i=startMark;i<=endMark;i++){
if(trueLines[i].contains("@JsonKey()") && i<endMark){
paras.add(trueLines[i+1].trim().replaceAll(";", ""));
}
}
// print(paras);
String factory1 = " factory $classN.fromJson(Map<String,dynamic> json)=> _\$${classN}FromJson(json);\n";
String factory2 = " Map<String,dynamic> toJson() => _\$${classN}ToJson(this);\n}\n";
String fromJson = "$classN _\$${classN}FromJson(Map<String, dynamic> json){\n";
String toJson = "Map<String, dynamic> _\$${classN}ToJson($classN instance) =>\n <String, dynamic>{\n";
// print(paras);
for(var para in paras){
print(para);
/// 首先匹配List的情况
// if(para.contains("List")){
if(RegExp(r'List<[a-zA-Z]*\??>').hasMatch(para)){
fromJson = "$fromJson ${para.split(" ")[0]} ${para.split(" ")[1]};\n";
fromJson = "$fromJson if(json['${para.split(" ")[1]}'] == null){\n ${para.split(" ")[1]} = [];\n }\n";
if(para.contains("String?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as String).toList();\n }\n\n";
}
else if(para.contains("int?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as int).toList();\n }\n\n";
}
else if(para.contains("double?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as double).toList();\n }\n\n";
}
else if(para.contains("bool?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as bool).toList();\n }\n\n";
}
else{
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:${para.split(" ")[0].split("<")[1].split("?")[0]}.fromJson(e as Map<String, dynamic>)).toList();\n }\n\n";
}
}
}
fromJson = "$fromJson return $classN(\n";
for(var para in paras){
if(RegExp(r'List<[a-zA-Z]*\??>').hasMatch(para)){
fromJson = "$fromJson ${para.split(" ")[1]},\n";
}else if(para.contains("String?")||para.contains("int?")||para.contains("double?")||para.contains("bool?")){
fromJson = "$fromJson json['${para.split(" ")[1]}'],\n";
}else{
fromJson = "$fromJson ${para.split("?").first}.fromJson(json['${para.split(" ")[1]}']),\n";
}
toJson = "$toJson '${para.split(" ")[1]}': instance.${para.split(" ")[1]},\n";
}
fromJson = "$fromJson );\n}\n";
toJson = "$toJson };\n\n";
String lastStr = "";
for(int i=startMark;i<endMark;i++){
lastStr = "$lastStr${trueLines[i]}\n";
}
// print(lastStr + factory1+factory2+"\n"+fromJson+"\n"+toJson);
return ("$lastStr$factory1$factory2\n$fromJson\n$toJson");
// print(toJson);
}
Dart
import 'dart:convert';
import 'dart:io';
Future<void> main() async {
String filePath = "D:\\FlutterProject\\suzhou_bridge_2023\\lib\\services"
"\\frequent_inspection\\frequent_inspection_disease_data_test.dart";
var file = File(filePath);
List<String> classNames = [];
Stream<List<int>> inputStream = file.openRead();
List<String> trueLines = [];
var lines = utf8.decoder
.bind(inputStream)
.transform(const LineSplitter());
try {
await for (final line in lines) {
trueLines.add(line);
if(RegExp(r'class [a-zA-Z0-9]* extends Object').hasMatch(line)){
classNames.add(line.split(" ")[1]);
}
}
// print(classNames);
// print(trueLines);
String fileStr = "import 'dart:core';\nimport 'package:json_annotation/json_annotation.dart';\n\n";
for(var className in classNames){
fileStr = fileStr+_getClassAndPara(className,trueLines);
}
// print(fileStr);
await file.delete();
try{
File file2 = File(filePath);
await file2.writeAsString(fileStr);
}catch (e) {
// print(e);
}
} catch (e) {
// print(e);
}
}
String _getClassAndPara(String classN,List<String> trueLines){
int startMark = 0;
int endMark = 0;
List<String> paras = [];
for(int i=0;i<trueLines.length;i++){
if(trueLines[i].contains(classN) && trueLines[i-1].contains("@JsonSerializable()")){
startMark = i-2;
// startMark = i-1;
break;
}
}
for(int i=startMark;i<trueLines.length;i++){
if(trueLines[i].contains("}")){
endMark = i;
break;
}
}
// print(startMark);
// print(endMark);
for(int i=startMark;i<=endMark;i++){
if(trueLines[i].contains("@JsonKey()") && i<endMark){
paras.add(trueLines[i+1].trim().replaceAll(";", ""));
}
}
// print(paras);
String factory1 = " factory $classN.fromJson(Map<String,dynamic> json)=> _\$${classN}FromJson(json);\n";
String factory2 = " Map<String,dynamic> toJson() => _\$${classN}ToJson(this);\n}\n";
String fromJson = "$classN _\$${classN}FromJson(Map<String, dynamic> json){\n";
String toJson = "Map<String, dynamic> _\$${classN}ToJson($classN instance) =>\n <String, dynamic>{\n";
// print(paras);
for(var para in paras){
print(para);
/// 首先匹配List的情况
// if(para.contains("List")){
if(RegExp(r'List<[a-zA-Z]*\??>').hasMatch(para)){
fromJson = "$fromJson ${para.split(" ")[0]} ${para.split(" ")[1]};\n";
fromJson = "$fromJson if(json['${para.split(" ")[1]}'] == null){\n ${para.split(" ")[1]} = [];\n }\n";
if(para.contains("String?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as String).toList();\n }\n\n";
}
else if(para.contains("int?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as int).toList();\n }\n\n";
}
else if(para.contains("double?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as double).toList();\n }\n\n";
}
else if(para.contains("bool?")){
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:e as bool).toList();\n }\n\n";
}
else{
fromJson = "$fromJson else{\n ${para.split(" ")[1]} = (json['${para.split(" ")[1]}'] as List).map((e) => e == null? null:${para.split(" ")[0].split("<")[1].split("?")[0]}.fromJson(e as Map<String, dynamic>)).toList();\n }\n\n";
}
}
}
fromJson = "$fromJson return $classN(\n";
for(var para in paras){
if(RegExp(r'List<[a-zA-Z]*\??>').hasMatch(para)){
fromJson = "$fromJson ${para.split(" ")[1]},\n";
}else if(para.contains("String?")||para.contains("int?")||para.contains("double?")||para.contains("bool?")){
fromJson = "$fromJson json['${para.split(" ")[1]}'],\n";
}else{
fromJson = "$fromJson ${para.split("?").first}.fromJson(json['${para.split(" ")[1]}']),\n";
}
toJson = "$toJson '${para.split(" ")[1]}': instance.${para.split(" ")[1]},\n";
}
fromJson = "$fromJson );\n}\n";
toJson = "$toJson };\n\n";
String lastStr = "";
for(int i=startMark;i<endMark;i++){
lastStr = "$lastStr${trueLines[i]}\n";
}
// print(lastStr + factory1+factory2+"\n"+fromJson+"\n"+toJson);
return ("$lastStr$factory1$factory2\n$fromJson\n$toJson");
// print(toJson);
}