-
Notifications
You must be signed in to change notification settings - Fork 1k
/
ToJSON.swift
140 lines (114 loc) · 4.59 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
140
//
// ToJSON.swift
// ObjectMapper
//
// Created by Tristan Himmelman on 2014-10-13.
// Copyright (c) 2014 hearst. All rights reserved.
//
import Foundation
class ToJSON {
func addObject(object: AnyObject, inout toDictionary dictionary: [String : AnyObject]){
}
func baseType<N>(field: N, key: String, inout dictionary: [String : AnyObject]) {
var temp = dictionary
var currentKey = key
switch N.self {
// basic Types
case is Bool.Type:
dictionary[currentKey] = field as Bool
case is Int.Type:
dictionary[currentKey] = field as Int
case is Double.Type:
dictionary[currentKey] = field as Double
case is Float.Type:
dictionary[currentKey] = field as Float
case is String.Type:
dictionary[currentKey] = field as String
// Arrays with basic types
case is Array<Bool>.Type:
dictionary[currentKey] = field as Array<Bool>
case is Array<Int>.Type:
dictionary[currentKey] = field as Array<Int>
case is Array<Double>.Type:
dictionary[currentKey] = field as Array<Double>
case is Array<Float>.Type:
dictionary[currentKey] = field as Array<Float>
case is Array<String>.Type:
dictionary[currentKey] = field as Array<String>
// Dictionaries with basic types
case is Dictionary<String, Bool>.Type:
dictionary[currentKey] = field as Dictionary<String,Bool>
case is Dictionary<String, Bool>.Type:
dictionary[currentKey] = field as Dictionary<String,Int>
case is Dictionary<String, Bool>.Type:
dictionary[currentKey] = field as Dictionary<String,Double>
case is Dictionary<String, Bool>.Type:
dictionary[currentKey] = field as Dictionary<String,Float>
case is Dictionary<String, String>.Type:
dictionary[currentKey] = field as Dictionary<String,String>
default:
//println("Default")
return
}
}
func optionalBaseType<N>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
if let field: N = field {
baseType(field, key: key, dictionary: &dictionary)
}
}
func baseArray(field: Array<AnyObject>, key: String, inout dictionary: [String : AnyObject]){
dictionary[key] = NSArray(array: field)
}
func optionalBaseArray(field: Array<AnyObject>?, key: String, inout dictionary: [String : AnyObject]){
if let value = field {
baseArray(value, key: key, dictionary: &dictionary)
}
}
func baseDictionary(field: Dictionary<String, AnyObject>, key: String, inout dictionary: [String : AnyObject]){
dictionary[key] = NSDictionary(dictionary: field)
}
func optionalBaseDictionary(field: Dictionary<String, AnyObject>?, key: String, inout dictionary: [String : AnyObject]){
if let value = field {
baseDictionary(value, key: key, dictionary: &dictionary)
}
}
func object<N: Mappable>(field: N, key: String, inout dictionary: [String : AnyObject]) {
let mapper = Mapper<N>()
dictionary[key] = NSDictionary(dictionary: mapper.toJSON(field))
}
func optionalObject<N: Mappable>(field: N?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
object(field, key: key, dictionary: &dictionary)
}
}
func objectArray<N: Mappable>(field: Array<N>, key: String, inout dictionary: [String : AnyObject]) {
var JSONObjects = NSMutableArray()
for object in field {
let mapper = Mapper<N>()
JSONObjects.addObject(mapper.toJSON(object))
}
if JSONObjects.count > 0 {
dictionary[key] = JSONObjects
}
}
func optionalObjectArray<N: Mappable>(field: Array<N>?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
objectArray(field, key: key, dictionary: &dictionary)
}
}
func objectDictionary<N: Mappable>(field: Dictionary<String, N>, key: String, inout dictionary: [String : AnyObject]) {
var JSONObjects = NSMutableDictionary()
for (k, object) in field {
let mapper = Mapper<N>()
JSONObjects.setObject(mapper.toJSON(object), forKey: k)
}
if JSONObjects.count > 0 {
dictionary[key] = JSONObjects
}
}
func optionalObjectDictionary<N: Mappable>(field: Dictionary<String, N>?, key: String, inout dictionary: [String : AnyObject]) {
if let field = field {
objectDictionary(field, key: key, dictionary: &dictionary)
}
}
}