/
ViewController.swift
135 lines (112 loc) · 4.15 KB
/
ViewController.swift
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 1999-2101 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Created by zhouzhuo on 7/11/16.
import UIKit
import HandyJSON
enum Grade: Int {
case One = 1
case Two = 2
case Three = 3
}
extension Grade: HandyJSONEnum {
static func makeInitWrapper() -> InitWrapperProtocol? {
return InitWrapper<Int>(rawInit: Grade.init)
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.serialization()
self.deserialization()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func serialization() {
enum Gender: String {
case Male = "male"
case Female = "Female"
}
struct Subject {
var id: Int64?
var name: String?
init(id: Int64, name: String) {
self.id = id
self.name = name
}
}
class Student {
var name: String?
var gender: Gender?
var subjects: [Subject]?
}
let student = Student()
student.name = "Jack"
student.gender = .Female
student.subjects = [Subject(id: 1, name: "math"), Subject(id: 2, name: "English"), Subject(id: 3, name: "Philosophy")]
print(JSONSerializer.serializeToJSON(object: student)!)
print(JSONSerializer.serializeToJSON(object: student, prettify: true)!)
let nsDict: NSDictionary = ["a": 1, "b": [1, 2, 3], "c": "hello"]
print(JSONSerializer.serialize(dict: nsDict).toJSON()!)
print(JSONSerializer.serialize(dict: nsDict).toPrettifyJSON()!)
let nsArray: NSArray = ["a", "b", 1, 2]
print(JSONSerializer.serialize(array: nsArray).toJSON()!)
print(JSONSerializer.serialize(array: nsArray).toPrettifyJSON()!)
}
func deserialization() {
enum Gender: String, HandyJSONEnum {
case Male = "Male"
case Female = "Female"
static func makeInitWrapper() -> InitWrapperProtocol? {
return InitWrapper<String>(rawInit: Gender.init)
}
}
struct Teacher: HandyJSON {
var name: String?
var age: Int?
var height: Int?
var gender: Gender?
}
struct Subject: HandyJSON {
var name: String?
var id: Int64?
var credit: Int?
var lessonPeriod: Int?
}
class Student: HandyJSON {
var id: String?
var name: String?
var age: Int?
var grade: Grade = .One
var height: Int?
var gender: Gender?
var className: String?
var teacher: Teacher?
var subject: [Subject]?
var seat: String?
required init() {}
}
let jsonString = "{\"id\":\"77544\",\"name\":\"Tom Li\",\"age\":18,\"grade\":2,\"height\":180,\"gender\":\"Male\",\"className\":\"A\",\"teacher\":{\"name\":\"Lucy He\",\"age\":28,\"height\":172,\"gender\":\"Female\",},\"subject\":[{\"name\":\"math\",\"id\":18000324583,\"credit\":4,\"lessonPeriod\":48},{\"name\":\"computer\",\"id\":18000324584,\"credit\":8,\"lessonPeriod\":64}],\"seat\":\"4-3-23\"}"
if let student = JSONDeserializer<Student>.deserializeFrom(json: jsonString) {
print("\(student)")
print("\(student.grade)")
print("\(student.gender)")
print("\(student.subject)")
}
}
}