forked from daltoniam/JSONJoy-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONJoy.swift
143 lines (139 loc) · 4.63 KB
/
JSONJoy.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
141
142
143
//////////////////////////////////////////////////////////////////////////////////////////////////
//
// JSONJoy.swift
//
// Created by Dalton Cherry on 9/17/14.
// Copyright (c) 2014 Vluxe. All rights reserved.
//
//////////////////////////////////////////////////////////////////////////////////////////////////
import Foundation
public class JSONDecoder {
var value: AnyObject?
//convert the value to a String
public var string: String? {
return value as? String
}
//convert the value to an Int
public var integer: Int? {
return value as? Int
}
//convert the value to a Double
public var double: Double? {
return value as? Double
}
//convert the value to a float
public var float: Float? {
return value as? Float
}
//treat the value as a bool
public var bool: Bool {
if let str = self.string {
let lower = str.lowercaseString
if lower == "true" || lower.toInt() > 0 {
return true
}
} else if let num = self.integer {
return num > 0
} else if let num = self.double {
return num > 0.99
} else if let num = self.float {
return num > 0.99
}
return false
}
//get the value if it is an error
public var error: NSError? {
return value as? NSError
}
//get the value if it is a dictionary
public var dictionary: Dictionary<String,JSONDecoder>? {
return value as? Dictionary<String,JSONDecoder>
}
//get the value if it is an array
public var array: Array<JSONDecoder>? {
return value as? Array<JSONDecoder>
}
//pull the raw values out of an array
public func getArray<T>(inout collect: Array<T>?) {
if let array = value as? Array<JSONDecoder> {
if collect == nil {
collect = Array<T>()
}
for decoder in array {
if let obj = decoder.value as? T {
collect?.append(obj)
}
}
}
}
//pull the raw values out of a dictionary.
public func getDictionary<T>(inout collect: Dictionary<String,T>?) {
if let dictionary = value as? Dictionary<String,JSONDecoder> {
if collect == nil {
collect = Dictionary<String,T>()
}
for (key,decoder) in dictionary {
if let obj = decoder.value as? T {
collect?[key] = obj
}
}
}
}
//the init that converts everything to something nice
public init(_ raw: AnyObject) {
var rawObject: AnyObject = raw
if let data = rawObject as? NSData {
var error: NSError?
var response: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(), error: &error)
if error != nil || response == nil {
value = error
return
}
rawObject = response!
}
if let array = rawObject as? NSArray {
var collect = [JSONDecoder]()
for val: AnyObject in array {
collect.append(JSONDecoder(val))
}
value = collect
} else if let dict = rawObject as? NSDictionary {
var collect = Dictionary<String,JSONDecoder>()
for (key,val: AnyObject) in dict {
collect[key as String] = JSONDecoder(val)
}
value = collect
} else {
value = rawObject
}
}
//Array access support
public subscript(index: Int) -> JSONDecoder {
get {
if let array = self.value as? NSArray {
if array.count > index {
return array[index] as JSONDecoder
}
}
return JSONDecoder(createError("index: \(index) is greater than array or this is not an Array type."))
}
}
//Dictionary access support
public subscript(key: String) -> JSONDecoder {
get {
if let dict = self.value as? NSDictionary {
if let value: AnyObject = dict[key] {
return value as JSONDecoder
}
}
return JSONDecoder(createError("key: \(key) does not exist or this is not a Dictionary type"))
}
}
func createError(text: String) -> NSError {
return NSError(domain: "JSONJoy", code: 1002, userInfo: [NSLocalizedDescriptionKey: text]);
}
}
//Implement this protocol on all objects you want to use JSONJoy with
public protocol JSONJoy {
init(_ decoder: JSONDecoder)
}