-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ToJSON.swift
139 lines (118 loc) · 3.83 KB
/
ToJSON.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
136
137
138
139
//
// ToJSON.swift
// ObjectMapper
//
// Created by Tristan Himmelman on 2014-10-13.
// Copyright (c) 2014 hearst. All rights reserved.
//
import class Foundation.NSNumber
private func setValue(value: AnyObject, forKey key: String, inout #dictionary: [String : AnyObject]) {
return setValue(value, forKeyPathComponents: key.componentsSeparatedByString("."), dictionary: &dictionary)
}
private func setValue(value: AnyObject, forKeyPathComponents components: [String], inout #dictionary: [String : AnyObject]) {
if components.isEmpty {
return
}
let head = components.first!
if components.count == 1 {
return dictionary[head] = value
} else {
var child = dictionary[head] as? [String : AnyObject]
if child == nil {
child = [:]
}
let tail = Array(components[1..<components.count])
setValue(value, forKeyPathComponents: tail, dictionary: &child!)
return dictionary[head] = child
}
}
internal final class ToJSON {
class func basicType<N>(field: N, key: String, inout dictionary: [String : AnyObject]) {
func _setValue(value: AnyObject) {
setValue(value, forKey: key, dictionary: &dictionary)
}
switch field {
// basic Types
case let x as NSNumber:
_setValue(x)
case let x as Bool:
_setValue(x)
case let x as Int:
_setValue(x)
case let x as Double:
_setValue(x)
case let x as Float:
_setValue(x)
case let x as String:
_setValue(x)
// Arrays with basic types
case let x as Array<NSNumber>:
_setValue(x)
case let x as Array<Bool>:
_setValue(x)
case let x as Array<Int>:
_setValue(x)
case let x as Array<Double>:
_setValue(x)
case let x as Array<Float>:
_setValue(x)
case let x as Array<String>:
_setValue(x)
case let x as Array<AnyObject>:
_setValue(x)
// Dictionaries with basic types
case let x as Dictionary<String, NSNumber>:
_setValue(x)
case let x as Dictionary<String, Bool>:
_setValue(x)
case let x as Dictionary<String, Int>:
_setValue(x)
case let x as Dictionary<String, Double>:
_setValue(x)
case let x as Dictionary<String, Float>:
_setValue(x)
case let x as Dictionary<String, String>:
_setValue(x)
case let x as Dictionary<String, AnyObject>:
_setValue(x)
default:
//println("Default")
return
}
}
class func optionalBasicType<N>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
basicType(field, key: key, dictionary: &dictionary)
}
}
class func object<N: Mappable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
setValue(Mapper().toJSON(field), forKey: key, dictionary: &dictionary)
}
class func optionalObject<N: Mappable>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
object(field, key: key, dictionary: &dictionary)
}
}
class func objectArray<N: Mappable>(field: Array<N>, key: String, inout dictionary: [String : AnyObject]) {
let JSONObjects = Mapper().toJSONArray(field)
if !JSONObjects.isEmpty {
setValue(JSONObjects, forKey: key, dictionary: &dictionary)
}
}
class func optionalObjectArray<N: Mappable>(field: Array<N>?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
objectArray(field, key: key, dictionary: &dictionary)
}
}
class func objectDictionary<N: Mappable>(field: Dictionary<String, N>, key: String, inout dictionary: [String : AnyObject]) {
let JSONObjects = Mapper().toJSONDictionary(field)
if !JSONObjects.isEmpty {
setValue(JSONObjects, forKey: key, dictionary: &dictionary)
}
}
class func optionalObjectDictionary<N: Mappable>(field: Dictionary<String, N>?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
objectDictionary(field, key: key, dictionary: &dictionary)
}
}
}