From d10b4d47a8816ca82f63214961a913089e885035 Mon Sep 17 00:00:00 2001 From: "15919688564@163.com" <15919688564@163.com> Date: Tue, 23 Jul 2019 23:26:05 +0800 Subject: [PATCH] add json 2 flutter mode --- .../Classes/Main/FileContent/YWContent.swift | 19 ++-- .../Classes/Main/FileContent/YWFile.swift | 4 + .../Classes/Main/FileContent/YWProperty.swift | 95 +++++++++++++------ .../Main/MainView/MainViewController.swift | 8 +- .../Classes/Vendor/String+Extension.swift | 14 +++ 5 files changed, 101 insertions(+), 39 deletions(-) diff --git a/JSONConverter/Classes/Main/FileContent/YWContent.swift b/JSONConverter/Classes/Main/FileContent/YWContent.swift index 2f78f8f..586ee55 100644 --- a/JSONConverter/Classes/Main/FileContent/YWContent.swift +++ b/JSONConverter/Classes/Main/FileContent/YWContent.swift @@ -29,9 +29,9 @@ class YWContent { let className = propertyKey.className(withPrefix: prefixStr) var contentStr = "" - let result = propertyPartAndIntPart() + let result = propertyAndInitPart() var propertyTotalPart = result.0 - let initSwiftTotalPart = result.1 + let initTotalPart = result.1 switch langStruct.langType { case .ObjC: @@ -53,22 +53,24 @@ class YWContent { } case .SwiftyJSON: if langStruct.structType == .class { - contentStr = "\nclass \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\tinit(json: JSON) {\n\(initSwiftTotalPart)\t}\n}\n" + contentStr = "\nclass \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\tinit(json: JSON) {\n\(initTotalPart)\t}\n}\n" }else if langStruct.structType == .struct { - contentStr = "\nstruct \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\tinit(json: JSON) {\n\(initSwiftTotalPart)\t}\n}\n" + contentStr = "\nstruct \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\tinit(json: JSON) {\n\(initTotalPart)\t}\n}\n" } case .ObjectMapper: if langStruct.structType == .class { - contentStr = "\nclass \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\trequired init?(map: Map) {}\n\n\tfunc mapping(map: Map) {\n\(initSwiftTotalPart)\t}\n}\n" + contentStr = "\nclass \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\trequired init?(map: Map) {}\n\n\tfunc mapping(map: Map) {\n\(initTotalPart)\t}\n}\n" }else if langStruct.structType == .struct { - contentStr = "\nstruct \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\tinit?(map: Map) {}\n\n\tmutating func mapping(map: Map) {\n\(initSwiftTotalPart)\t}\n}\n" + contentStr = "\nstruct \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\tinit?(map: Map) {}\n\n\tmutating func mapping(map: Map) {\n\(initTotalPart)\t}\n}\n" } + case .Flutter: + contentStr = "\n@JsonSerializable()\nclass \(className)\(superClassNamePart()) {\n\(propertyTotalPart)\n\t\(className)(\(initTotalPart));\n\n\tfactory \(className).fromJson(Map srcJson) => _$\(className)FromJson(srcJson);\n\n\tMap toJson() => _$\(className)ToJson(this);\n\n}\n" } return contentStr } - private func propertyPartAndIntPart() -> (String, String) { + private func propertyAndInitPart() -> (String, String) { var propertyStr = "" var initSwiftStr = "" @@ -93,7 +95,8 @@ class YWContent { superClassPart = superClass.isEmpty ? ": NSObject" : ": \(superClass)" case .ObjectMapper: superClassPart = superClass.isEmpty ? ": Mappable" : ": \(superClass)" - break + case .Flutter: + superClassPart = superClass.isEmpty ? " extends Object" : " extends \(superClass)" } return superClassPart diff --git a/JSONConverter/Classes/Main/FileContent/YWFile.swift b/JSONConverter/Classes/Main/FileContent/YWFile.swift index 8349ebd..17fcccb 100644 --- a/JSONConverter/Classes/Main/FileContent/YWFile.swift +++ b/JSONConverter/Classes/Main/FileContent/YWFile.swift @@ -56,6 +56,10 @@ class YWFile { func toString() -> String { var totalStr = "" + if langStruct.langType == LangType.Flutter { + var className = rootName.className(withPrefix: prefix); + totalStr = "\nimport 'package:json_annotation/json_annotation.dart';\n\npart '\(className.underline()).g.dart';\n" + } contents.forEach { (content) in totalStr += content.toString() } diff --git a/JSONConverter/Classes/Main/FileContent/YWProperty.swift b/JSONConverter/Classes/Main/FileContent/YWProperty.swift index 00ec1b6..69c39d0 100644 --- a/JSONConverter/Classes/Main/FileContent/YWProperty.swift +++ b/JSONConverter/Classes/Main/FileContent/YWProperty.swift @@ -45,7 +45,7 @@ class YWProperty { func toString() -> (String, String){ var propertyStr = "" - var swiftInitStr = "" + var initStr = "" switch type { case .String: switch langStruct.langType{ @@ -55,10 +55,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey): String?\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey): String?\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].stringValue\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].stringValue\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey): String?\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tString \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .Int: switch langStruct.langType{ @@ -68,10 +71,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey): Int = 0\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey): Int = 0\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].intValue\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].intValue\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey): Int = 0\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tint \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .Float: switch langStruct.langType{ @@ -81,10 +87,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey): Float = 0.0\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey): Float = 0.0\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].floatValue\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].floatValue\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey): Float = 0.0\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tdouble \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .Double: switch langStruct.langType{ @@ -94,10 +103,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey): Double = 0.0\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey): Double = 0.0\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].doubleValue\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].doubleValue\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey): Double = 0.0\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tdouble \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .Bool: switch langStruct.langType{ @@ -107,10 +119,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey): Bool = false\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey): Bool = false\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].boolValue\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].boolValue\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey): Bool = false\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tbool \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .Dictionary: switch langStruct.langType{ @@ -120,10 +135,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey): \(propertyKey.className(withPrefix: prefixStr))?\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey): \(propertyKey.className(withPrefix: prefixStr))?\n" - swiftInitStr = "\t\t\(propertyKey) = \(propertyKey.className(withPrefix: prefixStr))(json: json[\"\(propertyKey)\"])\n" + initStr = "\t\t\(propertyKey) = \(propertyKey.className(withPrefix: prefixStr))(json: json[\"\(propertyKey)\"])\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey): \(propertyKey.className(withPrefix: prefixStr))?\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tMap \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .ArrayString: switch langStruct.langType{ @@ -133,10 +151,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey) = [String]()\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey) = [String]()\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.stringValue})\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.stringValue})\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey) = [String]()\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tList \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .ArrayInt: switch langStruct.langType{ @@ -146,10 +167,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey) = [Int]()\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey) = [Int]()\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.intValue})\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.intValue})\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey) = [Int]()\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tList \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .ArrayFloat: switch langStruct.langType{ @@ -159,10 +183,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey) = [Float]()\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey) = [Float]()\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.floatValue})\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.floatValue})\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey) = [Float]()\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tList \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .ArrayDouble: switch langStruct.langType{ @@ -172,10 +199,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey) = [Double]()\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey) = [Double]()\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.doubleValue})\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.doubleValue})\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey) = [Double]()\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tList \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .ArrayBool: switch langStruct.langType{ @@ -185,10 +215,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey) = [Bool]()\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey) = [Bool]()\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.boolValue})\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({$0.boolValue})\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey) = [Bool]()\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tList \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .ArrayDictionary: switch langStruct.langType{ @@ -198,10 +231,13 @@ class YWProperty { propertyStr = "\tvar \(propertyKey) = [\(propertyKey.className(withPrefix: prefixStr))]()\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey) = [\(propertyKey.className(withPrefix: prefixStr))]()\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({ \(propertyKey.className(withPrefix: prefixStr))(json: $0)})\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].arrayValue.flatMap({ \(propertyKey.className(withPrefix: prefixStr))(json: $0)})\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey) = [\(propertyKey.className(withPrefix: prefixStr))]()\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tList<\(propertyKey.className(withPrefix: prefixStr))> \(propertyKey);\n" + initStr = "this.\(propertyKey)," } case .nil: switch langStruct.langType{ @@ -211,15 +247,18 @@ class YWProperty { propertyStr = "\tvar \(propertyKey): String?\n" case .SwiftyJSON: propertyStr = "\tvar \(propertyKey): String?\n" - swiftInitStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].stringValue\n" + initStr = "\t\t\(propertyKey) = json[\"\(propertyKey)\"].stringValue\n" case .ObjectMapper: propertyStr = "\tvar \(propertyKey): String?\n" - swiftInitStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + initStr = "\t\t\(propertyKey)\(currentMapperSpace)<- map[\"\(propertyKey)\"]\n" + case .Flutter: + propertyStr = "\n\t@JsonKey(name: '\(propertyKey)')\n\tString \(propertyKey);\n" + initStr = "this.\(propertyKey)," } } - return (propertyStr, swiftInitStr) + return (propertyStr, initStr) } } diff --git a/JSONConverter/Classes/Main/MainView/MainViewController.swift b/JSONConverter/Classes/Main/MainView/MainViewController.swift index 58e3004..27be4ef 100644 --- a/JSONConverter/Classes/Main/MainView/MainViewController.swift +++ b/JSONConverter/Classes/Main/MainView/MainViewController.swift @@ -14,6 +14,7 @@ enum LangType: Int { case SwiftyJSON case ObjectMapper case ObjC + case Flutter } enum StructType: Int { @@ -32,10 +33,11 @@ struct LangStruct { } let FILE_CACHE_CONFIG_KEY = "FILE_CACHE_CONFIG_KEY" + class MainViewController: NSViewController { lazy var transTypeTitleArr: [String] = { - let titleArr = ["Swift", "HandyJSON", "SwiftyJSON", "ObjectMapper", "Objective-C"] + let titleArr = ["Swift", "HandyJSON", "SwiftyJSON", "ObjectMapper", "Objective-C", "Flutter"] return titleArr }() @@ -188,12 +190,12 @@ extension MainViewController: NSComboBoxDelegate { let comBox = notification.object as! NSComboBox if comBox == converTypeBox { // 选择语言 let langType = LangType(rawValue: converTypeBox.indexOfSelectedItem) - if langType == LangType.ObjC { // 如果是OC 就选择 class + if langType == LangType.ObjC || langType == LangType.Flutter { // 如果是OC Flutter 就选择 class converStructBox.selectItem(at: 1) } }else if comBox == converStructBox { //选择类或结构体 let langType = LangType(rawValue: converTypeBox.indexOfSelectedItem) - if langType == LangType.ObjC { // 如果是OC 无论怎么选 都是 类 + if langType == LangType.ObjC || langType == LangType.Flutter { // 如果是OC Flutter 无论怎么选 都是 类 converStructBox.selectItem(at: 1) } } diff --git a/JSONConverter/Classes/Vendor/String+Extension.swift b/JSONConverter/Classes/Vendor/String+Extension.swift index 4add0a7..b134363 100644 --- a/JSONConverter/Classes/Vendor/String+Extension.swift +++ b/JSONConverter/Classes/Vendor/String+Extension.swift @@ -56,6 +56,20 @@ extension String { return space } + + /// 驼峰转为下划线 + mutating func underline() -> String { + self = lowercaseFirstChar() + var result = [String](); + for item in self { + if item >= "A" && item <= "Z" { + result.append("_\(item.lowercased())") + }else { + result.append(String(item)) + } + } + return result.joined() + } } extension NSNumber {