Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 822a0bd

Browse files
Nadohsmatthewcheok
authored andcommitted
Fix nesting (matthewcheok#40)
* update readme 1) remove unnecessary JSON example 2) re-order Enconding/Decoding sections 3) added section on enconding nested arrays/dictionaries 4) added note on extending initialiers into Classes * added decode cases and tests for handling double nested array items. i.e. [[Double]] * expanded test for [[JSONDecodable]] examples
1 parent 5919242 commit 822a0bd

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

JSONCodable.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
5211CD0A1CE2EBFB0097F255 /* NestItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5211CD091CE2EBFB0097F255 /* NestItem.swift */; };
1011
52E8F44F1C9087D200F40F7F /* UtilityTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52E8F44E1C9087D200F40F7F /* UtilityTests.swift */; };
1112
9E455BFA1BCE185B00070A4F /* EnumTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E455BF91BCE185B00070A4F /* EnumTests.swift */; };
1213
9E455BFC1BCE185B00070A4F /* JSONCodable.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9EDB39231B59D01D00C63019 /* JSONCodable.framework */; };
@@ -45,6 +46,7 @@
4546
/* End PBXContainerItemProxy section */
4647

4748
/* Begin PBXFileReference section */
49+
5211CD091CE2EBFB0097F255 /* NestItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NestItem.swift; sourceTree = "<group>"; };
4850
52E8F44E1C9087D200F40F7F /* UtilityTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UtilityTests.swift; sourceTree = "<group>"; };
4951
9E455BF71BCE185B00070A4F /* JSONCodableTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = JSONCodableTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5052
9E455BF91BCE185B00070A4F /* EnumTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnumTests.swift; sourceTree = "<group>"; };
@@ -108,6 +110,7 @@
108110
9E455C061BCE1D0B00070A4F /* Models */ = {
109111
isa = PBXGroup;
110112
children = (
113+
5211CD091CE2EBFB0097F255 /* NestItem.swift */,
111114
9E455C021BCE1C1E00070A4F /* Fruit.swift */,
112115
9E8E07231BD3F15800F98421 /* Food.swift */,
113116
9E455C041BCE1D0700070A4F /* User.swift */,
@@ -315,6 +318,7 @@
315318
buildActionMask = 2147483647;
316319
files = (
317320
52E8F44F1C9087D200F40F7F /* UtilityTests.swift in Sources */,
321+
5211CD0A1CE2EBFB0097F255 /* NestItem.swift in Sources */,
318322
9ECF00C41BCF82F5008D557C /* HelperTests.swift in Sources */,
319323
9ECF00C21BCF6E43008D557C /* ImageAsset.swift in Sources */,
320324
9E455C031BCE1C1E00070A4F /* Fruit.swift in Sources */,

JSONCodable/JSONDecodable.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,39 @@ public class JSONDecoder {
234234
}
235235
return try array.flatMap { try Element(object: $0)}
236236
}
237+
238+
// [[JSONDecodable]]
239+
public func decode<Element: JSONDecodable>(key: String) throws -> [[Element]] {
240+
guard let value = get(key) else {
241+
return []
242+
}
243+
guard let array = value as? [[JSONObject]] else {
244+
throw JSONDecodableError.ArrayTypeExpectedError(key: key, elementType: value.dynamicType)
245+
}
246+
var res:[[Element]] = []
247+
248+
for x in array {
249+
let nested = try x.flatMap { try Element(object: $0)}
250+
res.append(nested)
251+
}
252+
return res
253+
}
254+
255+
// [[JSONCompatible]]
256+
public func decode<Element: JSONCompatible>(key: String) throws -> [[Element]] {
257+
guard let value = get(key) else {
258+
return []
259+
}
260+
guard let array = value as? [[Element]] else {
261+
throw JSONDecodableError.IncompatibleTypeError(key: key, elementType: value.dynamicType, expectedType: [Element].self)
262+
}
263+
var res:[[Element]] = []
264+
265+
for x in array {
266+
res.append(x)
267+
}
268+
return res
269+
}
237270

238271
// [Enum]
239272
public func decode<Enum: RawRepresentable>(key: String) throws -> [Enum] {

JSONCodableTests/NestItem.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// NestItem.swift
3+
// JSONCodable
4+
//
5+
// Created by FoxRichard on 5/9/16.
6+
//
7+
//
8+
9+
import Foundation
10+
import JSONCodable
11+
12+
struct NestItem {
13+
let areas: [[Float]]
14+
var places: [[String]]?
15+
var business: [[Company]]
16+
var assets: [[ImageAsset]]?
17+
}
18+
19+
extension NestItem: JSONDecodable {
20+
init(object: JSONObject) throws {
21+
do {
22+
let decoder = JSONDecoder(object: object)
23+
areas = try decoder.decode("areas")
24+
places = try decoder.decode("places")
25+
business = try decoder.decode("business")
26+
assets = try decoder.decode("assets")
27+
}catch{
28+
fatalError("\(error)")
29+
}
30+
}
31+
32+
}
33+

JSONCodableTests/RegularTests.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@
99
import XCTest
1010

1111
class RegularTests: XCTestCase {
12+
13+
let nestedCodableArray = ["areas" : [[10.0,10.5,12.5]],
14+
"places":[["Tokyo","New York", "El Cerrito"]],
15+
"business" : [[
16+
[ "name": "Apple",
17+
"address": "1 Infinite Loop, Cupertino, CA"
18+
],
19+
[ "name": "Propeller",
20+
"address": "1212 broadway, Oakland, CA"
21+
]
22+
]],
23+
"assets": [[
24+
[ "name": "image-name",
25+
"uri": "http://www.example.com/image.png"
26+
],
27+
[ "name": "image2-name",
28+
"uri": "http://www.example.com/image2.png"
29+
]
30+
]]]
1231

1332
let encodedNestedArray = [
1433
"id": 99,
@@ -47,6 +66,32 @@ class RegularTests: XCTestCase {
4766
friendsLookup: ["Bob Jefferson": User(id: 27, likes:0, name: "Bob Jefferson", email: nil, company: nil, friends: [], friendsLookup: nil)]
4867
)
4968

69+
func testDecodeNestedCodableArray() {
70+
guard let nested = try? NestItem(object: nestedCodableArray) else {
71+
XCTFail()
72+
return
73+
}
74+
print("nested=",nested)
75+
let places = nested.places ?? [[]]
76+
let areas = nested.areas
77+
let business = nested.business
78+
let assets = nested.assets ?? [[]]
79+
XCTAssert(places == [["Tokyo","New York", "El Cerrito"]], "\(nestedCodableArray))")
80+
XCTAssert(areas == [[10.0,10.5,12.5]], "\(nestedCodableArray))")
81+
82+
XCTAssert(business.map{ $0.map{ $0.name } } == [[try! Company(object:["name": "Apple",
83+
"address": "1 Infinite Loop, Cupertino, CA"]),
84+
try! Company(object:[ "name": "Propeller",
85+
"address": "1212 broadway, Oakland, CA"])].map{ $0.name }],
86+
"\(nestedCodableArray))")
87+
88+
XCTAssert(assets.map{ $0.map{ $0.name } } == [[try! ImageAsset(object:[ "name": "image-name",
89+
"uri": "http://www.example.com/image.png"]),
90+
try! ImageAsset(object: ["name": "image2-name",
91+
"uri": "http://www.example.com/image2.png"])].map{ $0.name }],
92+
"\(nestedCodableArray))")
93+
}
94+
5095
func testDecodingNestedArray() {
5196
guard let user = try? User(object: encodedNestedArray) else {
5297
XCTFail()

0 commit comments

Comments
 (0)