Skip to content

Commit ded4978

Browse files
author
Jack Colley
committed
Updating to Swift 3
1 parent 42f3e94 commit ded4978

File tree

3 files changed

+74
-51
lines changed

3 files changed

+74
-51
lines changed

.DS_Store

6 KB
Binary file not shown.

JSONJoy.xcodeproj/project.pbxproj

+3
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@
343343
};
344344
6B3E79B619D355CF006071F7 = {
345345
CreatedOnToolsVersion = 6.0.1;
346+
LastSwiftMigration = 0800;
346347
};
347348
6B3E79C119D355CF006071F7 = {
348349
CreatedOnToolsVersion = 6.0.1;
@@ -723,6 +724,7 @@
723724
PRODUCT_NAME = "$(TARGET_NAME)";
724725
SKIP_INSTALL = YES;
725726
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
727+
SWIFT_VERSION = 3.0;
726728
};
727729
name = Debug;
728730
};
@@ -742,6 +744,7 @@
742744
PRODUCT_BUNDLE_IDENTIFIER = "com.vluxe.$(PRODUCT_NAME:rfc1034identifier)";
743745
PRODUCT_NAME = "$(TARGET_NAME)";
744746
SKIP_INSTALL = YES;
747+
SWIFT_VERSION = 3.0;
745748
};
746749
name = Release;
747750
};

Source/JSONJoy.swift

+71-51
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,71 @@
88
//////////////////////////////////////////////////////////////////////////////////////////////////
99

1010
import Foundation
11+
fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
12+
switch (lhs, rhs) {
13+
case let (l?, r?):
14+
return l < r
15+
case (nil, _?):
16+
return true
17+
default:
18+
return false
19+
}
20+
}
21+
22+
fileprivate func > <T : Comparable>(lhs: T?, rhs: T?) -> Bool {
23+
switch (lhs, rhs) {
24+
case let (l?, r?):
25+
return l > r
26+
default:
27+
return rhs < lhs
28+
}
29+
}
30+
1131

12-
public enum JSONError: ErrorType {
13-
case WrongType
32+
public enum JSONError: Error {
33+
case wrongType
1434
}
1535

16-
public class JSONDecoder {
17-
var value: AnyObject?
36+
open class JSONDecoder {
37+
var value: Any?
1838

1939
///return the value raw
20-
public var rawValue: AnyObject? {
40+
open var rawValue: Any? {
2141
return value
2242
}
2343
///print the description of the JSONDecoder
24-
public var description: String {
44+
open var description: String {
2545
return self.print()
2646
}
2747
///convert the value to a String
28-
public var string: String? {
48+
open var string: String? {
2949
return value as? String
3050
}
3151

3252
///convert the value to an Int
33-
public var integer: Int? {
53+
open var integer: Int? {
3454
return value as? Int
3555
}
3656
///convert the value to an UInt
37-
public var unsigned: UInt? {
57+
open var unsigned: UInt? {
3858
return value as? UInt
3959
}
4060
///convert the value to a Double
41-
public var double: Double? {
61+
open var double: Double? {
4262
return value as? Double
4363
}
4464
///convert the value to a float
45-
public var float: Float? {
65+
open var float: Float? {
4666
return value as? Float
4767
}
4868
///convert the value to an NSNumber
49-
public var number: NSNumber? {
69+
open var number: NSNumber? {
5070
return value as? NSNumber
5171
}
5272
///treat the value as a bool
53-
public var bool: Bool {
73+
open var bool: Bool {
5474
if let str = self.string {
55-
let lower = str.lowercaseString
75+
let lower = str.lowercased()
5676
if lower == "true" || Int(lower) > 0 {
5777
return true
5878
}
@@ -66,64 +86,64 @@ public class JSONDecoder {
6686
return false
6787
}
6888
//get the value if it is an error
69-
public var error: NSError? {
89+
open var error: NSError? {
7090
return value as? NSError
7191
}
7292
//get the value if it is a dictionary
73-
public var dictionary: Dictionary<String,JSONDecoder>? {
93+
open var dictionary: Dictionary<String,JSONDecoder>? {
7494
return value as? Dictionary<String,JSONDecoder>
7595
}
7696
//get the value if it is an array
77-
public var array: Array<JSONDecoder>? {
97+
open var array: Array<JSONDecoder>? {
7898
return value as? Array<JSONDecoder>
7999
}
80100

81101
//get the string and have it throw if it doesn't work
82-
public func getString() throws -> String {
83-
guard let str = string else {throw JSONError.WrongType}
102+
open func getString() throws -> String {
103+
guard let str = string else {throw JSONError.wrongType}
84104
return str
85105
}
86106

87107
//get the int and have it throw if it doesn't work
88-
public func getInt() throws -> Int {
89-
guard let i = integer else {throw JSONError.WrongType}
108+
open func getInt() throws -> Int {
109+
guard let i = integer else {throw JSONError.wrongType}
90110
return i
91111
}
92112

93113
//get the unsigned and have it throw if it doesn't work
94-
public func getUnsigned() throws -> UInt {
95-
guard let i = unsigned else {throw JSONError.WrongType}
114+
open func getUnsigned() throws -> UInt {
115+
guard let i = unsigned else {throw JSONError.wrongType}
96116
return i
97117
}
98118

99119
//get the double and have it throw if it doesn't work
100-
public func getDouble() throws -> Double {
101-
guard let i = double else {throw JSONError.WrongType}
120+
open func getDouble() throws -> Double {
121+
guard let i = double else {throw JSONError.wrongType}
102122
return i
103123
}
104124

105125
//get the Float and have it throw if it doesn't work
106-
public func getFloat() throws -> Float {
107-
guard let i = float else {throw JSONError.WrongType}
126+
open func getFloat() throws -> Float {
127+
guard let i = float else {throw JSONError.wrongType}
108128
return i
109129
}
110130

111131
//get the number and have it throw if it doesn't work
112-
public func getFloat() throws -> NSNumber {
113-
guard let i = number else {throw JSONError.WrongType}
132+
open func getFloat() throws -> NSNumber {
133+
guard let i = number else {throw JSONError.wrongType}
114134
return i
115135
}
116136

117137
//get the bool and have it throw if it doesn't work
118-
public func getBool() throws -> Bool {
138+
open func getBool() throws -> Bool {
119139
if let _ = value as? NSNull {
120-
throw JSONError.WrongType
140+
throw JSONError.wrongType
121141
}
122142
return bool
123143
}
124144

125145
//pull the raw values out of an array
126-
public func getArray<T>(inout collect: Array<T>?) {
146+
open func getArray<T>(_ collect: inout Array<T>?) {
127147
if let array = value as? Array<JSONDecoder> {
128148
if collect == nil {
129149
collect = Array<T>()
@@ -136,7 +156,7 @@ public class JSONDecoder {
136156
}
137157
}
138158
///pull the raw values out of a dictionary.
139-
public func getDictionary<T>(inout collect: Dictionary<String,T>?) {
159+
open func getDictionary<T>(_ collect: inout Dictionary<String,T>?) {
140160
if let dictionary = value as? Dictionary<String,JSONDecoder> {
141161
if collect == nil {
142162
collect = Dictionary<String,T>()
@@ -149,15 +169,15 @@ public class JSONDecoder {
149169
}
150170
}
151171
///the init that converts everything to something nice
152-
public init(_ raw: AnyObject, isSub: Bool = false) {
153-
var rawObject: AnyObject = raw
154-
if let str = rawObject as? String where !isSub {
155-
rawObject = str.dataUsingEncoding(NSUTF8StringEncoding)!
172+
public init(_ raw: Any, isSub: Bool = false) {
173+
var rawObject: Any = raw
174+
if let str = rawObject as? String , !isSub {
175+
rawObject = str.data(using: String.Encoding.utf8)! as Any
156176
}
157-
if let data = rawObject as? NSData {
158-
var response: AnyObject?
177+
if let data = rawObject as? Data {
178+
var response: Any?
159179
do {
160-
try response = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions())
180+
try response = JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions())
161181
rawObject = response!
162182
}
163183
catch let error as NSError {
@@ -167,22 +187,22 @@ public class JSONDecoder {
167187
}
168188
if let array = rawObject as? NSArray {
169189
var collect = [JSONDecoder]()
170-
for val: AnyObject in array {
190+
for val: Any in array {
171191
collect.append(JSONDecoder(val, isSub: true))
172192
}
173-
value = collect
193+
value = collect as Any?
174194
} else if let dict = rawObject as? NSDictionary {
175195
var collect = Dictionary<String,JSONDecoder>()
176196
for (key,val) in dict {
177-
collect[key as! String] = JSONDecoder(val, isSub: true)
197+
collect[key as! String] = JSONDecoder(val as AnyObject, isSub: true)
178198
}
179-
value = collect
199+
value = collect as Any?
180200
} else {
181201
value = rawObject
182202
}
183203
}
184204
///Array access support
185-
public subscript(index: Int) -> JSONDecoder {
205+
open subscript(index: Int) -> JSONDecoder {
186206
get {
187207
if let array = self.value as? NSArray {
188208
if array.count > index {
@@ -193,36 +213,36 @@ public class JSONDecoder {
193213
}
194214
}
195215
///Dictionary access support
196-
public subscript(key: String) -> JSONDecoder {
216+
open subscript(key: String) -> JSONDecoder {
197217
get {
198218
if let dict = self.value as? NSDictionary {
199-
if let value: AnyObject = dict[key] {
219+
if let value: Any = dict[key] {
200220
return value as! JSONDecoder
201221
}
202222
}
203223
return JSONDecoder(createError("key: \(key) does not exist or this is not a Dictionary type"))
204224
}
205225
}
206226
///private method to create an error
207-
func createError(text: String) -> NSError {
227+
func createError(_ text: String) -> NSError {
208228
return NSError(domain: "JSONJoy", code: 1002, userInfo: [NSLocalizedDescriptionKey: text]);
209229
}
210230

211231
///print the decoder in a JSON format. Helpful for debugging.
212-
public func print() -> String {
232+
open func print() -> String {
213233
if let arr = self.array {
214234
var str = "["
215235
for decoder in arr {
216236
str += decoder.print() + ","
217237
}
218-
str.removeAtIndex(str.endIndex.advancedBy(-1))
238+
str.remove(at: str.characters.index(str.endIndex, offsetBy: -1))
219239
return str + "]"
220240
} else if let dict = self.dictionary {
221241
var str = "{"
222242
for (key, decoder) in dict {
223243
str += "\"\(key)\": \(decoder.print()),"
224244
}
225-
str.removeAtIndex(str.endIndex.advancedBy(-1))
245+
str.remove(at: str.characters.index(str.endIndex, offsetBy: -1))
226246
return str + "}"
227247
}
228248
if let v = value {

0 commit comments

Comments
 (0)