Swift encode class without nesting super class - json

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)
}

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)
}
}

How to load stored properties from json string?

I wish refresh the all class properties using json data obtained from an API when called. The code I wrote here works fine but imagine if you have a large number of stored properties, say 50,
then in loadJsonData(), you will have to define the property manually line by line, which isn't great. Is there a better way to do this?
struct loadUserData: View {
#ObservedObject var someUser = SomeUser()
var body: some View {
VStack(alignment: .leading){
TextField("Enter current user name", text: self.$someUser.name)
Text("Name: \(someUser.name)")
Button(action: {self.someUser.loadJsonData()}){
Text("Load Json Data")
}
}
}
}
class SomeUser: ObservableObject, Codable {
#Published var name: String
init(){
self.name = "Emma"
}
func loadJsonData(){
let newJson = "{\"name\":\"Jason\"}" // imagine this is what you get from API query
let data = Data(newJson.utf8)
guard let decoded = try? JSONDecoder().decode(SomeUser.self, from: data) else {
print("something went wrong. newJson is \(newJson)")
return
}
self.name = decoded.name
}
enum CodingKeys: String, CodingKey {
case name
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
}
}

Make class conforming to Codable by default in Swift

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!

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.

Core Data - NSManagedObject (with relationships) to JSON

I'm new to Core Data and I want to convert NSManagedObject (fetched from local database) to a JSON string. It's a simple object with relationships, as you can see above:
That's the code that I use for fetching:
func loadData() {
//1
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let managedContext = appDelegate.persistentContainer.viewContext
//2
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Bill")
//3
do {
let fetchedData = try managedContext.fetch(fetchRequest) as! [Bill]
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
How can I convert fetchedData into a JSON string? I'd like to use Codable but i don't know if it's supported.
Create an extension for each class and let it implement Encodable. Something like this for Lot
extension Lot: Encodable {
enum CodingKeys: String, CodingKey {
case id
case quantity
case expiration
case quantity_packages
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(quantity, forKey: . quantity)
try container.encode(expiration, forKey: . expiration)
try container.encode(quantity_packages, forKey: . quantity_packages)
}
and for Bill, notice that I convert lots from NSSet to Array
extension Bill: Encodable {
enum CodingKeys: String, CodingKey {
case id
case name
case lots
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(id, forKey: .id)
try container.encode(name, forKey: .name)
if let array = lots?.allObjects as? [Lot] {
try container.encode(array, forKey: .lots)
} // else Not sure what to do here, maybe use an empty array?
}
}
I haven't been able to test this properly but I hope this helps.