Skip to content

Commit c565520

Browse files
committed
added decode using the type
1 parent 1d7c23a commit c565520

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

Main/Source/Decoder.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ extension Decoder {
4444
return nil
4545
}
4646

47+
public func decode<T: Encodable>(forKey key: String, type: T.Type) -> T? {
48+
if let decoder = self.decoder(forKey: key) {
49+
return T(decoder: decoder)
50+
}
51+
return nil
52+
}
53+
4754
public func decodeArray<T: Encodable>(forKey key: String, type: T.Type) -> [T]? {
4855
return self.decodeArray(forKey: key) { decoder in
4956
return T(decoder: decoder)

Main/Source/JSON/JSONDecoder.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
import Foundation
77

8-
open class JSONDecoder : Decoder {
8+
open class JSONDecoder: Decoder {
99

10-
var data : [String: Any]
10+
var data: [String: Any]
1111

1212
public convenience init(jsonString: String) {
1313
self.init(jsonString: jsonString, defaultValues: [:])
@@ -37,24 +37,24 @@ open class JSONDecoder : Decoder {
3737
}
3838

3939

40-
public func decode<T:Encodable>(type: T.Type) -> T? {
40+
public func decode<T: Encodable>(type: T.Type) -> T? {
4141
return T(decoder: self)
4242
}
4343

4444

4545
public func decoder(forKey key: String) -> Decoder? {
46-
if let value = data[key] as? [String : Any]{
47-
let decoder = JSONDecoder(data:value)
46+
if let value = data[key] as? [String: Any] {
47+
let decoder = JSONDecoder(data: value)
4848
return decoder
4949
}
5050
return nil
5151
}
5252

5353
public func decodeArray<T>(forKey key: String, closure: DecodeClosure<T>) -> [T]? {
54-
if let dataArray = data[key] as? [[String : Any]] {
54+
if let dataArray = data[key] as? [[String: Any]] {
5555
var resultArray = [T]()
5656
for data in dataArray {
57-
let decoder = JSONDecoder(data:data)
57+
let decoder = JSONDecoder(data: data)
5858
if let object = closure(decoder) {
5959
resultArray.append(object)
6060
}
@@ -130,7 +130,7 @@ open class JSONDecoder : Decoder {
130130
}
131131
return nil
132132
}
133-
133+
134134
public func dictionary(forKey key: String) -> [String: Any]? {
135135
if let result = data[key] as? [String: Any] {
136136
return result

Test/Source/JSON/JSONDecoderTest.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ class JSONDecoderTest: XCTestCase {
166166
assertThat(result?.topLeft.y, presentAnd(equalTo(1)))
167167
}
168168

169+
func test_decode_encodeable_() {
170+
let quadrilateral = Quadrilateral(topLeft: CGPoint(x: 1, y: 1), topRight: CGPoint(x: 5, y: 1), bottomLeft: CGPoint(x: 2, y: 5), bottomRight: CGPoint(x: 6, y: 6))
171+
let coder = JSONCoder()
172+
coder.encode(quadrilateral, forKey: "crop")
173+
174+
let decoder = JSONDecoder(jsonString: coder.jsonString)
175+
176+
let result = decoder.decode(forKey: "crop", type: Quadrilateral.self)
177+
178+
assertThat(result, present())
179+
assertThat(result?.topLeft.x, equalTo(1))
180+
assertThat(result?.topLeft.y, equalTo(1))
181+
}
169182

170183
func test_decode_with_default_values() {
171184
let decoder = OBCoder.JSONDecoder(jsonString: "{}", defaultValues: ["string": "Test"])

0 commit comments

Comments
 (0)