laurent espinosa
laurent espinosa

Reputation: 57

How to Add or Remove an object in result of JSON Codable Swift 4 (with dynamic Keys)

i want import JSON in swift with Codable, modify the object by adding or removing object, and export it in JSON.

Here, my structure

class GenericCodingKeys: CodingKey {
var stringValue: String
var intValue: Int?

required init?(stringValue: String) { self.stringValue = stringValue }
required init?(intValue: Int) { self.intValue = intValue; self.stringValue = "\(intValue)" }
}

class ListSpecie : Codable {
var species: [String : Specie]

required init(from decoder: Decoder) throws
{
    let container = try decoder.container(keyedBy: GenericCodingKeys.self)
    self.species = [String: Specie]()
    for key in container.allKeys{
        let value = try container.decodeIfPresent(Specie.self, forKey: GenericCodingKeys(stringValue: key.stringValue)!)
        self.species[key.stringValue] = value
    }
}
}

class Specie : Codable {
var name : String?
var latinName : [String]?

enum CodingKeys: String, CodingKey {
    case name = "l"
    case latinName = "ll"
}
required init(from decoder: Decoder) throws
{
    let sValues = try decoder.container(keyedBy: CodingKeys.self)
    name = try sValues.decodeIfPresent(String.self, forKey: .name)
    latinName = try sValues.decodeIfPresent(Array<String>.self, forKey: .latinName)
}
func encode(to encoder: Encoder) throws {
    var container = encoder.container(keyedBy: CodingKeys.self)
    try container.encodeIfPresent(name, forKey: .name)
    try container.encodeIfPresent(latinName, forKey: .latinName)
}
}

Here, the is a code with sample JSON

let myJson = """
       {
            "especeID1": {
            "l": "Ail",
            "ll": ["Allium sativum L.","Allium"]
            },
            "especeID2": {
            "l": "Artichaut",
            "ll": ["Cynara cardunculus"]
            }
        }
    """

    let jsonDATA = myJson.data(using: .utf8)!

    do{
        self.jsonResult = try JSONDecoder().decode(ListSpecie.self, from: jsonDATA)
    }catch{
        print(error.localizedDescription)
    }

Here, I want append or remove specie Object on jsonResult

    for myspecie in (self.jsonResult?.species)! {
        print(myspecie.key + " " +  myspecie.value.name!)
    }

    // Encodage
    let encoder = JSONEncoder()
    let productJSON = try! encoder.encode(self.jsonResult?.species) 

    let jsonString = String(data: productJSON, encoding: .utf8)!

Someone could tell me how i can append or remove a specie object in my jsonResult variable .

Thanks a lot for the help you can bring me.

Upvotes: 1

Views: 1886

Answers (1)

vadian
vadian

Reputation: 285240

First of all your code is too complicated, most of the code is redundant.

One class (consider a struct) is sufficient

class Specie : Codable {
    var name : String?
    var latinName : [String]?

    enum CodingKeys: String, CodingKey {
        case name = "l"
        case latinName = "ll"
    }
}

If name and latin name is supposed to appear everywhere declare the properties non-optional (remove the question marks).

And decode the JSON

self.jsonResult = try JSONDecoder().decode([String:Specie].self, from: jsonDATA)

jsonResult is now a dictionary ([String:Specie]), you can remove items

self.jsonResult.removeValue(forKey: "especeID2")

or add an item

let newSpecies = Specie()
newSpecies.name = "Potato"
newSpecies.latinName = ["Solanum tuberosum"]
self.jsonResult["especeID3"] = newSpecies

and encode the object

let encoder = JSONEncoder()
let productJSON = try! encoder.encode(self.jsonResult)
let jsonString = String(data: productJSON, encoding: .utf8)!

Upvotes: 2

Related Questions