This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Death Star": { | |
"model": "DS-1 Orbital Battle Station", | |
"movie": "Star Wars: Episode VI – Return of the Jedi" | |
}, | |
"Millennium Falcon": { | |
"model": "YT-1300 light freighter", | |
"movie": "Star Wars: Episode V – The Empire Strikes Back" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
do { | |
let model = try jsonDecoder.decode(BattleShip.self, from: jsonData!) | |
print(model) | |
} catch DecodingError.dataCorrupted(let context) { | |
print(context.debugDescription) | |
} catch DecodingError.keyNotFound(let key, let context) { | |
print("\(key.stringValue) was not found, \(context.debugDescription)") | |
} catch DecodingError.typeMismatch(let type, let context) { | |
print("\(type) was expected, \(context.debugDescription)") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"name": "Star Wars: Episode IV – A New Hope", | |
"director": "George Lucas" | |
}, | |
{ | |
"name": "Star Wars: Episode VI – Return of the Jedi", | |
"director": "Richard Marquand" | |
} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Custom Date Decoding | |
jsonDecoder.dateDecodingStrategy = .custom({ (decoder) -> Date in | |
let data = try decoder.singleValueContainer().decode(String.self) | |
//perform your operation on obtained string | |
let disturbance = "01-07-1977" | |
let formatter = DateFormatter() | |
formatter.dateFormat = "dd-MM-yyyy" | |
if data == "First disturbance in force" { | |
return formatter.date(from: disturbance) ?? Date() | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BattleShip { | |
var name: String | |
var created: String | |
var movieName: String | |
var director: String | |
enum CodingKeys: String, CodingKey { | |
case name | |
case created | |
case film |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Death Star", | |
"created": "2014-12-10T16:36:50.509000Z", | |
"film": { | |
"name": "Star Wars: Episode IV – A New Hope", | |
"director": "George Lucas" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
decoder.nonConformingFloatDecodingStrategy = .convertFromString( | |
positiveInfinity: "+Infinity", | |
negativeInfinity: "-Infinity", | |
nan: "NaN" | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BattleShip: Codable { | |
//... | |
var url: URL | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct BattleShip: Codable { | |
//... | |
var created: Date | |
//... | |
} | |
//... | |
// Let's decode | |
let decoder = JSONDecoder() | |
do { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let decoder = JSONDecoder() | |
do { | |
let films = try decoder.decode([Film].self, from: jsonData) | |
} catch { | |
print("parse error") | |
} |