简体   繁体   English

当超类也是可编码的时,如何在子类中使用CodingKeys?

[英]How to use CodingKeys in sub-class when the super class is also Codable?

I'm working on JSON encode and decode, but some problems are very annoying and I don't know how to use CodingKeys in inherit classes. 我正在研究JSON编码和解码,但是有些问题非常烦人,而且我不知道如何在继承类中使用CodingKeys

I have two classes ResponseBean and ResponseWithObjectBean<T> . 我有两个类ResponseBeanResponseWithObjectBean<T>

Here is the response class definition: 这是响应类的定义:

public class ResponseBean: Codable
{
    //This is only sample, I define `CodingKeys` because the property in json is in different name.
    private enum CodingKeys: String, CodingKey
    {
        case intA
        case intB
    }
    public var intA: Int32 = 0
    public var intB: Int32 = 0

}

public class ResponseWithObjectBean<T: Codable> : ResponseBean
{
    /*
    Here I don't know how to define an enum to confirm protocl CondingKey. 
    I defined an enum named CodingKeys or whatever, they just don't work and 
    the testMessage and obj are still nil.
    But if I implement the init(from decoder: Decoder) construction and manually
    pass the coding keys which I defined to the decode function, all works fine.
    */
    public var testMessage: String? = nil
    public var obj: T? = nil
}

and I will get a user from the response: 我将从响应中得到一个用户:

public class User: Codable
{
    private enum CodingKeys: String, CodingKey
    {
        case name
        case age
    }

    public var name: String? = nil
    public var age: Int32? = nil
}

Here is the test json: 这是测试json:

var testJson = """
{
    "intA": 10,
    "intB": 20,
    "testMessage": "This is a test json",
    "obj":{
        "name": "LiHong",
        "age": 11
    }
}
"""

The following is how I run: 以下是我的运行方式:

do{
    var responseData = testJson.data(using: .utf8)
    var decoder = JSONDecoder()
    var response: ResponseWithObjectBean<User> = try decoder.decode(ResponseWithObjectBean<User>.self, from: responseData)
}catch let e{

}

I don't know how to define CodingKeys in ResponseWithObjectBean class, and even I did, it dosen't work at all. 我不知道如何在ResponseWithObjectBean类中定义CodingKeys ,即使我这样做,它也不起作用。 But if I implement init(from decoder: Decoder) throws construction and manully pass coding keys I defined in ResponseWithObjectBean , I can get all properties. 但是,如果我实现init(from decoder: Decoder) throws则会init(from decoder: Decoder) throws结构并手动传递在ResponseWithObjectBean定义的编码键,则我可以获得所有属性。

This is pretty simple, you just have to do the coding and decoding by hand, in the child class: 这非常简单,您只需要在子类中手动进行编码和解码即可:

public class ResponseWithObjectBean<T: Codable> : ResponseBean {
    public var testMessage: String? = nil
    public var obj: T? = nil

    // Create another CodingKey compliant enum with another name for the new keys
    private enum CustomCodingKeys: String, CodingKey {
        case testMessage
        case obj
    }

    // Override the decoder
    required init(from decoder: Decoder) throws {
        try super.init(from: decoder)

        let container = try decoder.container(keyedBy: CustomCodingKeys.self)

        testMessage = try container.decode(String?.self, forKey: .testMessage)
        obj = try container.decode(T?.self, forKey: .obj)
    }

    // And the coder
    public override func encode(to encoder: Encoder) throws {
        try super.encode(to: encoder)

        var container = encoder.container(keyedBy: CustomCodingKeys.self)

        try container.encode(testMessage, forKey: .testMessage)
        try container.encode(obj, forKey: .obj)
    }

}

This way you can decode and encode the way you want: 这样,您可以对所需的方式进行解码和编码:

let decoder = JSONDecoder()
let response = try decoder.decode(ResponseWithObjectBean<User>.self, from: responseData)

let data = try JSONEncoder().encode(response)
print(String(data: data, encoding: .utf8))

EDIT : In order to prevent you from writing manually all this boilerplate, you can use generation tools like Sourcery: https://github.com/krzysztofzablocki/Sourcery 编辑 :为了防止您手动编写所有这些样板,可以使用诸如Sourcery的生成工具: https : //github.com/krzysztofzablocki/Sourcery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM