swift-json

代码生成

在对接net api 之前, 我们往往需要定义一个 modal 来对应返回的数据.

在 codable 出来之前, 我们使用的转换工具有几种, 原来的项目使用的是 SwiftJson, 在此之上, 统一规范, 只能找一些生成相应 boilerplate code 的方法来减少工作量.
这里记录下使用的工具.

JSONExport

一个工具, 可以根据 JSON 生成对应的模板代码, 但是这个模板的修改需要侵入源码去修改. 而且它是根据字符串占位去修改的. 改成想要的模板可以说是很痛苦了. 对…我就吃了这么个屎…但是后来减少的工作量可以说是很值得了.

Jsoncafe

jsoncafe 和上面的 JSONExport 功能相似, BUT! 他是在线的! 不需要安装! 而且可以提供编辑模板, 而且是提供了某个模板语言的 syntax, 可读性非常好!

十分激动发现了这个…激动下…记于2018-09-05 16:19:06…

修改了下 template, 以备后续之需

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Foundation
import SwiftyJSON
import Common
import CocoaLumberjack

final class {{className}} : JSONAbleType {
{{#properties}}
{{!-- Start Array Handling --}}
{{#xif "isArray"}}
{{#xif "elementsAreOfCustomType"}}
var {{nativeName}} : {{type}}
{{else}}
var {{nativeName}} : [String]
{{/xif}}
{{!-- End Array handling --}}
{{else}}
{{#xif "isCustomClass"}}
var {{nativeName}} : {{type}}
{{else}}
{{!-- Handling basicType --}}
{{#xif "basicType"}}
var {{nativeName}} : String
{{else}}
var {{nativeName}} : String
{{/xif}}
{{!-- End Handling basicType --}}
{{/xif}}
{{/xif}}
{{/properties}}

/**
* Get bean from json
*/
class func fromJSON(_ json: [String: Any]) -> {{className}} {
let json = JSON(json)
return {{className}}(fromJson: json)
}
class func fromJsonArray(_ json: JSON!) -> [{{className}}] {
let arr = json.arrayValue
return arr.map{(product) in
{{className}}(fromJson: product)
}
}

/**
* Instantiate the instance using the passed json values to set the properties values
*/
init(fromJson json: JSON!){

{{#properties}}
{{!-- Start Array Handling --}}
{{#xif "isArray"}}
{{#xif "elementsAreOfCustomType"}}
{{nativeName}} = [{{elementType}}]()
var {{nativeName}}Array = json["{{jsonName}}"].arrayValue
for {{nativeName}}Json in {{nativeName}}Array{
let value = {{elementType}}(fromJson: {{nativeName}}Json)
{{nativeName}}.append(value)
}
{{else}}
{{nativeName}} = [Int]()
let {{nativeName}}Array = json["{{jsonName}}"].arrayValue
for {{nativeName}}Json in {{nativeName}}Array{
{{nativeName}}.append({{nativeName}}Json.{{basicType}})
}
{{/xif}}
{{!-- End Array handling --}}
{{else}}
{{#xif "isCustomClass"}}
let {{nativeName}}Json = json["{{jsonName}}"]
if !{{nativeName}}Json.isEmpty{
{{nativeName}} = {{capitalizeVarType}}(fromJson: {{nativeName}}Json)
}
{{else}}
{{!-- Handling basicType --}}
{{#xif "basicType"}}
{{nativeName}} = json["{{jsonName}}"].stringValue
{{else}}
{{nativeName}} = json["{{jsonName}}"].stringValue
{{/xif}}
{{!-- End Handling basicType --}}
{{/xif}}
{{/xif}}
{{/properties}}
}

/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
{{#properties}}
{{#xif isCustomClass}}
{{!--Custom type--}}
if {{nativeName}} != nil{
dictionary["{{nativeName}}"] = {{nativeName}}.toDictionary()
}
{{else}}
{{#xif "isArray && elementsAreOfCustomType"}}
{{!-- Array Type --}}
if {{nativeName}} != nil{
var dictionaryElements = [[String:Any]]()
for {{nativeName}}Element in {{nativeName}} {
dictionaryElements.append({{nativeName}}Element.toDictionary())
}
dictionary["{{nativeName}}"] = dictionaryElements
}
{{else}}
{{!-- Default Type --}}
dictionary["{{jsonName}}"] = {{nativeName}}
{{/xif}}
{{/xif}}
{{/properties}}
return dictionary
}
}

另一个选择:

quicktype 他的站点能是呢工程的呢个更多类型的文件, 虽然在上面那个自定义模板之下, 其实可以自己在某些限度之内也生成其他类型的文件, 但是自定义避免不了出现错误的情况. 这个也提供了 app 的安装, 还有几个 IDE 的插件也都提供. 在 web 上使用想要修改些参数的话还要登录. 目前没有登录试用, 不知道能否修改模板. 想要更多语言的可以试试这个.