vapor / http

🚀 Non-blocking, event-driven HTTP built on Swift NIO.
MIT License
240 stars 65 forks source link

Why must the client request parsing use content #379

Closed shenfu1991 closed 3 years ago

shenfu1991 commented 3 years ago

Hello, Why does the client request parsing have to use content? I feel that this is very inefficient and troublesome. I also encountered a problem. How to decode from pure array? There is no way to design a content model.

my server response data is:

[
  [
    1615304580000,
    "54273.30000000",
    "54314.03000000",
    "54273.29000000",
    "54276.01000000",
    "10.94453300",
    1615304639999,
    "594183.63629659",
    327,
    "6.83191000",
    "370931.97423908",
    "0"
  ]
]

i try to write like this:

struct ResponModel: Content{
    let 0: Double?
    let 1: String?
//    ...
}

but the compiler reported an error.

grundoon commented 3 years ago

IF the response array is in a guaranteed format (there are always those 12 fields present, in that order), you can accomplish this by manually decoding from an unkeyedContainer, along the lines of this approach:

struct ResponModel: Decodable {
    var value1: Double?
    var value2: String?
    ...

    required init(from decoder: Decoder) throws {
        var container = try? decoder.unkeyedContainer()
        self.value1 = try container.decodeIfPresent(Double.self)
        self.value2 = try container.decodeIfPresent(String.self)
        ...
    }
}
shenfu1991 commented 3 years ago

IF the response array is in a guaranteed format (there are always those 12 fields present, in that order), you can accomplish this by manually decoding from an unkeyedContainer, along the lines of this approach:

struct ResponModel: Decodable {
    var value1: Double?
    var value2: String?
    ...

    required init(from decoder: Decoder) throws {
        var container = try? decoder.unkeyedContainer()
        self.value1 = try container.decodeIfPresent(Double.self)
        self.value2 = try container.decodeIfPresent(String.self)
        ...
    }
}

Thank you very much for your reply, I have successfully decoded.

struct ResponModel: Content{
    var value1: Int?
    var value2: String?
    var value3: String?
    var value4: String?
    var value5: String?
    var value6: String?
    var value7: Int?
    var value8: String?
    var value9: Int?
    var value10: String?
    var value11: String?
    var value12: String?

     init(from decoder: Decoder) throws {
        var container = try decoder.unkeyedContainer()
        self.value1 = try? container.decodeIfPresent(Int.self)
        self.value2 = try? container.decodeIfPresent(String.self)
        self.value3 = try? container.decodeIfPresent(String.self)
        self.value4 = try? container.decodeIfPresent(String.self)
        self.value5 = try? container.decodeIfPresent(String.self)
        self.value6 = try? container.decodeIfPresent(String.self)
        self.value7 = try? container.decodeIfPresent(Int.self)
        self.value8 = try? container.decodeIfPresent(String.self)
        self.value9 = try? container.decodeIfPresent(Int.self)
        self.value10 = try? container.decodeIfPresent(String.self)
        self.value11 = try? container.decodeIfPresent(String.self)
        self.value12 = try? container.decodeIfPresent(String.self)
    }

}