How to load stored properties from json string? - json

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

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

ARKit 4.0 – Is it possible to convert ARWorldMap data to JSON file?

I'd like to know whether it is possible to convert a worldMap binary data (that stores a space-mapping state and set of ARAnchors) to json or xml file?
func writeWorldMap(_ worldMap: ARWorldMap, to url: URL) throws {
let data = try NSKeyedArchiver.archivedData(withRootObject: worldMap,
requiringSecureCoding: true)
try data.write(to: url)
}
If this possible, what tools can I use for that?
I am afraid that the only way to do this is by wrapping the ARWorldMap object into a Codable object like that:
struct ARData {
var worldMap: ARWorldMap?
}
extension ARData: Codable {
enum CodingKeys: String, CodingKey {
case worldMap
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let worldMapData = try container.decode(Data.self, forKey: .worldMap)
worldMap = try NSKeyedUnarchiver.unarchivedObject(ofClass: ARWorldMap.self, from: worldMapData)
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
if let worldMap = worldMap {
let colorData = try NSKeyedArchiver.archivedData(withRootObject: worldMap, requiringSecureCoding: true)
try container.encode(colorData, forKey: .worldMap)
}
}
}
To encode an instance of that object to JSON use the encode(:) function of the JSONEncoder:
let arData = ARData(worldMap: worldMap)
let encoder = JSONEncoder()
do {
let jsonData = try encoder.encode(arData)
} catch {
print(error)
}
By default, JSONEncoder will convert the ARWorldMap data to a Base64 string that can be read using JSONDecoder:
let decoder = JSONDecoder()
do {
let decoded = try decoder.decode(ARData.self, from: jsonData)
} catch {
print(error)
}

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.

Swift Decoding nested JSON

I have a problem with parsing data from NBP api "http://api.nbp.pl/api/exchangerates/tables/a/?format=json" . I created struct CurrencyDataStore and Currency
struct CurrencyDataStore: Codable {
var table: String
var no : String
var rates: [Currency]
enum CodingKeys: String, CodingKey {
case table
case no
case rates
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
table = ((try values.decodeIfPresent(String.self, forKey: .table)))!
no = (try values.decodeIfPresent(String.self, forKey: .no))!
rates = (try values.decodeIfPresent([Currency].self, forKey: .rates))!
} }
struct Currency: Codable {
var currency: String
var code: String
var mid: Double
enum CodingKeys: String, CodingKey {
case currency
case code
case mid
}
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
currency = try values.decode(String.self, forKey: .currency)
code = try values.decode(String.self, forKey: .code)
mid = try values.decode(Double.self, forKey: .mid)
}
}
In controllerView class I wrote 2 methods to parse data from API
func getLatestRates(){
guard let currencyUrl = URL(string: nbpApiUrl) else {
return
}
let request = URLRequest(url: currencyUrl)
let task = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
if let error = error {
print(error)
return
}
if let data = data {
self.currencies = self.parseJsonData(data: data)
}
})
task.resume()
}
func parseJsonData(data: Data) -> [Currency] {
let decoder = JSONDecoder()
do{
let currencies = try decoder.decode([String:CurrencyDataStore].self, from: data)
}
catch {
print(error)
}
return currencies
}
This code didn't work. I have this error "typeMismatch(Swift.Dictionary, Swift.DecodingError.Context(codingPath: [], debugDescription: "Expected to decode Dictionary but found an array instead.", underlyingError: nil))".
Could you help me?
The JSON being returned by that API gives you an array, not a dictionary, but you're telling the JSONDecoder to expect a dictionary type. Change that line to:
let currencies = try decoder.decode([CurrencyDataStore].self, from: data)

Swift 4 JSON Parsing Int - what am I doing wrong?

Swift 4 JSON Parsing Int issue. All the examples I've seen Int is encoded / decoded out of the box. Anyone see what I am doing wrong?
Thanks!
JSON
let patientBundleEntry = """
{
"resourceType": "Bundle",
"id": "patientListBundle",
"type": "SearchSet",
"total": 123
}
"""
Classes
class BundleFHIR: Resource {
var entry:[BundleEntry]?
var total:Int? // this is printing -> Optional(105553116787496) instead of 123
}
class Resource:Codable {
var resourceType:ResourceType? // this is printing fine
}
Test - my assert for total being 123 fails and the optional is a long number. Any ideas why? Is my encoding wrong using .utf8??
func testModelBundle(){
let jsonDataEncoded:Data? = patientBundleEntry.data(using: .utf8)!
guard let responseData = jsonDataEncoded else {
print("Error: did not receive data")
}
do {
let bundleDecoded = try JSONDecoder().decode(BundleFHIR.self, from: responseData)
print("bundleDecoded.resourceType resource type \(bundleDecoded.resourceType )") //THIS is right
print("bundleDecoded.resourceType total \(bundleDecoded.total )") THIS is wrong
assert(bundleDecoded.total == 123, "something is wrong") // ***** <- this assert fails and it prints Optional(105553116787496)
} catch {
print("error trying to convert data to JSON")
}
}
At first you have to decode and then parse the JSON data.
Follow the below code :
struct Patient: Codable {
var resourceType: String
var id: String
var type: String
var total: Int
}
let json = patientBundleEntry.data(using: .utf8)!
let decoder = JSONDecoder()
let patient = try! decoder.decode(Patient.self, from: json)
print(patient.total) // Prints - 123
Ok there were a lot of issues with my code. Mainly that I didn't have codingKeys as private and therefore tried to rename it because the inheritance tree could not discern between the two. This caused me to not be implementing the true protocol. Not sure why it was half working... but here is my final code and it works great!
class BundleFHIR: Resource {
var entry:[BundleEntry]?
var total:Int?
override init() {
super.init()
}
required init(from decoder: Decoder) throws {
try super.init(from: decoder)
let values = try decoder.container(keyedBy: CodingKeys.self)
total = try values.decodeIfPresent(Int.self, forKey: .total)
entry = try values.decodeIfPresent([BundleEntry].self, forKey: .entry)
}
private enum CodingKeys: String, CodingKey
{
case total
case entry
}
}
class Resource:Codable {
var resourceType:ResourceType?
var id:String?
init(){
}
required init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
resourceType = try values.decode(ResourceType.self, forKey: .resourceType)
id = try values.decode(String.self, forKey: .id)
}
private enum CodingKeys: String, CodingKey
{
case resourceType
case id
}
}