I am using Swift's Codable Protocol. I have shared the code.
I want the variable boss in the class Employee to acquire the type based on the personType String in the Person class. I want to use personType as discriminator. Response coming from the server will be different every time based on the personType value.
In Employee class, I have declared the boss variable with Person type. I want it to decode for type Employee if the personType string in the Person class is "Employee" and decode for type Boss if the personType string is "Boss". If it is null I simply want it to decode for type Person.
Any help would be really appreciated.
public class Person: Codable {
public let address: String
public let age: Int
public let name: String
public let uid: String
public let personType: String?
private enum CodingKeys: String, CodingKey {
case address
case age
case name
case uid
case personType
}
required public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
address = try container.decode(String.self, forKey: .address)
age = try container.decode(Int.self, forKey: .age)
name = try container.decode(String.self, forKey: .name)
uid = try container.decode(String.self, forKey: .uid)
personType = try container.decodeIfPresent(String.self, forKey: .personType)
}
}
public class Employee: Person {
public let department: String
public let dependents: [Person]?
public let salary: Int
public let workingDays: [Days]
public var boss: Person?
private enum CodingKeys: String, CodingKey {
case department
case dependents
case salary
case workingDays
case boss
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
department = try container.decode(String.self, forKey: .department)
dependents = try container.decode([Person].self, forKey: .dependents)
salary = try container.decode(Int.self, forKey: .salary)
workingDays = try container.decode([Days].self, forKey: .workingDays)
boss = try container.decode(Person.self, forKey: .boss)
try super.init(from: decoder)
}
}
public class Boss: Employee {
let promotedAt: Double
let assistant: Employee?
enum CodingKeys: String, CodingKey {
case promotedAt
case assistant
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
promotedAt = try container.decode(Double.self, forKey: .promotedAt)
assistant = try container.decodeIfPresent(Employee.self, forKey: .assistant)
try super.init(from: decoder)
}
}
For example in the following response, in the boss section, personType is set to 'Boss'. So it should be decoded to the Boss type. If it were 'Employee' it should automatically decode to Employee or if it null is should decode to 'Person'.
{ name: 'Shahid Khaliq',
age: 5147483645,
address: 'H # 531, S # 20',
uid: '123321',
salary: 20000,
department: 'Software Development',
workingDays: [ 'Monday', 'Tuesday', 'Friday' ],
boss:
{ personType: 'Boss',
assistant: null,
name: 'Zeeshan Ejaz',
age: 5147483645,
address: 'H # 531, S # 20',
uid: '123321',
birthday: '1994-02-13',
birthtime: '1994-02-13T14:01:54.000Z',
salary: 20000,
department: 'Software Development',
joiningDay: 'Saturday',
workingDays: [ 'Monday', 'Tuesday', 'Friday' ],
dependents: null,
hiredAt: 'Sun, 06 Nov 1994 08:49:37 GMT',
boss: null,
promotedAt: 1484719381 },
dependents: null,
hiredAt: 'Sun, 06 Nov 1994 08:49:37 GMT',
personType: null }
Need to change model as per current response you posted
public class Employee: Person {
public let department: String
public let dependents: [Person]?
public let salary: Int
public let workingDays:[String] //[Days]
public var boss: Person?
private enum CodingKeys: String, CodingKey {
case department
case dependents
case salary
case workingDays
case boss
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
department = try container.decode(String.self, forKey: .department)
//dependents = try container.decode([Person].self, forKey: .dependents)
dependents = try container.decodeIfPresent([Person].self, forKey: .dependents)
salary = try container.decode(Int.self, forKey: .salary)
// workingDays = try container.decode([Days].self, forKey: .workingDays)
workingDays = try container.decode([String].self, forKey: .workingDays)
// boss = try container.decode(Person.self, forKey: .boss)
boss = try container.decodeIfPresent(Person.self, forKey: .boss)
try super.init(from: decoder)
}
}
here added decodeIfPresent in few properties as it null as per current response
while decoding you can use different thing, i have used SwiftJSON to make code more readable ,
// i have saved response in Response.JSON file so can change response as per need while testing below code.
let file = Bundle.main.path(forResource: "Response", ofType: "JSON")
let dataURL = URL(fileURLWithPath: file!)
let data = try! Data(contentsOf: dataURL)
let jsonData = try! JSON(data: data)
//here JSON is struct which is part of SwiftyJSON
print("jsondata \(jsonData)")
do {
let bossDict = jsonData["boss"]
let dataBoss : Data = try! bossDict.rawData()
let bossType = bossDict["personType"].string
if let type = bossType {
if type == "Boss"{
let bossObj = try! JSONDecoder().decode(Boss.self, from: dataBoss)
print("boss name \(String(describing: bossObj.name))")
bossObj.listPropertiesWithValues()
}else{
// type == "Employee"
let emplyeeObj = try! JSONDecoder().decode(Employee.self, from: dataBoss)
print("Employee name \(String(describing: emplyeeObj.name))")
emplyeeObj.listPropertiesWithValues()
}
}else{
//type = nil
}
}catch{
print("exception \(error)")
}
You can download working demo of the same at link
DemoCodable
I would add a switch statement at the end of the init(from decoder:) in the Employee class
switch personType {
case "Boss":
boss = try container.decode(Boss.self, forKey: .boss)
case "Employee":
boss = try container.decode(Employee.self, forKey: .boss)
default:
boss = nil
}
Related
Consider the following JSON: I'm trying to decode the "teams" object.
let jsonString = """
{
"Superheroes":{
"Marvel":"107",
"DC":"106"
},
"teams":{
"106":{
"name":"Marvel",
"Superheroes":{
"890":{
"name":"Batman"
}
}
},
"107":{
"name":"DC",
"Superheroes":{
"891":{
"name":"Wonder Woman"
}
}
}
}
}
"""
I have tried something like this:
struct SuperheroResponse: Decodable {
let teams: [Team]
private enum CodingKeys: String, CodingKey {
case teams = "teams"
}
private struct DynamicCodingKeys: CodingKey {
var stringValue: String
init?(stringValue: String) {
self.stringValue = stringValue
}
var intValue: Int?
init?(intValue: Int) {
return nil
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let teamContainer = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: CodingKeys.teams)
print(teamContainer.allKeys.count)
let tempArray: [Team] = []
for key in teamContainer.allKeys {
let decodedObject = try teamContainer.decode(Team.self, forKey: DynamicCodingKeys(stringValue: key.stringValue)!)
tempArray.append(decodedObject)
}
teams = tempArray
}
}
struct Team: Decodable {
let name: String
}
I thought that first I would get the teams container, map over the keys and go on from there. Problem is teamContainer.allKeys.count is always zero.
Also the following line, results in following error: Cannot convert value of type 'SuperheroResponse.DynamicCodingKeys' to expected argument type 'SuperheroResponse.CodingKeys'
let decodedObject = try teamContainer.decode(Team.self, forKey: DynamicCodingKeys(stringValue: key.stringValue)!)
Finally I decode it as follows:
let jsonData = Data(jsonString.utf8)
let decodedResult = try! JSONDecoder().decode(SuperheroResponse.self, from: jsonData)
dump(decodedResult)
Any help would be appreciated. Ideally I would like something like SuperheroResponse -> [Team],
Team -> name, [Superhero], Superhero -> name
You just have a couple of minor mistakes. You're almost there.
The team container is keyed by DynamicCodingKeys:
let teamContainer = try container.nestedContainer(keyedBy: DynamicCodingKeys.self, // <=
forKey: .teams)
And the Teams can be decoded as using the key you're given:
let decodedObject = try teamContainer.decode(Team.self, forKey: key)
Also, tempArray needs to be var:
var tempArray: [Team] = []
Or replace that loop with a map:
teams = try teamContainer.allKeys.map {
try teamContainer.decode(Team.self, forKey: $0)
}
All together:
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let teamContainer = try container.nestedContainer(keyedBy: DynamicCodingKeys.self, forKey: .teams)
teams = try teamContainer.allKeys.map {
try teamContainer.decode(Team.self, forKey: $0)
}
}
How to do codable for Any & JSONNull from response data
Json Response Data
"userType": {
"id": "5f18J20a21n",
"name": "userName",
"name_prefix": null,
"group": []
}
UserType Model Class
struct UserType : Codable {
let id : String?
let name : String?
let namePrefix: JSONNull? // I replace JSONNull with String, Is it good way to do so?
let group : [JSONAny?]?
enum CodingKeys: String, CodingKey {
case id = "id"
case name = "name"
case namePrefix = "name_prefix"
case group = "group"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decodeIfPresent(String.self, forKey: .id)
name = try values.decodeIfPresent(String.self, forKey: .name)
namePrefix = try values.decodeIfPresent(String.self, forKey: . namePrefix)
// namePrefix = try values.decodeIfPresent(JSONNull.self, forKey: . namePrefix)
group = try values.decodeIfPresent([JSONAny].self, forKey: .group)
// Type of expression is ambiguous without more context
}
}
I try above code which gives an error
Type of expression is ambiguous without more context
This is a part of the JSON i am getting from Github in response to a request
{
"total_count": 1657,
"incomplete_results": false,
"items": [
{
"id": 68911683,
"node_id": "MDEwOlJlcG9zaXRvcnk2ODkxMTY4Mw==",
"name": "tetros",
"full_name": "daniel-e/tetros",
"private": false,
"html_url": "https://github.com/daniel-e/tetros",
"description": "Tetris that fits into the boot sector.",
"size": 171,
"stargazers_count": 677,
"watchers_count": 677,
"language": "Assembly",
}
]
}
This is my Model
struct RepoGroup:Codable {
var items:[Repo]
}
struct Repo: Codable {
var fullName:String
var stars:Int
var watchers:Int
init(url:String,star:Int,watcher:Int) {
fullName = url
stars = star
watchers = watcher
}
enum MyStructKeys: String, CodingKey {
case fullName = "full_name"
case stars = "stargazers_count"
case watchers = "watchers_count"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MyStructKeys.self)
let fullName: String = try container.decode(String.self, forKey: .fullName)
let stars: Int = try container.decode(Int.self, forKey: .stars)
let watchers: Int = try container.decode(Int.self, forKey: .watchers)
self.init(url: fullName, star: stars, watcher: watchers)
}
}
So far so good. But as soon as i add description:String field in my model, the JSON decoder inexplicably fails to parse.
Here is my parser
let model = try JSONDecoder().decode(RepoGroup.self, from: dataResponse)
I am struggling to understand what is so special about the description field. Any kind of help would be greatly appreciated. Thanks.
Description appears to be an optional field in the GitHub API, and when a repo doesn't define a description, it is coming back as a null. This means you need to make your description field a String? and switch to using decodeIfPresent to account for the fact that it is optional.
Nothing seems off about that particular JSON to not work for description. Haven't tested this, but this is what your code looks like?
struct RepoGroup:Codable {
var items:[Repo]
}
struct Repo: Codable {
var fullName:String
var stars:Int
var watchers:Int
var description:String
init(url:String,star:Int,watcher:Int,description:String) {
fullName = url
stars = star
watchers = watcher
description = description
}
enum MyStructKeys: String, CodingKey {
case fullName = "full_name"
case stars = "stargazers_count"
case watchers = "watchers_count"
case description = "description"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: MyStructKeys.self)
let fullName: String = try container.decode(String.self, forKey: .fullName)
let stars: Int = try container.decode(Int.self, forKey: .stars)
let watchers: Int = try container.decode(Int.self, forKey: .watchers)
let description: String = try container.decode(String.self, forKey: .description)
self.init(url: fullName, star: stars, watcher: watchers, description: description)
}
}
From the server I have a big JSON returned that looks something like this:
{
"id": "123",
"status": "ok",
"person": {
"administration": {
"name": "John"
}
},
"company": {
"name": "Test"
}
}
I have a struct:
struct Info: Decodable, Object {
let id: String
let status: String
let personName: String
let companyName: String
}
It conforms to Decodable protocol and also is a Object (Realm entity).
My question is: Am I able somehow to decode the name of the person in personName? Something like person.administration.name.
I want the end Realm Object, to be a flat one and mostly all of the fields are strings.
Should I create separate structs for Person/Company without being Realm Objects and in decode method to set the corresponding value to "personName"?
let personName: String = try container.decode((Person.Administration.name).self, forKey: .personName)
You can simply use containers to decode nested data with Decodable, i.e.
struct Info: Decodable {
let id: String
let status: String
let personName: String
let companyName: String
enum CodingKeys: String, CodingKey {
case id, status
case person, administration
case company
case name
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
id = try values.decode(String.self, forKey: .id)
status = try values.decode(String.self, forKey: .status)
//Decoding personName
let person = try values.nestedContainer(keyedBy: CodingKeys.self, forKey: .person)
let administration = try person.nestedContainer(keyedBy: CodingKeys.self, forKey: .administration)
personName = try administration.decode(String.self, forKey: .name)
//Decoding companyName
let company = try values.nestedContainer(keyedBy: CodingKeys.self, forKey: .company)
companyName = try company.decode(String.self, forKey: .name)
}
}
Example:
I've decoded the JSON you provided above, i.e.
if let data = json.data(using: .utf8) {
let info = try? JSONDecoder().decode(Info.self, from: data)
print(info)
}
The output it gives is:
(id: "123", status: "ok", personName: "John", companyName: "Test")
You can separate out the CodingKeys for all the different levels as per your wish. I kept them at the same level for simplicity.
Suggestion: Try using the optional types with Codable. This is because the API response can be unexpected. And if you don't get any expected key-value pair, you might end up getting a nil while creating the object.
It is best practice to separate transport types you're parsing your JSON into and types to represent object in the storage.
But if you want to use this combined types you should do something like this:
struct Info: Decodable {
let id: String
let status: String
let personName: String
let companyName: String
// JSON root keys
private enum RootKeys: String, CodingKey {
case id, status, person, company
}
// Keys for "person" nested "object"
private enum PersonKeys: String, CodingKey {
case administration
}
// Keys for "administration" and "company"
private enum NamedKeys: String, CodingKey {
case name
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: RootKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.status = try container.decode(String.self, forKey: .status)
let personContainer = try container.nestedContainer(keyedBy: PersonKeys.self, forKey: .person)
let administrationContainer = try personContainer.nestedContainer(keyedBy: NamedKeys.self, forKey: .administration)
self.personName = try administrationContainer.decode(String.self, forKey: .name)
let companyContainer = try container.nestedContainer(keyedBy: NamedKeys.self, forKey: .company)
self.companyName = try companyContainer.decode(String.self, forKey: .name)
}
}
I separated keys into three different CodingKey types for some type safety, and to prevent accidental mixup.
(This is a follow-up from this question: Using Decodable protocol with multiples keys.)
I have the following Swift code:
let additionalInfo = try values.nestedContainer(keyedBy: UserInfoKeys.self, forKey: .age)
age = try additionalInfo.decodeIfPresent(Int.self, forKey: .realage)
I know that if I use decodeIfPresent and don't have the property it will still handle it correctly if it's an optional variable.
For example the following JSON works to parse it using the code above.
{
"firstname": "Test",
"lastname": "User",
"age": {"realage": 29}
}
And the following JSON works as well.
{
"firstname": "Test",
"lastname": "User",
"age": {"notrealage": 30}
}
But the following doesn't work.
{
"firstname": "Test",
"lastname": "User"
}
How can I make all 3 examples work? Is there something similar to decodeIfPresent for nestedContainer?
You can use the following KeyedDecodingContainer function:
func contains(_ key: KeyedDecodingContainer.Key) -> Bool
Returns a Bool value indicating whether the decoder contains a value associated with the given key. The value associated with the given key may be a null value as appropriate for the data format.
For instance, to check if the "age" key exists before requesting the corresponding nested container:
struct Person: Decodable {
let firstName, lastName: String
let age: Int?
enum CodingKeys: String, CodingKey {
case firstName = "firstname"
case lastName = "lastname"
case age
}
enum AgeKeys: String, CodingKey {
case realAge = "realage"
case fakeAge = "fakeage"
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.firstName = try values.decode(String.self, forKey: .firstName)
self.lastName = try values.decode(String.self, forKey: .lastName)
if values.contains(.age) {
let age = try values.nestedContainer(keyedBy: AgeKeys.self, forKey: .age)
self.age = try age.decodeIfPresent(Int.self, forKey: .realAge)
} else {
self.age = nil
}
}
}
I had this issue and I found this solution, just in case is helpful to somebody else:
let ageContainer = try? values.nestedContainer(keyedBy: AgeKeys.self, forKey: .age)
self.age = try ageContainer?.decodeIfPresent(Int.self, forKey: .realAge)
If you have an optional container, using try? values.nestedContainer(keyedBy:forKey) you don't need to check if the container exist using contains(.
Can you try pasting your sample JSON into quicktype to see what types it infers? Based on your question, I pasted your samples and got:
struct UserInfo: Codable {
let firstname: String
let age: Age?
let lastname: String
}
struct Age: Codable {
let realage: Int?
}
Making UserInfo.age and Age.realage optionals works, if that's what you're trying to accomplish.