Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final class Post: Model, Codable {
- // other stuff...
- // MARK: Codable compliance
- private enum CodingKeys: String, CodingKey {
- case id
- case content
- }
- func encode(to encoder: Encoder) throws {
- var container = encoder.container(keyedBy: CodingKeys.self)
- // Note that Identifier is not Codable, so as a workaround, I'm always representing it as a String.
- try container.encode(id?.string, forKey: .id)
- try container.encode(content, forKey: .content)
- }
- init(from decoder: Decoder) throws {
- let container = try decoder.container(keyedBy: CodingKeys.self)
- content = try container.decode(String.self, forKey: .content)
- }
- }
- // You can now use request.decodeJSONBody(Post.self), post.makeResponse(), etc.
Add Comment
Please, Sign In to add comment