Skip to content

Instantly share code, notes, and snippets.

@froggomad
Last active February 12, 2020 14:15
Show Gist options
  • Save froggomad/991bdf1f29bbec46b2403ec8a9e577b3 to your computer and use it in GitHub Desktop.
Save froggomad/991bdf1f29bbec46b2403ec8a9e577b3 to your computer and use it in GitHub Desktop.
Intermediate Decoding reference
{
"artist" : "Weezer",
"coverArt" : [ {
"url" : "https://lastfm-img2.akamaized.net/i/u/174s/1918fe81bb68441d96b2247682bfda21.png"
} ],
"genres" : [ "Alternative" ],
"id" : "5E58FA0F-7DBD-4F1D-956F-89756CF1EB22",
"name" : "Weezer (The Blue Album)",
"songs" : [ {
"duration" : {
"duration" : "3:25"
},
"id" : "82BDE132-E492-4FED-9A77-B49CADBC2BFD",
"name" : {
"title" : "My Name Is Jonas"
}
}, {
"duration" : {
"duration" : "3:05"
},
"id" : "6E8AC92B-ABB4-4303-89CC-51020CEBB557",
"name" : {
"title" : "No One Else"
}
}, {
"duration" : {
"duration" : "4:19"
},
"id" : "0A856A54-2E15-4D4A-9B4B-1870F7783EBE",
"name" : {
"title" : "The World Has Turned And Left Me Here"
}
}, {
"duration" : {
"duration" : "2:39"
},
"id" : "7119F603-B37E-40B5-968D-E0091A610765",
"name" : {
"title" : "Buddy Holly"
}
}, {
"duration" : {
"duration" : "5:06"
},
"id" : "DDECE717-E075-4A64-9A9F-68CB2A5A58B6",
"name" : {
"title" : "Undone (The Sweater Song)"
}
}, {
"duration" : {
"duration" : "3:06"
},
"id" : "3FD6E647-4DF1-48CF-8E76-B932DF773ED3",
"name" : {
"title" : "Surf Wax America"
}
}, {
"duration" : {
"duration" : "4:17"
},
"id" : "214FB379-4032-4310-8B7C-C69BA21C4394",
"name" : {
"title" : "Say It Ain’t So"
}
}, {
"duration" : {
"duration" : "3:56"
},
"id" : "E4ECC640-120D-4A35-8938-3D83DE031430",
"name" : {
"title" : "In The Garage"
}
}, {
"duration" : {
"duration" : "3:25"
},
"id" : "34C44D53-6B14-40F7-8438-F147BD2B321D",
"name" : {
"title" : "Holiday"
}
}, {
"duration" : {
"duration" : "7:59"
},
"id" : "BA71E72D-E148-4D81-9A11-D7EA048F2949",
"name" : {
"title" : "Only In Dreams"
}
} ]
}
import Foundation
struct Album: Decodable {
let artist: String
let coverArt: [URL]
let genres: [String]
let id: UUID
let name: String
let songs: [Song]
enum AlbumKeys: String, CodingKey {
case artist
case coverArt
case genres
case id
case name
case songs
}
enum CoverArtKey: String, CodingKey {
case url
}
init(from decoder: Decoder) throws {
do {
let albumContainer = try decoder.container(keyedBy: AlbumKeys.self)
artist = try albumContainer.decode(String.self, forKey: .artist)
var coverArtContainer = try albumContainer.nestedUnkeyedContainer(forKey: .coverArt)
var coverArtUrls = [URL]()
let urlContainer = try coverArtContainer.nestedContainer(keyedBy: CoverArtKey.self)
while !coverArtContainer.isAtEnd {
coverArtUrls.append(try urlContainer.decode(URL.self, forKey: .url))
}
coverArt = coverArtUrls
genres = try albumContainer.decode([String].self, forKey: .genres)
id = try albumContainer.decode(UUID.self, forKey: .id)
name = try albumContainer.decode(String.self, forKey: .name)
songs = try albumContainer.decode([Song].self, forKey: .songs)
} catch {
throw error
}
}
struct Song: Decodable {
let duration: String
let id: UUID
let title: String
enum SongKeys: String, CodingKey {
case id
case title = "name"
case duration
}
enum TitleKey: String, CodingKey {
case title
}
enum DurationKeys: String, CodingKey {
case duration
}
init(from decoder: Decoder) throws {
let songContainer = try decoder.container(keyedBy: SongKeys.self)
do {
let titleContainer = try songContainer.nestedContainer(keyedBy: TitleKey.self, forKey: .title)
title = try titleContainer.decode(String.self, forKey: .title)
id = try songContainer.decode(UUID.self, forKey: .id)
let durationContainer = try songContainer.nestedContainer(keyedBy: DurationKeys.self, forKey: .duration)
duration = try durationContainer.decode(String.self, forKey: .duration)
} catch {
throw error
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment