|
24 | 24 |
|
25 | 25 | import Foundation |
26 | 26 |
|
27 | | -/// The `Decodable` protocol type declares an interface used to create a JSON `Any` object from the instance. |
| 27 | +/// The `Encodable` protocol type declares an interface used to create a JSON `Any` object from the instance. |
28 | 28 | public protocol Encodable { |
29 | 29 | /// Returns the JSON form of the type to be encoded using `JSONSerialization`. |
30 | 30 | var json: Any { get } |
31 | 31 | } |
32 | 32 |
|
33 | | -// MARK: - String |
| 33 | +protocol JSONPrimative: Encodable {} |
34 | 34 |
|
35 | | -extension String: Encodable { |
| 35 | +extension JSONPrimative { |
36 | 36 | /// Returns `self` as type `Any`. |
37 | 37 | public var json: Any { return self } |
38 | 38 | } |
39 | 39 |
|
| 40 | +// MARK: - String |
| 41 | + |
| 42 | +extension String: JSONPrimative { } |
| 43 | + |
40 | 44 | extension URL: Encodable { |
41 | 45 | /// Returns the `absoluteString` of `self` as type `Any`. |
42 | 46 | public var json: Any { return absoluteString } |
43 | 47 | } |
44 | 48 |
|
45 | 49 | // MARK: - Int |
46 | 50 |
|
47 | | -extension Int: Encodable { |
48 | | - /// Returns `self` as type `Any`. |
49 | | - public var json: Any { return self } |
50 | | -} |
51 | | - |
52 | | -extension Int8: Encodable { |
53 | | - /// Returns `self` as type `Any`. |
54 | | - public var json: Any { return self } |
55 | | -} |
56 | | - |
57 | | -extension Int16: Encodable { |
58 | | - /// Returns `self` as type `Any`. |
59 | | - public var json: Any { return self } |
60 | | -} |
61 | | - |
62 | | -extension Int32: Encodable { |
63 | | - /// Returns `self` as type `Any`. |
64 | | - public var json: Any { return self } |
65 | | -} |
66 | | - |
67 | | -extension Int64: Encodable { |
68 | | - /// Returns `self` as type `Any`. |
69 | | - public var json: Any { return self } |
70 | | -} |
| 51 | +extension Int: JSONPrimative {} |
| 52 | +extension Int8: JSONPrimative {} |
| 53 | +extension Int16: JSONPrimative {} |
| 54 | +extension Int32: JSONPrimative {} |
| 55 | +extension Int64: JSONPrimative {} |
71 | 56 |
|
72 | 57 | // MARK: - UInt |
73 | 58 |
|
74 | | -extension UInt: Encodable { |
75 | | - /// Returns `self` as type `Any`. |
76 | | - public var json: Any { return self } |
77 | | -} |
78 | | - |
79 | | -extension UInt8: Encodable { |
80 | | - /// Returns `self` as type `Any`. |
81 | | - public var json: Any { return self } |
82 | | -} |
83 | | - |
84 | | -extension UInt16: Encodable { |
85 | | - /// Returns `self` as type `Any`. |
86 | | - public var json: Any { return self } |
87 | | -} |
88 | | - |
89 | | -extension UInt32: Encodable { |
90 | | - /// Returns `self` as type `Any`. |
91 | | - public var json: Any { return self } |
92 | | -} |
93 | | - |
94 | | -extension UInt64: Encodable { |
95 | | - /// Returns `self` as type `Any`. |
96 | | - public var json: Any { return self } |
97 | | -} |
| 59 | +extension UInt: JSONPrimative {} |
| 60 | +extension UInt8: JSONPrimative {} |
| 61 | +extension UInt16: JSONPrimative {} |
| 62 | +extension UInt32: JSONPrimative {} |
| 63 | +extension UInt64: JSONPrimative {} |
98 | 64 |
|
99 | 65 | // MARK: - Number |
100 | 66 |
|
101 | | -extension Float: Encodable { |
102 | | - /// Returns `self` as type `Any`. |
103 | | - public var json: Any { return self } |
104 | | -} |
105 | | - |
106 | | -extension Double: Encodable { |
107 | | - /// Returns `self` as type `Any`. |
108 | | - public var json: Any { return self } |
109 | | -} |
110 | | - |
111 | | -extension Bool: Encodable { |
112 | | - /// Returns `self` as type `Any`. |
113 | | - public var json: Any { return self } |
114 | | -} |
| 67 | +extension Float: JSONPrimative {} |
| 68 | +extension Double: JSONPrimative {} |
| 69 | +extension Bool: JSONPrimative {} |
115 | 70 |
|
116 | 71 | // MARK: - Collection |
117 | 72 |
|
|
0 commit comments