/
FromJSON.swift
executable file
·105 lines (86 loc) · 3.12 KB
/
FromJSON.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
//
// FromJSON.swift
// ObjectMapper
//
// Created by Tristan Himmelman on 2014-10-09.
// Copyright (c) 2014 hearst. All rights reserved.
//
internal final class FromJSON {
/// Basic type
class func basicType<FieldType>(inout field: FieldType, object: FieldType?) {
if let value = object {
field = value
}
}
/// optional basic type
class func optionalBasicType<FieldType>(inout field: FieldType?, object: FieldType?) {
if let value = object {
field = value
}
}
/// Implicitly unwrapped optional basic type
class func optionalBasicType<FieldType>(inout field: FieldType!, object: FieldType?) {
if let value = object {
field = value
}
}
/// Mappable object
class func object<N: Mappable>(inout field: N, object: AnyObject?) {
if let value: N = Mapper().map(object) {
field = value
}
}
/// Optional Mappable Object
class func optionalObject<N: Mappable>(inout field: N?, object: AnyObject?) {
field = Mapper().map(object)
}
/// Implicitly unwrapped Optional Mappable Object
class func optionalObject<N: Mappable>(inout field: N!, object: AnyObject?) {
field = Mapper().map(object)
}
/// mappable object array
class func objectArray<N: Mappable>(inout field: Array<N>, object: AnyObject?) {
let parsedObjects = Mapper<N>().mapArray(object)
if let objects = parsedObjects {
field = objects
}
}
/// optional mappable object array
class func optionalObjectArray<N: Mappable>(inout field: Array<N>?, object: AnyObject?) {
field = Mapper().mapArray(object)
}
/// Implicitly unwrapped optional mappable object array
class func optionalObjectArray<N: Mappable>(inout field: Array<N>!, object: AnyObject?) {
field = Mapper().mapArray(object)
}
/// Dctionary containing Mappable objects
class func objectDictionary<N: Mappable>(inout field: Dictionary<String, N>, object: AnyObject?) {
let parsedObjects = Mapper<N>().mapDictionary(object)
if let objects = parsedObjects {
field = objects
}
}
/// Optional dictionary containing Mappable objects
class func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>?, object: AnyObject?) {
field = Mapper().mapDictionary(object)
}
/// Implicitly unwrapped Dictionary containing Mappable objects
class func optionalObjectDictionary<N: Mappable>(inout field: Dictionary<String, N>!, object: AnyObject?) {
field = Mapper().mapDictionary(object)
}
/// Dictionary containing Array of Mappable objects
class func objectDictionaryOfArrays<N: Mappable>(inout field: Dictionary<String, [N]>, object: AnyObject?) {
let parsedObjects = Mapper<N>().mapDictionaryOfArrays(object)
if let objects = parsedObjects {
field = objects
}
}
/// Optional Dictionary containing Array of Mappable objects
class func optionalObjectDictionaryOfArrays<N: Mappable>(inout field: Dictionary<String, [N]>?, object: AnyObject?) {
field = Mapper<N>().mapDictionaryOfArrays(object)
}
/// Implicitly unwrapped Dictionary containing Array of Mappable objects
class func optionalObjectDictionaryOfArrays<N: Mappable>(inout field: Dictionary<String, [N]>!, object: AnyObject?) {
field = Mapper<N>().mapDictionaryOfArrays(object)
}
}