Skip to content

Commit eba1cde

Browse files
committed
adds default Encodable protocol implementation for JSONPrimative types
1 parent 34ba194 commit eba1cde

File tree

2 files changed

+21
-66
lines changed

2 files changed

+21
-66
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ carthage update --platform iOS
8080

8181
Elevate aims to make JSON parsing and validation simple, yet robust. This is achieved through a set of protocols and classes that can be utilized to create `Decodable` and `Decoder` classes. By using Elevate's parsing infrastructure, you'll be able to easily parse JSON data into strongly typed model objects or simple dictionaries by specifying each property key path and its associated type. Elevate will validate that the keys exist (if they're not optional) and that they are of the correct type. Validation errors will be aggregated as the JSON data is parsed. If an error is encountered, a `ParserError` will be thrown.
8282

83-
Elevate also supports encoding model objects back into JSON objects through the light-weight `Encodable` protocol. It also supports convenience extensions on collection types to make it easy to encode nested objects in a single pass.
83+
Elevate also supports encoding model objects back into JSON objects through the light-weight `Encodable` protocol. Convenience extensions have been added to collection types to make it easy to encode nested objects in a single pass.
8484

8585
### Parsing JSON with Elevate
8686

Source/Encodable.swift

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -24,94 +24,49 @@
2424

2525
import Foundation
2626

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.
2828
public protocol Encodable {
2929
/// Returns the JSON form of the type to be encoded using `JSONSerialization`.
3030
var json: Any { get }
3131
}
3232

33-
// MARK: - String
33+
protocol JSONPrimative: Encodable {}
3434

35-
extension String: Encodable {
35+
extension JSONPrimative {
3636
/// Returns `self` as type `Any`.
3737
public var json: Any { return self }
3838
}
3939

40+
// MARK: - String
41+
42+
extension String: JSONPrimative { }
43+
4044
extension URL: Encodable {
4145
/// Returns the `absoluteString` of `self` as type `Any`.
4246
public var json: Any { return absoluteString }
4347
}
4448

4549
// MARK: - Int
4650

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 {}
7156

7257
// MARK: - UInt
7358

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 {}
9864

9965
// MARK: - Number
10066

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 {}
11570

11671
// MARK: - Collection
11772

0 commit comments

Comments
 (0)