enum PostType: Decodable {
init(from decoder: Decoder) throws {
// What do i put here?
}
case Image
enum CodingKeys: String, CodingKey {
case image
}
}
What do i put to complete this?
Also, lets say i changed the case to this:
case image(value: Int)
How do I make this conform to Decodable?
Here is my full code (which does not work)
let jsonData = """
{
"count": 4
}
""".data(using: .utf8)!
do {
let decoder = JSONDecoder()
let response = try decoder.decode(PostType.self, from: jsonData)
print(response)
} catch {
print(error)
}
}
}
enum PostType: Int, Codable {
case count = 4
}
Also, how will it handle an enum like this?
enum PostType: Decodable {
case count(number: Int)
}
It's pretty easy, just use String or Int raw values which are implicitly assigned.
enum PostType: Int, Codable {
case image, blob
}
image is encoded to 0 and blob to 1
Or
enum PostType: String, Codable {
case image, blob
}
image is encoded to "image" and blob to "blob"
This is a simple example how to use it:
enum PostType : Int, Codable {
case count = 4
}
struct Post : Codable {
var type : PostType
}
let jsonString = "{\"type\": 4}"
let jsonData = Data(jsonString.utf8)
do {
let decoded = try JSONDecoder().decode(Post.self, from: jsonData)
print("decoded:", decoded.type)
} catch {
print(error)
}
Update
In iOS 13.3+ and macOS 15.1+ it's allowed to en-/decode fragments – single JSON values which are not wrapped in a collection type
let jsonString = "4"
let jsonData = Data(jsonString.utf8)
do {
let decoded = try JSONDecoder().decode(PostType.self, from: jsonData)
print("decoded:", decoded) // -> decoded: count
} catch {
print(error)
}
In Swift 5.5+ it's even possible to en-/decode enums with associated values without any extra code. The values are mapped to a dictionary and a parameter label must be specified for each associated value
enum Rotation: Codable {
case zAxis(angle: Double, speed: Int)
}
let jsonString = #"{"zAxis":{"angle":90,"speed":5}}"#
let jsonData = Data(jsonString.utf8)
do {
let decoded = try JSONDecoder().decode(Rotation.self, from: jsonData)
print("decoded:", decoded)
} catch {
print(error)
}
How to make enums with associated types conform to Codable
This answer is similar to #Howard Lovatt's but avoids creating a PostTypeCodableForm struct and instead uses the KeyedEncodingContainer type provided by Apple as a property on Encoder and Decoder, which reduces boilerplate.
enum PostType: Codable {
case count(number: Int)
case title(String)
}
extension PostType {
private enum CodingKeys: String, CodingKey {
case count
case title
}
enum PostTypeCodingError: Error {
case decoding(String)
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
if let value = try? values.decode(Int.self, forKey: .count) {
self = .count(number: value)
return
}
if let value = try? values.decode(String.self, forKey: .title) {
self = .title(value)
return
}
throw PostTypeCodingError.decoding("Whoops! \(dump(values))")
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .count(let number):
try container.encode(number, forKey: .count)
case .title(let value):
try container.encode(value, forKey: .title)
}
}
}
This code works for me on Xcode 9b3.
import Foundation // Needed for JSONEncoder/JSONDecoder
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let decoder = JSONDecoder()
let count = PostType.count(number: 42)
let countData = try encoder.encode(count)
let countJSON = String.init(data: countData, encoding: .utf8)!
print(countJSON)
// {
// "count" : 42
// }
let decodedCount = try decoder.decode(PostType.self, from: countData)
let title = PostType.title("Hello, World!")
let titleData = try encoder.encode(title)
let titleJSON = String.init(data: titleData, encoding: .utf8)!
print(titleJSON)
// {
// "title": "Hello, World!"
// }
let decodedTitle = try decoder.decode(PostType.self, from: titleData)
Swift would throw a .dataCorrupted error if it encounters unknown enum value. If your data is coming from a server, it can send you an unknown enum value at any time (bug server side, new type added in an API version and you want the previous versions of your app to handle the case gracefully, etc), you'd better be prepared, and code "defensive style" to safely decode your enums.
Here is an example on how to do it, with or without associated value
enum MediaType: Decodable {
case audio
case multipleChoice
case other
// case other(String) -> we could also parametrise the enum like that
init(from decoder: Decoder) throws {
let label = try decoder.singleValueContainer().decode(String.self)
switch label {
case "AUDIO": self = .audio
case "MULTIPLE_CHOICES": self = .multipleChoice
default: self = .other
// default: self = .other(label)
}
}
}
And how to use it in a enclosing struct:
struct Question {
[...]
let type: MediaType
enum CodingKeys: String, CodingKey {
[...]
case type = "type"
}
extension Question: Decodable {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
[...]
type = try container.decode(MediaType.self, forKey: .type)
}
}
To extend on #Toka's answer, you may too add a raw representable value to the enum, and use the default optional constructor to build the enum without a switch:
enum MediaType: String, Decodable {
case audio = "AUDIO"
case multipleChoice = "MULTIPLE_CHOICES"
case other
init(from decoder: Decoder) throws {
let label = try decoder.singleValueContainer().decode(String.self)
self = MediaType(rawValue: label) ?? .other
}
}
It may be extended using a custom protocol that allows to refactor the constructor:
protocol EnumDecodable: RawRepresentable, Decodable {
static var defaultDecoderValue: Self { get }
}
extension EnumDecodable where RawValue: Decodable {
init(from decoder: Decoder) throws {
let value = try decoder.singleValueContainer().decode(RawValue.self)
self = Self(rawValue: value) ?? Self.defaultDecoderValue
}
}
enum MediaType: String, EnumDecodable {
static let defaultDecoderValue: MediaType = .other
case audio = "AUDIO"
case multipleChoices = "MULTIPLE_CHOICES"
case other
}
It can also be easily extended for throwing an error if an invalid enum value was specified, rather than defaulting on a value. Gist with this change is available here: https://gist.github.com/stephanecopin/4283175fabf6f0cdaf87fef2a00c8128.
The code was compiled and tested using Swift 4.1/Xcode 9.3.
A variant of #proxpero's response that is terser would be to formulate the decoder as:
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
guard let key = values.allKeys.first else { throw err("No valid keys in: \(values)") }
func dec<T: Decodable>() throws -> T { return try values.decode(T.self, forKey: key) }
switch key {
case .count: self = try .count(dec())
case .title: self = try .title(dec())
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .count(let x): try container.encode(x, forKey: .count)
case .title(let x): try container.encode(x, forKey: .title)
}
}
This permits the compiler to exhaustively verify the cases, and also doesn't suppress the error message for the case where the encoded value doesn't match the key's expected value.
Actually the answers above are really great, but they are missing some details for what many people need in a continuously developed client/server project. We develop an app while our backend continually evolves over time, which means some enum cases will change that evolution. So we need an enum decoding strategy that is able to decode arrays of enums that contain unknown cases. Otherwise decoding the object that contains the array simply fails.
What I did is quite simple:
enum Direction: String, Decodable {
case north, south, east, west
}
struct DirectionList {
let directions: [Direction]
}
extension DirectionList: Decodable {
public init(from decoder: Decoder) throws {
var container = try decoder.unkeyedContainer()
var directions: [Direction] = []
while !container.isAtEnd {
// Here we just decode the string from the JSON which always works as long as the array element is a string
let rawValue = try container.decode(String.self)
guard let direction = Direction(rawValue: rawValue) else {
// Unknown enum value found - ignore, print error to console or log error to analytics service so you'll always know that there are apps out which cannot decode enum cases!
continue
}
// Add all known enum cases to the list of directions
directions.append(direction)
}
self.directions = directions
}
}
Bonus: Hide implementation > Make it a Collection
To hide implementation detail is always a good idea. For this you'll need just a little bit more code. The trick is to conform DirectionsList to Collection and make your internal list array private:
struct DirectionList {
typealias ArrayType = [Direction]
private let directions: ArrayType
}
extension DirectionList: Collection {
typealias Index = ArrayType.Index
typealias Element = ArrayType.Element
// The upper and lower bounds of the collection, used in iterations
var startIndex: Index { return directions.startIndex }
var endIndex: Index { return directions.endIndex }
// Required subscript, based on a dictionary index
subscript(index: Index) -> Element {
get { return directions[index] }
}
// Method that returns the next index when iterating
func index(after i: Index) -> Index {
return directions.index(after: i)
}
}
You can read more about conforming to custom collections in this blog post by John Sundell: https://medium.com/#johnsundell/creating-custom-collections-in-swift-a344e25d0bb0
You can do what you want, but it is a bit involved :(
import Foundation
enum PostType: Codable {
case count(number: Int)
case comment(text: String)
init(from decoder: Decoder) throws {
self = try PostTypeCodableForm(from: decoder).enumForm()
}
func encode(to encoder: Encoder) throws {
try PostTypeCodableForm(self).encode(to: encoder)
}
}
struct PostTypeCodableForm: Codable {
// All fields must be optional!
var countNumber: Int?
var commentText: String?
init(_ enumForm: PostType) {
switch enumForm {
case .count(let number):
countNumber = number
case .comment(let text):
commentText = text
}
}
func enumForm() throws -> PostType {
if let number = countNumber {
guard commentText == nil else {
throw DecodeError.moreThanOneEnumCase
}
return .count(number: number)
}
if let text = commentText {
guard countNumber == nil else {
throw DecodeError.moreThanOneEnumCase
}
return .comment(text: text)
}
throw DecodeError.noRecognizedContent
}
enum DecodeError: Error {
case noRecognizedContent
case moreThanOneEnumCase
}
}
let test = PostType.count(number: 3)
let data = try JSONEncoder().encode(test)
let string = String(data: data, encoding: .utf8)!
print(string) // {"countNumber":3}
let result = try JSONDecoder().decode(PostType.self, from: data)
print(result) // count(3)
Features
Simple use. One line in Decodable instance: line eg let enum: DecodableEnum<AnyEnum>
Is decoded with standard mapping mechanism: JSONDecoder().decode(Model.self, from: data)
covered case of receiving unknown data (for example, mapping a Decodable object will not fail if you receive unexpected data)
handle/deliver mapping or decoding errors
Details
Xcode 12.0.1 (12A7300)
Swift 5.3
Solution
import Foundation
enum DecodableEnum<Enum: RawRepresentable> where Enum.RawValue == String {
case value(Enum)
case error(DecodingError)
var value: Enum? {
switch self {
case .value(let value): return value
case .error: return nil
}
}
var error: DecodingError? {
switch self {
case .value: return nil
case .error(let error): return error
}
}
enum DecodingError: Error {
case notDefined(rawValue: String)
case decoding(error: Error)
}
}
extension DecodableEnum: Decodable {
init(from decoder: Decoder) throws {
do {
let rawValue = try decoder.singleValueContainer().decode(String.self)
guard let layout = Enum(rawValue: rawValue) else {
self = .error(.notDefined(rawValue: rawValue))
return
}
self = .value(layout)
} catch let err {
self = .error(.decoding(error: err))
}
}
}
Usage sample
enum SimpleEnum: String, Codable {
case a, b, c, d
}
struct Model: Decodable {
let num: Int
let str: String
let enum1: DecodableEnum<SimpleEnum>
let enum2: DecodableEnum<SimpleEnum>
let enum3: DecodableEnum<SimpleEnum>
let enum4: DecodableEnum<SimpleEnum>?
}
let dictionary: [String : Any] = ["num": 1, "str": "blablabla", "enum1": "b", "enum2": "_", "enum3": 1]
let data = try! JSONSerialization.data(withJSONObject: dictionary)
let object = try JSONDecoder().decode(Model.self, from: data)
print("1. \(object.enum1.value)")
print("2. \(object.enum2.error)")
print("3. \(object.enum3.error)")
print("4. \(object.enum4)")
A lot of good approaches here, but I have not seen one discussing enums with more than one value, although it can be deduced from examples - maybe someone can find a use for this one:
import Foundation
enum Tup {
case frist(String, next: Int)
case second(Int, former: String)
enum TupType: String, Codable {
case first
case second
}
enum CodingKeys: String, CodingKey {
case type
case first
case firstNext
case second
case secondFormer
}
}
extension Tup: Codable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
let type = try values.decode(TupType.self, forKey: .type)
switch type {
case .first:
let str = try values.decode(String.self, forKey: .first)
let next = try values.decode(Int.self, forKey: .firstNext)
self = .frist(str, next: next)
case .second:
let int = try values.decode(Int.self, forKey: .second)
let former = try values.decode(String.self, forKey: .secondFormer)
self = .second(int, former: former)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
switch self {
case .frist(let str, next: let next):
try container.encode(TupType.first, forKey: .type)
try container.encode(str, forKey: .first)
try container.encode(next, forKey: .firstNext)
case .second(let int, former: let former):
try container.encode(TupType.second, forKey: .type)
try container.encode(int, forKey: .second)
try container.encode(former, forKey: .secondFormer)
}
}
}
let example1 = Tup.frist("123", next: 90)
do {
let encoded = try JSONEncoder().encode(example1)
print(encoded)
let decoded = try JSONDecoder().decode(Tup.self, from: encoded)
print("decoded 1 = \(decoded)")
}
catch {
print("errpr = \(error.localizedDescription)")
}
let example2 = Tup.second(10, former: "dantheman")
do {
let encoded = try JSONEncoder().encode(example2)
print(encoded)
let decoded = try JSONDecoder().decode(Tup.self, from: encoded)
print("decoded 2 = \(decoded)")
}
catch {
print("errpr = \(error.localizedDescription)")
}
Here is a simple example of how to make an enum decodable in Swift.
Sample JSON:
[
{
"title": "1904",
"artist": "The Tallest Man on Earth",
"year": "2012",
"type": "hindi"
},
{
"title": "#40",
"artist": "Dave Matthews",
"year": "1999",
"type": "english"
},
{
"title": "40oz to Freedom",
"artist": "Sublime",
"year": "1996",
"type": "english"
},
{
"title": "#41",
"artist": "Dave Matthews",
"year": "1996",
"type": "punjabi"
}
]
Model struct:
struct Song: Codable {
public enum SongType: String, Codable {
case hindi = "hindi"
case english = "english"
case punjabi = "punjabi"
case tamil = "tamil"
case none = "none"
}
let title: String
let artist: String
let year: String
let type: SongType?
}
Now, you can parse the JSON file and parse the data into an array of songs like below:
func decodeJSON() {
do {
// creating path from main bundle and get data object from the path
if let bundlePath = Bundle.main.path(forResource: "sample", ofType: "json"),
let jsonData = try String(contentsOfFile: bundlePath).data(using: .utf8) {
// decoding an array of songs
let songs = try JSONDecoder().decode([Song].self, from: jsonData)
// printing the type of song
songs.forEach { song in
print("Song type: \(song.type?.rawValue ?? "")")
}
}
} catch {
print(error)
}
}
Comment below in case of any queries.
Related
I have a custom struct I want to store in AppStorage:
struct Budget: Codable, RawRepresentable {
enum CodingKeys: String, CodingKey {
case total, spent
}
var total: Double
var spent: Double
init(total: Double = 5000.0, spent: Double = 3000.0) {
self.total = total
self.spent = spent
}
init?(rawValue: String) {
guard let data = rawValue.data(using: .utf8),
let result = try? JSONDecoder().decode(Budget.self, from: data)
else { return nil }
self = result
}
var rawValue: String {
guard let data = try? JSONEncoder().encode(self),
let result = String(data: data, encoding: .utf8)
else {
return ""
}
return result
}
}
I then have the following view:
struct DemoView: View {
#AppStorage(UserDefaults.StorageKeys.budget.rawValue) var budget = Budget()
var body: some View {
Button("Update") {
budget.total = 10
}
}
}
When I tap the button the app crashes with Thread 1: EXC_BAD_ACCESS on guard let data = try? JSONEncoder().encode(self) for rawValue in Budget. What am I doing wrong here?
You are running into infinite recursion. This is because types that conforms to both Encodable and RawRepresentable automatically get this encode(to:) implementation (source), which encodes the raw value. This means that when you call JSONEncoder().encode, it would try to call the getter of rawValue, which calls JSONEncoder().encode, forming infinite recursion.
To solve this, you can implement encode(to:) explicitly:
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(total, forKey: .total)
try container.encode(spent, forKey: .spent)
}
Note that you should also implement init(from:) explicitly, because you also get a init(from:) implementation (source) that tries to decode your JSON as a single JSON string, which you certainly do not want.
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
total = try container.decode(Double.self, forKey: .total)
spent = try container.decode(Double.self, forKey: .spent)
}
I have a base model called Requirements and another more specific model called AccountRequirements.
When I try to read the currentDeadline property, if i use Requirements it works fine. If I use AccountRequirements it comes out as nil.
I do not understand why. I'm guessing it has to do somehow with the class. I always use struct in my models but since I can not inherit from a struct I'm using class here.
class Requirements: Codable {
var commonProperty: String
// works
var currentDeadline: Int?
enum CodingKeys: String, CodingKey {
case commonProperty = "common_property"
case currentDeadline = "current_deadline"
}
}
class AccountRequirements: Requirements {
// doesnt work
var currentDeadline: Int?
enum CodingKeys: String, CodingKey {
case currentDeadline = "current_deadline"
}
}
I decode data like this:
documentReference.addSnapshotListener { [self] documentSnapshot, error in
guard let document = documentSnapshot else {
self.error = error!.localizedDescription
return
}
self.user = try? document.data(as: Requirements.self)
}
If you want to decode it as the subclass then you need to give that class and not the superclass to document.data(as:). You also need to implement init(from:) for the subclass to decode it properly
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
currentDeadline = try container.decodeIfPresent(Int.self, forKey: .currentDeadline)
try super.init(from: decoder)
}
Below is an example with a hardcoded json value
let data = """
{ "common_property": "Some text",
"current_deadline": 42
}
""".data(using: .utf8)!
do {
let result = try JSONDecoder().decode(Requirements.self, from: data)
print(type(of: result), result.commonProperty)
let result2 = try JSONDecoder().decode(AccountRequirements.self, from: data)
print(type(of: result2), result2.commonProperty, result2.currentDeadline ?? "")
} catch {
print(error)
}
Requirements Some text
AccountRequirements Some text 42
Scenario: Data stream that contains an item that has changed from an Int to String type causing the JSON parser to crash.
Result: Subscriber 'sink' crashed with data type not matching the original receiving type via JSON parser.
Goal: to convert the Int values to String to have a consistent stream for a successful parsing.
Here's a snippet of the data stream that has caused the crash:
...
{
"city": "אלון שבות",
"sickCount": 124,
"actualSick": 15,
"verifiedLast7Days": " 11-14 ",
"testLast7Days": 699,
"patientDiffPopulationForTenThousands": 47
},
{
"city": "סייד (שבט)",
"sickCount": " קטן מ-15 ",
"actualSick": " קטן מ-15 ",
"verifiedLast7Days": " 0 ",
"testLast7Days": 17,
"patientDiffPopulationForTenThousands": 4
},
...
Here's the error via console:
CodingKeys(stringValue: "sickCount", intValue: nil)], debugDescription: "Expected to decode Int but found a string/data instead.", underlyingError: nil))
Here's the code:
func getData() {
let str = "https://disease.sh/v3/covid-19/gov/Israel"
let url = URL(string: str)!
let remoteDataPublisher = URLSession.shared.dataTaskPublisher(for: url)
.map(\.data)
.receive(on: DispatchQueue.main)
.decode(type: IsraelDataElement.self, decoder: JSONDecoder())
remoteDataPublisher
.eraseToAnyPublisher()
.sink(receiveCompletion: { completion in
switch completion {
case .finished:
print("{IsraelModel} Publisher Finished")
case let .failure(anError):
Swift.print("\nIsrael - Received error: #function", anError)
}
}, receiveValue: { someData in
self.add_UUID(origData: someData)
print(someData)
}).store(in: &cancellables)
}
I wasn't sure if what I was suggesting in comments wasn't clear, so here's an example of what I had in mind.
If the object you're trying to decode is:
struct IsraelDataElement {
let city: String
let sickCount: String,
let actualSick: String,
let verifiedLast7Days: String,
let testLast7Days: Int,
let patientDiffPopulationForTenThousands: Int
}
then you can manually decode it, converting renegade Ints to Strings:
extension IsraelDataElement: Decodable {
private enum CodingKeys: CodingKey {
case city, sickCount, actualSick, verifiedLast7Days //... etc
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.city = try container.decode(String.self, forKey: .city)
do {
self.sickCount = try String(container.decode(Int.self, forKey: .sickCount))
} catch DecodingError.typeMismatch {
self.sickCount try container.decode(String.self, forKey: .sickCount)
}
// and so on for other properties
}
}
Then, no further changes are needed in your Combine chain.
Swift strongly typed so it will not coerce the types for you. If you genuinely mean for the types to be heterogenous then idiomatically you should use an enum to represent that this thing is either an Int or String. This is the only way you can actually round trip the data and preserver the type. For instance:
struct Element: Decodable {
let city: String
let sickCount: Either<String, Int>
let actualSick: Either<String, Int>
let verifiedLast7Days: String
let testLast7Days: Int
let patientDiffPopulationForTenThousands: Int
}
enum Either<Left: Decodable, Right: Decodable>: Decodable {
case left(Left)
case right(Right)
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Left.self) {
self = .left(x)
} else if let x = try? container.decode(Right.self) {
self = .right(x)
} else {
throw DecodingError.typeMismatch(Self.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for \(String(describing:Self.self))"))
}
}
}
If this is just a case of your backend developers being sloppy then convert the type yourself and you can do so generically so you can easily fix their data problems everywhere in your app:
struct Element: Decodable {
let city: String
let sickCount: Int
let actualSick: Int
enum CodingKeys: CodingKey {
case city, sickCount, actualSick
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.city = try container.decode(String.self, forKey: .city)
self.sickCount = try container.decodeAndCoerce(to: Int.self, from: String.self, conversion: Int.init(_:), forKey: .sickCount)
self.actualSick = try container.decodeAndCoerce(to: Int.self, from: String.self, conversion: Int.init(_:), forKey: .actualSick)
}
}
extension KeyedDecodingContainer {
func decodeAndCoerce<Target: Decodable, Source: Decodable>(to: Target.Type, from: Source.Type, conversion: #escaping (Source) -> Target?, forKey key: Key) throws -> Target {
guard let value = (try? decode(Target.self, forKey: key)) ?? ((try? decode(Source.self, forKey: key)).flatMap(conversion)) else {
throw DecodingError.typeMismatch(Target.self, DecodingError.Context(codingPath: codingPath, debugDescription: "Expected \(String(describing: Target.self))"))
}
return value
}
}
Aren't generics fun?
I am trying to parse an array of heterogeneous objects using Codable. These objects are also unkeyed as well. I should note that I have the container structure correct, because it DOES loop through and print "it is type1" at all correct times as seen below. I just can't figure out how to access the actual object. Here is my code:
var data: [Any]
public init(from decoder: Decoder) throws {
var container = try! decoder.container(keyedBy: CodingKeys.self).nestedUnkeyedContainer(forKey: .data)
while !container.isAtEnd {
var itemContainer = try container.nestedContainer(keyedBy: CodingKeys.self)
let itemType = try itemContainer.decode(String.self, forKey: .type)
switch itemType {
case "type1":
print("it is type1")
// this does not compile, but is what I need
//let objectOfItem1 = try itemContainer.decode(Type1.self)
// this compiles, but doesn't work because there is no key with these objects
//let objectOfItem1 = try itemContainer.decode(Type1, forKey: .type)
default:
print("test:: it is the default")
}
}
}
private enum CodingKeys: String, CodingKey {
case data
case type
}
And here is the JSON I am trying to decode (many properties committed for clarity):
"contents" : {
"data" : [
{
"type" : "type1",
"id" : "6a406cdd7a9cace5"
},
{
"type" : "type2",
"id" : "ljhdgsouilghoipsu"
}
]
}
How can I correctly get my individual Type1 objects out of this structure?
I think the easy way to get around the heterogenous data is to use an enum as an interim type to wrap your various Item types (which all need to Codable):
To allow myself to test this I've changed your json slightly to give me more heterogenous data for testing. I've used:
let json = """
{
"contents": {
"data": [
{
"type": "type1",
"id": "6a406cdd7a9cace5"
},
{
"type": "type2",
"dbl": 1.01
},
{
"type": "type3",
"int": 5
}
]
}
}
and then created the three final types represented by this json
struct Item1: Codable {
let type: String
let id: String
}
struct Item2: Codable {
let type: String
let dbl: Double
}
struct Item3: Codable {
let type: String
let int: Int
}
To allow decoding the multiple types in a type-safe way (as required by Codable) you need to use a single type that can represent (or wrap) the possible options. An enum with associated values works nicely for this
enum Interim {
case type1 (Item1)
case type2 (Item2)
case type3 (Item3)
case unknown //to handle unexpected json structures
}
So far, so good, but then it gets slightly more complicated when it comes to creating the Interim from the JSON. It will need a CodingKey enum which represents all the possible keys for all the Item# types, and then it will need to decode the JSON linking all these keys to their respective types and data:
extension Interim: Decodable {
private enum InterimKeys: String, CodingKey {
case type
case id
case dbl
case int
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: InterimKeys.self)
let type = try container.decode(String.self, forKey: .type)
switch type {
case "type1":
let id = try container.decode(String.self, forKey: .id)
let item = Item1(type: type, id: id)
self = .type1(item)
case "type2":
let dbl = try container.decode(Double.self, forKey: .dbl)
let item = Item2(type: type, dbl: dbl)
self = .type2(item)
case "type3":
let int = try container.decode(Int.self, forKey: .int)
let item = Item3(type: type, int: int)
self = .type3(item)
default: self = .unknown
}
}
}
This provides the mechanism for decoding the heterogenous components, now we just need to deal with the higher-level keys. As we have a Decodable Interim type this is straightforward:
struct DataArray: Decodable {
var data: [Interim]
}
struct Contents: Decodable {
var contents: DataArray
}
This now means the json can be decoded like this...
let data = Data(json.utf8)
let decoder = JSONDecoder()
do {
let contents = try decoder.decode(Contents.self, from: data)
print(contents)
} catch {
print("Failed to decode JSON")
print(error.localizedDescription)
}
This successfully decodes the data into a nested structure where the major component is the array of Interim types with their associated Item# objects. The above produces the following output, showing these nested types:
Contents(contents: testbed.DataArray(data: [testbed.Interim.type1(testbed.Item1(type: "type1", id: "6a406cdd7a9cace5")), testbed.Interim.type2(testbed.Item2(type: "type2", dbl: 1.01)), testbed.Interim.type3(testbed.Item3(type: "type3", int: 5))]))
I think there should be an even safer way to do this with Type Erasure to provide a more extensible solution, but I've not got my head around that fully yet.
I think you need use this structure:
struct A: Codable {
let contents: B?
enum CodingKeys: String, CodingKey {
case contents
}
struct B: Codable {
let data: [C]?
enum CodingKeys: String, CodingKey {
case data
}
struct C: Codable {
let type : String?
let id : String?
}
}
}
extension A {
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let contents = try container.decodeIfPresent(B.self, forKey: .contents)
self.init(contents: contents)
}
}
I'd like to add to flanker's answer an improvement for cleaner approach to avoid having all possible keys to be stored under Interim's CodingKey. Here is an updated Interim
enum Interim: Decodable {
case item1(Item1)
case item2(Item2)
case item3(Item3)
case unknown
init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: Key.self)
// Fallback for any unsupported types
guard let type = try? typeContainer.decode(ItemType.self, forKey: .type) else {
self = .unknown
return
}
// Let corresponding Decodable Item to be initialized from the same decoder.
switch type {
case .type1: self = .item1(try .init(from: decoder))
case .type2: self = .item2(try .init(from: decoder))
case .type3: self = .item3(try .init(from: decoder))
}
}
/// These are values for Item.type
private enum ItemType: String, Decodable {
case type1
case type2
case type3
}
private enum Key: CodingKey {
case type
}
}
I need to parse JSON of which one of the field value is either an array:
"list" :
[
{
"value" : 1
}
]
or an empty string, in case there's no data:
"list" : ""
Not nice, but I can't change the format.
I'm looking at converting my manual parsing, for which this was easy, to JSONDecoder and Codable struct's.
How can I handle this nasty inconsistency?
You need to try decoding it one way, and if that fails, decode it the other way. This means you can't use the compiler-generated decoding support. You have to do it by hand. If you want full error checking, do it like this:
import Foundation
struct ListItem: Decodable {
var value: Int
}
struct MyResponse: Decodable {
var list: [ListItem] = []
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
do {
list = try container.decode([ListItem].self, forKey: .list)
} catch {
switch error {
// In Swift 4, the expected type is [Any].self, but I think it will be [ListItem].self in a newer Swift with conditional conformance support.
case DecodingError.typeMismatch(let expectedType, _) where expectedType == [Any].self || expectedType == [ListItem].self:
let dummyString = try container.decode(String.self, forKey: .list)
if dummyString != "" {
throw DecodingError.dataCorruptedError(forKey: .list, in: container, debugDescription: "Expected empty string but got \"\(dummyString)\"")
}
list = []
default: throw error
}
}
}
enum CodingKeys: String, CodingKey {
case list
}
}
If you want no error checking, you can shorten init(from:) to this:
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
list = (try? container.decode([ListItem].self, forKey: .list)) ?? []
}
Test 1:
let jsonString1 = """
{
"list" : [ { "value" : 1 } ]
}
"""
print(try! JSONDecoder().decode(MyResponse.self, from: jsonString1.data(using: .utf8)!))
Output 1:
MyResponse(list: [__lldb_expr_82.ListItem(value: 1)])
Test 2:
let jsonString2 = """
{
"list" : ""
}
"""
print(try! JSONDecoder().decode(MyResponse.self, from: jsonString2.data(using: .utf8)!))
Output 2:
MyResponse(list: [])