Make class conforming to Codable by default in Swift - json

Such feature of Swift as Codable (Decodable&Encodable) protocol is very useful.
But I found such issue:
Let we have class Parent conforming to Codable:
class Parent: Codable {
var name: String
var email: String?
var password: String?
}
Ok, that class is conforming to Codable protocol "from the box", you don't need write any initializers, it's ready to be initialized from JSON like that:
{ "name": "John", "email": "johndoe#yahoo.com", "password": <null>}
But let's say we need other class, Child inherits from Parent and be conforming to Codable:
class Child: Parent {
var token: String
var date: Date?
}
so class Child must be conforming to Codable by conformance to Parent,
BUT properties of class Child won't be initialized from JSON properly.
Decision I found is write all Codable stuff for class Child by myself, like:
class Child: Parent {
var token: String
var date : Date?
enum ChildKeys: CodingKey {
case token, date
}
required init(from decoder: Decoder) throws {
try super.init(from: decoder)
let container = try decoder.container(keyedBy: ChildKeys.self)
self.token = try container.decode(String.self, forKey: .token)
self.date = try container.decodeIfPresent(Date.self, forKey: .date)
}
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: ChildKeys.self)
try container.encode(self.token, forKey: .token)
try container.encodeIfPresent(self.date, forKey: .date)
}
}
But I feel it can't be right, did I missed something? How to make class Child conforming to Codable properly without writing all that stuff?

Here's a good blog post which includes an answer to your question: source
Scroll down to inheritance and you'll see the following:
Assuming we have the following classes:
class Person : Codable {
var name: String?
}
class Employee : Person {
var employeeID: String?
}
We get the Codable conformance by inheriting from the Person class, but what happens if we try to encode an instance of Employee?
let employee = Employee()
employee.employeeID = "emp123"
employee.name = "Joe"
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(employee)
print(String(data: data, encoding: .utf8)!)
{
"name" : "Joe"
}
This is not the expected result, so we have to add a custom implementation like this:
class Person : Codable {
var name: String?
private enum CodingKeys : String, CodingKey {
case name
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
}
}
Same story for the subclass:
class Employee : Person {
var employeeID: String?
private enum CodingKeys : String, CodingKey {
case employeeID = "emp_id"
}
override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(employeeID, forKey: .employeeID)
}
}
The result would be:
{
"emp_id" : "emp123"
}
Which again is not the expected result, so here we are using inheritance by calling super.
// Employee.swift
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(employeeID, forKey: .employeeID)
}
Which finally gives us what we really wanted from the beginning:
{
"name": "Joe",
"emp_id": "emp123"
}
If you're not happy with the flattened result, there's a tip on how to avoid that too.
All the credits to the guy who wrote the blog post and my thanks.
Hope it helps you as well, cheers!

Related

How to decode an array of inherited classes in Swift

The problem: decode an array of objects belonging to Parent and Child classes.
I read a lot of stuff on this subject but I have not been able to find a simple solution.
I encoded a type property which provide the information of the original class, but I haven't found a way to use it in decoding the object.
class Parent: Codable, CustomDebugStringConvertible {
var debugDescription: String {
return "[\(name)]"
}
var name: String
init(name: String) {
self.name = name
}
enum CodingKeys: CodingKey {
case name
case type
case age
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try! container.decode(String.self, forKey: .name)
let type = try! container.decode(String.self, forKey: .type)
print("Reading \(type)")
if type == "Child" {
try Child.init(from: decoder)
return
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode("Parent", forKey: .type)
try container.encode(name, forKey: .name)
}
}
class Child: Parent {
override var debugDescription: String {
return "[\(name) - \(age)]"
}
var age: Int
init(name: String, age: Int) {
self.age = age
super.init(name: name)
}
enum CodingKeys: CodingKey {
case name
case age
case type
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
age = try! container.decode(Int.self, forKey: .age)
let name = try! container.decode(String.self, forKey: .name)
super.init(name: name) // I think the problem is here!
}
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode("Child", forKey: .type)
try container.encode(age, forKey: .age)
}
}
This is the test code.
let array = [Parent(name: "p"), Child(name: "c",age: 2)]
print(array)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let decoder = JSONDecoder()
do {
let jsonData = try encoder.encode(array)
let s = String(data: jsonData, encoding: .ascii)
print("Json Data \(s!)")
let decodedArray = try decoder.decode([Parent].self, from: jsonData)
print(decodedArray)
}
catch {
print(error.localizedDescription)
}
The output of the original array is:
[[p], [c - 2]]
The output of the decode array is:
[[p], [c]]
How do I change the Parent and/or the Child init function in order to correctly decode the object?
Clearly, my actual scenario is much more complex of this. I have to encode / decode a class which contains an array of classes with inheritance. I have tried to use this:
https://github.com/IgorMuzyka/Type-Preserving-Coding-Adapter
Apparently, it works fine on an array of Parent, Child but it doesn't if the array is inside another class.
Moreover, I would like to learn a solution to reuse in other cases and avoid including external library is not strictly needed.
I think a major part of the problem is that you are using an array of mixed types, [Any], and then you are decoding it as one type Parent because it is quite possible to get the child objects to be properly encoded as Child.
One solution is to create a new Codable struct that holds the array and that with the use of a type property keeps track on how to decode the objects in the array
enum ObjectType: String, Codable {
case parent
case child
}
struct ParentAndChild: Codable {
let objects: [Parent]
enum CodingKeys: CodingKey {
case objects
}
enum ObjectTypeKey: CodingKey {
case type
}
init(with objects: [Parent]) {
self.objects = objects
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
var objectsArray = try container.nestedUnkeyedContainer(forKey: CodingKeys.objects)
var items = [Parent]()
var array = objectsArray
while !objectsArray.isAtEnd {
let object = try objectsArray.nestedContainer(keyedBy: ObjectTypeKey.self)
let type = try object.decode(ObjectType.self, forKey: ObjectTypeKey.type)
switch type {
case .parent:
items.append(try array.decode(Parent.self))
case .child:
items.append(try array.decode(Child.self))
}
}
self.objects = items
}
}
I have also made some changes to the classes as well, the Parent class is hugely simplified and the Child class has modified functionality for encoding/decoding where the main change is that init(from:) calls supers init(from:)
class Parent: Codable, CustomDebugStringConvertible {
var debugDescription: String {
return "[\(name)]"
}
var name: String
init(name: String) {
self.name = name
}
}
class Child: Parent {
override var debugDescription: String {
return "[\(name) - \(age)]"
}
var age: Int
init(name: String, age: Int) {
self.age = age
super.init(name: name)
}
enum CodingKeys: CodingKey {
case age
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
age = try container.decode(Int.self, forKey: .age)
try super.init(from: decoder)
}
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(age, forKey: .age)
}
}

Swift encode class without nesting super class

Summary:
I would like to encode all fields of my super class without nesting them in the json result.
Here's what I mean:
Let's say we have these structs:
struct Toy: Codable {
var name: String
}
class BasicEmployee: Codable {
var name: String
var id: Int
init(name: String, id: Int) {
self.name = name
self.id = id
}
}
class GiftEmployee: BasicEmployee {
var birthday: Date
var toy: Toy
enum CodingKeys: CodingKey {
case employee, birthday, toy
}
init(name: String, id: Int, birthday: Date, toy: Toy) {
self.birthday = birthday
self.toy = toy
super.init(name: name, id: id)
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
birthday = try container.decode(Date.self, forKey: .birthday)
toy = try container.decode(Toy.self, forKey: .toy)
let baseDecoder = try container.superDecoder(forKey: .employee)
try super.init(from: baseDecoder)
}
override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(birthday, forKey: .birthday)
try container.encode(toy, forKey: .toy)
let baseEncoder = container.superEncoder(forKey: .employee)
try super.encode(to: baseEncoder)
}
}
Now we decide to encode a GiftEmployee object like so:
let encoder = JSONEncoder()
let decoder = JSONDecoder()
let giftEmployee = GiftEmployee(name: "John Appleseed", id: 7, birthday: Date(), toy: Toy(name: "Teddy Bear"))
let giftData = try encoder.encode(giftEmployee)
let giftString = String(data: giftData, encoding: .utf8)!
Printing out giftString gives us the following output:
{"toy":{"name":"Teddy Bear"},"employee":{"name":"John Appleseed","id":7},"birthday":597607945.92342305}
As you can see, all properties of our BasicEmployee super class are nested inside the "employee" json field.
However, I don't want that. I would like the output of giftString to be the following:
{"toy":{"name":"Teddy Bear"},"name":"John Appleseed","id":7,"birthday":597607945.92342305}
The properties of the BasicEmployee struct should not be nested and be on the same level as the encoded properties of the GiftEmployee struct.
Note
I am aware that I could avoid all the trouble by changing the structure of the structs, however, this is not a possibility right now.
I would greatly appreciate any help I could get on my issue.
You can call super.init(from:) and super.encode(to:):
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
birthday = try container.decode(Date.self, forKey: .birthday)
toy = try container.decode(Toy.self, forKey: .toy)
super.init(from: decoder)
}
override func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(birthday, forKey: .birthday)
try container.encode(toy, forKey: .toy)
try super.encode(to: encoder)
}

Swift can't access part of JSON response with enum initializer

I have trouble accessing a part of a JSON response.
(part of)The Source:
{
"time":1552726518,
"result":"success",
"errors":null,
"responce":{
"categories":{
"1":{
"nl":{
"name":"New born",
"description":"TEST",
"children":[
{
"name":"Unisex",
"description":"TESTe",
"children":[
{
"name":"Pants",
"description":"TEST",
"children":false
}
]
}
]
}
}
}
}
}
Approach
As you can see the source can have multiple categories. A single categorie will have a 'name', 'description' and may have 'children'. Children wil have a 'name', 'description' and also may have 'children'. This might go endless. If there are no children the SJON statement is 'false'
I use the website: https://app.quicktype.io to generate a junk of code to parse the JSON. I modified the result, because the website doesn't know that the children can go endless:
struct ProductCategories: Codable {
let time: Int?
let result: String?
let errors: [String]?
let responce: ProductCategoriesResponce?
init(time: Int? = nil, result: String? = nil, errors: [String]? = nil, responce: ProductCategoriesResponce? = nil) {
self.time = time
self.result = result
self.errors = errors
self.responce = responce
}
}
struct ProductCategoriesResponce: Codable {
let categories: [String: Category]?
}
struct Category: Codable {
let nl, en: Children?
}
struct Children: Codable {
let name, description: String?
let children: EnChildren?
}
enum EnChildren: Codable {
case bool(Bool)
case childArray([Children])
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let x = try? container.decode(Bool.self) {
self = .bool(x)
return
}
if let x = try? container.decode([Children].self) {
self = .childArray(x)
return
}
throw DecodingError.typeMismatch(EnChildren.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for EnChildren"))
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .bool(let x):
try container.encode(x)
case .childArray(let x):
try container.encode(x)
}
}
}
And I can decode de data with:
productCategories = try JSONDecoder().decode(ProductCategories.self, from: jsonData!)
This part works fine. I can access "New Born", but can't get access his children.
I search a long time for the answer I tried so much. To much to all share here. What I expect to get access is:
if let temp = productCategories.responce?.categories?["\(indexPath.item)"]?.nl?.children! {
let x = temp(from: Decoder)
but this will trow me an error:
"Cannot call value of non-function type 'EnChildren'"
also code like:
let temp1 = productCategories.responce?.categories?["\(indexPath.item)"]?.nl?.children
Won't get me anywhere.
So, any ideas? Thank you.
First of all: If you are responsible for the server side, send null for the leaf children or omit the key rather than sending false. It makes the decoding process so much easier.
app.quicktype.io is a great resource but I disagree with its suggestion.
My solution uses a regular struct Children with a custom initializer. This struct can be used recursively.
The key children is decoded conditionally. First decode [Children], on failure decode Bool and set children to nil or hand over the error.
And I declared struct members as much as possible as non-optional
struct Response: Decodable {
let time: Date
let result: String
let errors: [String]?
let responce: ProductCategories
}
struct ProductCategories: Decodable {
let categories: [String: Category]
}
struct Category: Decodable {
let nl: Children
}
struct Children: Decodable {
let name, description: String
let children : [Children]?
private enum CodingKeys : String, CodingKey { case name, description, children }
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
description = try container.decode(String.self, forKey: .description)
do {
children = try container.decode([Children].self, forKey: .children)
} catch DecodingError.typeMismatch {
_ = try container.decode(Bool.self, forKey: .children)
children = nil
}
}
}
In the root object the key time can be decoded to Date if you specify an appropriate date decoding strategy
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .secondsSince1970
let result = try decoder.decode(Response.self, from: jsonData!)
And you can access for example the name Unisex with
result.responce.categories["1"]?.nl.children?[0].name

Swift - JSONDecoder - decoding generic nested classes

I am very new to Swift and I am trying to create a tree of classes to model my data. I want to use JSONEncoder and JSONDecoder to send and receive objects. I have an issue when decoding generic classes inside another object (nested) because in the init(from: decoder) method I do not have access to other properties that could help me.
In my code:
NestedSecondObject extends NestedObjects, which extends Codable - NestedObject can be extended by NesteThirtObject and so on...
Contact extends Object1, which extends Codable;
Contact contains a NestedObject type (which can be any subclass of NestedObject at runtime)
Because JSONEncoder and JSONDecoder do not support inheritance by default, i override the methods "encode" and init(from: decoder) as described here: Using Decodable in Swift 4 with Inheritance
My code is:
class NestedSecondObject: NestedObject {
var subfield2: Int?
private enum CodingKeys : String, CodingKey {
case subfield2
}
override init() { super.init() }
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(subfield2, forKey: .subfield2)
}
required init(from decoder: Decoder) throws
{
try super.init(from: decoder)
let values = try decoder.container(keyedBy: CodingKeys.self)
self.subfield2 = try values.decode(Int.self, forKey: .subfield2)
}
}
class Contact:Object1 {
var name: String = ""
var age: Int = 0
var address: String = ""
// var className = "biz.ebas.platform.generic.shared.EContactModel"
var nestedObject:NestedObject?
private enum CodingKeys : String, CodingKey {
case name,age,address,nestedObject
}
override init() { super.init() }
override func encode(to encoder: Encoder) throws {
try super.encode(to: encoder)
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(age, forKey: .age)
try container.encode(address, forKey: .address)
try container.encode(nestedObject, forKey: .nestedObject)
}
required init(from decoder: Decoder) throws
{
try super.init(from: decoder)
print(type(of: self))
print(type(of: decoder))
let values = try decoder.container(keyedBy: CodingKeys.self)
print(type(of: values))
self.name = try values.decode(String.self, forKey: .name)
self.age = try values.decode(Int.self, forKey: .age)
self.address = try values.decode(String.self, forKey: .address)
self.nestedObject = try values.decodeIfPresent(???.self, forKey: .nestedObject) // HERE i need to know what ???.self is
}
}
Decoding is:
let jsonDec = JSONDecoder()
let jsonData = json?.data(using: .utf8)
let decodedContact: Contact = try jsonDec.decode(Contact.self, from: jsonData!)
So, basically, when I make a request to the server, I know what types I receive (let's say I request NestedSecondObject), but how do I pass it to the method "init(from decoder:Decoder)" ?
I tried to extend class JSONDecoder and add a simple property like this:
class EJSONDecoder: JSONDecoder {
var classType:AnyObject.Type?
}
But inside the required init(from decoder:Decoder) method, the type of decoder is not EJSONDecoder, is _JSONDecoder, so I cannot access the decoder.classType property.
Can anyone help with a solution or some sort of workaround?
Thanks!
New answer
You can give the decoder a userInfo array where you can store things you want to use during decoding.
let decoder = JSONDecoder()
decoder.userInfo = [.type: type(of: NestedSecondObject.self)]
let decodedContact: Contact = try! decoder.decode(Contact.self, from: json)
extension CodingUserInfoKey {
static let type = CodingUserInfoKey(rawValue: "Type")!
}
Then use it during decoding:
switch decoder.userInfo[.type]! {
case let second as NestedSecondObject.Type:
self.nestedObject = try values.decode(second, forKey: .nestedObject)
case let first as NestedObject.Type:
self.nestedObject = try values.decode(first, forKey: .nestedObject)
default:
fatalError("didnt work")
}
I have sadly not found a way to skip the switch.
Old answer:
Decode as
NestedSecondObject.self
and if that fails decode inside the catch with
NestedObject.self
. Do not catch the NestedObject-decoding cause you want to fail if its not even decodable to the basic type.

How to exclude properties from Swift Codable?

Swift's Encodable/Decodable protocols, released with Swift 4, make JSON (de)serialization quite pleasant. However, I have not yet found a way to have fine-grained control over which properties should be encoded and which should get decoded.
I have noticed that excluding the property from the accompanying CodingKeys enum excludes the property from the process altogether, but is there a way to have more fine-grained control?
The list of keys to encode/decode is controlled by a type called CodingKeys (note the s at the end). The compiler can synthesize this for you but can always override that.
Let's say you want to exclude the property nickname from both encoding and decoding:
struct Person: Codable {
var firstName: String
var lastName: String
var nickname: String?
private enum CodingKeys: String, CodingKey {
case firstName, lastName
}
}
If you want it to be asymmetric (i.e. encode but not decode or vice versa), you have to provide your own implementations of encode(with encoder: ) and init(from decoder: ):
struct Person: Codable {
var firstName: String
var lastName: String
// Since fullName is a computed property, it's excluded by default
var fullName: String {
return firstName + " " + lastName
}
private enum CodingKeys: String, CodingKey {
case firstName, lastName, fullName
}
// We don't want to decode `fullName` from the JSON
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
firstName = try container.decode(String.self, forKey: .firstName)
lastName = try container.decode(String.self, forKey: .lastName)
}
// But we want to store `fullName` in the JSON anyhow
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(firstName, forKey: .firstName)
try container.encode(lastName, forKey: .lastName)
try container.encode(fullName, forKey: .fullName)
}
}
Solution with custom property wrapper
struct Person: Codable {
var firstName: String
var lastName: String
#CodableIgnored
var nickname: String?
}
Where CodableIgnored is
#propertyWrapper
public struct CodableIgnored<T>: Codable {
public var wrappedValue: T?
public init(wrappedValue: T?) {
self.wrappedValue = wrappedValue
}
public init(from decoder: Decoder) throws {
self.wrappedValue = nil
}
public func encode(to encoder: Encoder) throws {
// Do nothing
}
}
extension KeyedDecodingContainer {
public func decode<T>(
_ type: CodableIgnored<T>.Type,
forKey key: Self.Key) throws -> CodableIgnored<T>
{
return CodableIgnored(wrappedValue: nil)
}
}
extension KeyedEncodingContainer {
public mutating func encode<T>(
_ value: CodableIgnored<T>,
forKey key: KeyedEncodingContainer<K>.Key) throws
{
// Do nothing
}
}
Another way to exclude some properties from encoder, separate coding container can be used
struct Person: Codable {
let firstName: String
let lastName: String
let excludedFromEncoder: String
private enum CodingKeys: String, CodingKey {
case firstName
case lastName
}
private enum AdditionalCodingKeys: String, CodingKey {
case excludedFromEncoder
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let anotherContainer = try decoder.container(keyedBy: AdditionalCodingKeys.self)
firstName = try container.decode(String.self, forKey: .firstName)
lastName = try container.decode(String.self, forKey: .lastName)
excludedFromEncoder = try anotherContainer(String.self, forKey: . excludedFromEncoder)
}
// it is not necessary to implement custom encoding
// func encode(to encoder: Encoder) throws
// let person = Person(firstName: "fname", lastName: "lname", excludedFromEncoder: "only for decoding")
// let jsonData = try JSONEncoder().encode(person)
// let jsonString = String(data: jsonData, encoding: .utf8)
// jsonString --> {"firstName": "fname", "lastName": "lname"}
}
same approach can be used for decoder
If we need to exclude decoding of a couple of properties from a large set of properties in the structure, declare them as optional properties. Code to unwrapping optionals is less than writing a lot of keys under CodingKey enum.
I would recommend using extensions to add computed instance properties and computed type properties. It separates codable comforming properties from other logic hence provides better readability.
You can use computed properties:
struct Person: Codable {
var firstName: String
var lastName: String
var nickname: String?
var nick: String {
get {
nickname ?? ""
}
}
private enum CodingKeys: String, CodingKey {
case firstName, lastName
}
}
While this can be done it ultimately ends up being very unSwifty and even unJSONy. I think I see where you are coming from, the concept of #ids is prevalent in HTML, but it is rarely transported over to the world of JSON which I consider a good thing (TM).
Some Codable structs will be able to parse your JSON file just fine if you restructure it using recursive hashes, i.e. if your recipe just contains an array of ingredients which in turn contains (one or several) ingredient_info. That way the parser will help you to stitch your network together in the first place and you only have to provide some backlinks through a simple traversal the structure if you really need them. Since this requires a thorough rework of your JSONand your data structure I only sketch out the idea for you to think about it. If you deem it acceptable please tell me in the comments then I could elaborate it further, but depending on the circumstances you may not be at the liberty to change either one of them.
I have used protocol and its extension along with AssociatedObject to set and get image (or any property which needs to be excluded from Codable) property.
With this we dont have to implement our own Encoder and Decoder
Here is the code, keeping relevant code for simplicity:
protocol SCAttachmentModelProtocol{
var image:UIImage? {get set}
var anotherProperty:Int {get set}
}
extension SCAttachmentModelProtocol where Self: SCAttachmentUploadRequestModel{
var image:UIImage? {
set{
//Use associated object property to set it
}
get{
//Use associated object property to get it
}
}
}
class SCAttachmentUploadRequestModel : SCAttachmentModelProtocol, Codable{
var anotherProperty:Int
}
Now, whenever we want to access the Image property we can use on the object confirming to protocol (SCAttachmentModelProtocol)