Skip to content

Commit

Permalink
add json 2 flutter mode
Browse files Browse the repository at this point in the history
  • Loading branch information
vvkeep committed Jul 23, 2019
1 parent 8802325 commit d10b4d4
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 39 deletions.
19 changes: 11 additions & 8 deletions JSONConverter/Classes/Main/FileContent/YWContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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<String, dynamic> srcJson) => _$\(className)FromJson(srcJson);\n\n\tMap<String, dynamic> toJson() => _$\(className)ToJson(this);\n\n}\n"
}

return contentStr
}

private func propertyPartAndIntPart() -> (String, String) {
private func propertyAndInitPart() -> (String, String) {
var propertyStr = ""
var initSwiftStr = ""

Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions JSONConverter/Classes/Main/FileContent/YWFile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
95 changes: 67 additions & 28 deletions JSONConverter/Classes/Main/FileContent/YWProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class YWProperty {

func toString() -> (String, String){
var propertyStr = ""
var swiftInitStr = ""
var initStr = ""
switch type {
case .String:
switch langStruct.langType{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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<String,dynamic> \(propertyKey);\n"
initStr = "this.\(propertyKey),"
}
case .ArrayString:
switch langStruct.langType{
Expand All @@ -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<String> \(propertyKey);\n"
initStr = "this.\(propertyKey),"
}
case .ArrayInt:
switch langStruct.langType{
Expand All @@ -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<int> \(propertyKey);\n"
initStr = "this.\(propertyKey),"
}
case .ArrayFloat:
switch langStruct.langType{
Expand All @@ -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<double> \(propertyKey);\n"
initStr = "this.\(propertyKey),"
}
case .ArrayDouble:
switch langStruct.langType{
Expand All @@ -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<double> \(propertyKey);\n"
initStr = "this.\(propertyKey),"
}
case .ArrayBool:
switch langStruct.langType{
Expand All @@ -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<bool> \(propertyKey);\n"
initStr = "this.\(propertyKey),"
}
case .ArrayDictionary:
switch langStruct.langType{
Expand All @@ -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{
Expand All @@ -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)
}

}
8 changes: 5 additions & 3 deletions JSONConverter/Classes/Main/MainView/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum LangType: Int {
case SwiftyJSON
case ObjectMapper
case ObjC
case Flutter
}

enum StructType: Int {
Expand All @@ -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
}()

Expand Down Expand Up @@ -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)
}
}
Expand Down
14 changes: 14 additions & 0 deletions JSONConverter/Classes/Vendor/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d10b4d4

Please sign in to comment.