Codable, introduced in Swift 4, makes is possible to share Swift classes and structs between client and server, making it easy to share data. It can also be used to add such more type safety to other parts of Fullstack Swift. This presentations shows some of the many ways that Codable is being using in Kitura to enable Fullstack Swift.
3. struct Profile {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
4. struct Profile : Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
5. struct Profile : Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
let encoder = JSONEncoder()
let jsonData = try encoder.encode(profile)
6. struct Profile : Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let profile = Profile(name: “Chris”, photo: image, dateOfBirth: Date(“06-06-1980”))
let encoder = JSONEncoder()
let jsonData = try encoder.encode(profile)
let decoder = JSONDecoder()
let person = try decoder.decode(Profile.self, from: jsonData)
17. struct Profile {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
KITURA
18. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
KITURA
19. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
20. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let encoder = JSONEncoder()
let data = try encoder.encode(profile)
KITURA
21. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
let decoder = JSONDecoder()
let person = try decoder.decode(Person.self, from: jsonData)
KITURA
22. {
"name": "",
"photo": "",
"dateOfBirth": ""
}
{
"name": "",
"photo": "",
"dateOfBirth": ""
}
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
23. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
24. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
25. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
Swift Swift
struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
KITURA
29. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
30. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Decodable {
init(from decoder: Decoder) throws {
}
}
31. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
}
}
32. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
}
}
33. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
photo = try values.decode(Data.self, forKey: .photo)
}
}
34. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Decodable {
init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
name = try values.decode(String.self, forKey: .name)
photo = try values.decode(Data.self, forKey: .photo)
dateOfBirth = try values.decode(Date.self, forKey: .dateOfBirth)
}
}
35. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Encodable {
func encode(to encoder: Encoder) throws {
}
}
36. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Encodable {
func encode(to encoder: Encoder) throws {
var container = try encoder.container(keyedBy: CodingKeys.self)
}
}
37. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
extension Profile {
enum CodingKeys: String, CodingKey {
case name = "name"
case photo = "photo"
case dateOfBirth = "dateOfBirth"
}
}
extension Profile : Encodable {
func encode(to encoder: Encoder) throws {
var container = try encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
try container.encode(photo, forKey: .photo)
try container.encode(dateOfBirth, forKey: .dateOfBirth)
}
}
72. struct Profile: Codable {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
73. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
...
respondWith(profiles, nil)
}
74. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll()
respondWith(profiles, nil)
}
75. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(respondWith)
respondWith(profiles, nil)
}
76. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(respondWith: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(respondWith)
}
77. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
81. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
router.get("/profile", handler: getProfiles)
func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
82. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
83. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(completion)
}
84. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll( completion)
}
85. struct Profile: Model {
var name: String
var photo: Data
var dateOfBirth: Date
}
struct Query: QueryParams {
var name: String
}
router.get("/profile", handler: getProfiles)
func getProfiles(query: Query, completion: @escaping ([Profile]?, Error?) -> Void) -> Void
{
Profile.getAll(matching: query, completion)
}