forked from alexaubry/JavaScriptKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodableModels.swift
More file actions
64 lines (48 loc) · 1.26 KB
/
CodableModels.swift
File metadata and controls
64 lines (48 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// JavaScriptKit
// Copyright (c) 2017 - present Alexis Aubry. Licensed under the MIT license.
//
import Foundation
import JavaScriptKit
// MARK: Empty Object
/// An object that does not encode a value.
class EmptyObject: Codable {
class Void: Encodable {
func encode(to encoder: Encoder) {}
}
let void = Void()
func encode(to encoder: Encoder) throws {
var singleValueContainer = encoder.singleValueContainer()
try singleValueContainer.encode(void)
}
init() {}
required init(from decoder: Decoder) throws {}
}
// MARK: - User
/// A simple structure.
struct User: Codable, Hashable {
let displayName: String
let handle: String
}
// MARK: - Person
/// A structure with nested unkeyed containers containing keyed containers.
struct Company: Codable, Hashable {
let name: String
let address: Address
var childCompanies: [Company]
}
// MARK: - Address
/// A structure with an optional field.
struct Address: Codable, Hashable {
let line1: String
let line2: String?
let zipCode: Int
let city: String
let country: Country
}
// MARK: - Country
/// A Raw Representable and Codable enum.
enum Country: String, Codable {
case unitedStates = "United States"
case france = "France"
}