-
Notifications
You must be signed in to change notification settings - Fork 2
/
Contents.swift
61 lines (49 loc) · 919 Bytes
/
Contents.swift
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
//: Playground - noun: a place where people can play
import MsgPack
let encoder = Encoder()
struct Graph: Codable {
let title: String
let circles: Circle
}
struct Circle: Codable {
let radius: UInt64
let center: Position
}
struct Position: Codable {
let x: Int8
let y: Int8
}
let graph = Graph(
title: "My graph",
circles:
Circle(
radius: 0x0102030405060708,
center: Position(x: -123, y: 2)
)
// ,
// Circle(
// radius: 1000,
// center: Position(x: 116, y: 81)
// )
)
try encoder.encode(graph)
let decoder = Decoder()
func roundtrip<T: Codable>(value: T) throws -> T {
return try decoder.decode(T.self, from: encoder.encode(value))
}
struct Simple: Codable {
let a: Bool
let b: Bool
let c: Bool?
let d: Bool?
let e: Bool?
let f: String
}
try roundtrip(value: -56.4)
try roundtrip(value: "Hello world 😎")
do {
try roundtrip(value: graph)
} catch {
error
print(error)
}