Mr Some Dev.
Mr Some Dev.

Reputation: 315

Decrypt String when decoding JSON on Swift

I have a JSON file and a related class. I decode it by calling init?(data: Data) initializer. I'm fetching this JSON from an endpoint. I want to encrypt URL text with SHA-256. My problem is that I want to decrypt this URL string when decoding data. So when decoding URL, It needs to call a function. Any possible way to do this?

PS: I know I can write encrypted text and decrypt it where I'll use but I want to do this as last option.

struct TableCellData: Codable {
    let type: Int
    let cellText: String
    let cellImage: String?
    let url: URL?
    let detailText: String?
    let tableID: Int?

    enum CodingKeys: String, CodingKey {
        case type = "type"
        case cellText = "cell_text"
        case cellImage = "cell_image"
        case url = "url"
        case detailText = "detail_text"
        case tableID = "table_id"
    }
}

extension TableCellData {
    init?(data: Data) {
        guard let me = try? JSONDecoder().decode(TableCellData.self, from: data) else { return nil }
        self = me
    }

    init?(_ json: String, using encoding: String.Encoding = .utf8) {
        guard let data = json.data(using: encoding) else { return nil }
        self.init(data: data)
    }

    init?(fromURL url: String) {
        guard let url = URL(string: url) else { return nil }
        guard let data = try? Data(contentsOf: url) else { return nil }
        self.init(data: data)
    }

    var jsonData: Data? {
        return try? JSONEncoder().encode(self)
    }

    var json: String? {
        guard let data = self.jsonData else { return nil }
        return String(data: data, encoding: .utf8)
    }
}

Upvotes: 0

Views: 1252

Answers (1)

Yahel
Yahel

Reputation: 8550

If I understand correctly, you have a field URL that you would SHA256 encrypt on the server side. You would then receive it encrypted in the json and you would like to get it decrypted in the class instance.

If so, simply look at the documentation :encoding and decoding custom types and search for the title : Encode and Decode Manually

The first block of code is the structure, the second is a custom parser in which you could sha256 encrypt your field.

Edit :

I don't have the time to write code for you unfortunately but maybe this more in depth tutorial on Encodable and coding keys will help you (look for the title "Encode and Decode Manually") : Tutorial Swift 4.0 Encodable

The gist of it is really easy : You provide your own decoding logic in the decodable extension. Here they group width and height in a Size variable :

  struct Photo
{
    var title: String
    var size: Size

    enum CodingKeys: String, CodingKey
    {
        case title = "name"
        case width
        case height
    }
}

extension Photo: Encodable
{
    func encode(to encoder: Encoder) throws
    {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(title, forKey: .title)
        try container.encode(size.width, forKey: .width)
        try container.encode(size.height, forKey: .height)
    }
}

extension Photo: Decodable
{
    init(from decoder: Decoder) throws
    {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        title = try values.decode(String.self, forKey: .title)
        let width = try values.decode(Double.self, forKey: .width)
        let height = try values.decode(Double.self, forKey: .height)
        size = Size(width: width, height: height)
    }
}

Upvotes: 1

Related Questions