You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+125-2Lines changed: 125 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,7 +35,8 @@ Supercharge `Swift`'s `Codable` implementations with macros.
35
35
36
36
## Installation
37
37
38
-
### Swift Package Manager
38
+
<details>
39
+
<summary><h3>Swift Package Manager</h3></summary>
39
40
40
41
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
41
42
@@ -50,10 +51,132 @@ Then you can add the `MetaCodable` module product as dependency to the `target`s
See the full [documentation](https://swiftylab.github.io/MetaCodable/documentation/metacodable/) for API details and articles on sample scenarios.
58
+
`MetaCodable` allows to get rid of boiler plate that was often needed in some typical `Codable` implementations with features like:
59
+
60
+
<details>
61
+
<summary>Custom `CodingKey` value declaration per variable, instead of requiring you to write for all fields.</summary>
62
+
63
+
i.e. in the official [docs](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types#2904057), to define custom `CodingKey` for 2 fields of `Landmark` type you had to write:
64
+
```swift
65
+
structLandmark: Codable {
66
+
var name: String
67
+
var foundingYear: Int
68
+
var location: Coordinate
69
+
var vantagePoints: [Coordinate]
70
+
71
+
enumCodingKeys: String, CodingKey {
72
+
casename="title"
73
+
casefoundingYear="founding_date"
74
+
caselocation
75
+
casevantagePoints
76
+
}
77
+
}
78
+
```
79
+
But with `MetaCodable` all you have to write is this:
80
+
```swift
81
+
@Codable
82
+
structLandmark {
83
+
@CodablePath("title")
84
+
var name: String
85
+
@CodablePath("founding_date")
86
+
var foundingYear: Int
87
+
88
+
var location: Coordinate
89
+
var vantagePoints: [Coordinate]
90
+
}
91
+
```
92
+
</details>
93
+
94
+
<details>
95
+
<summary>Create flattened model for nested `CodingKey` values.</summary>
96
+
97
+
i.e. in official [docs](https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types#2904058) to decode a JSON like this:
98
+
```json
99
+
{
100
+
"latitude": 0,
101
+
"longitude": 0,
102
+
"additionalInfo": {
103
+
"elevation": 0
104
+
}
105
+
}
106
+
```
107
+
You had to write all these boilerplate:
108
+
```swift
109
+
structCoordinate {
110
+
var latitude: Double
111
+
var longitude: Double
112
+
var elevation: Double
113
+
114
+
enumCodingKeys: String, CodingKey {
115
+
caselatitude
116
+
caselongitude
117
+
caseadditionalInfo
118
+
}
119
+
120
+
enumAdditionalInfoKeys: String, CodingKey {
121
+
caseelevation
122
+
}
123
+
}
124
+
125
+
extensionCoordinate: Decodable {
126
+
init(fromdecoder: Decoder) throws {
127
+
let values =try decoder.container(keyedBy: CodingKeys.self)
128
+
latitude =try values.decode(Double.self, forKey: .latitude)
But with `MetaCodable` all you have to write is this:
148
+
```swift
149
+
@Codable
150
+
structCoordinate {
151
+
var latitude: Double
152
+
var longitude: Double
153
+
154
+
@CodablePath("additionalInfo", "elevation")
155
+
var elevation: Double
156
+
}
157
+
```
158
+
</details>
159
+
160
+
<details>
161
+
<summary>Provide default value in case of decoding failures and member-wise initializer generated considers these default values.</summary>
162
+
163
+
Instead of throwing error in case of missing data or type mismatch, you can provide a default value that will be assigned in this case. The memberwise initializer generated also uses this default value for the field. The following definition with `MetaCodable`:
164
+
```swift
165
+
@Codable
166
+
structCodableData {
167
+
@CodablePath(default:"some")
168
+
let field: String
169
+
}
170
+
```
171
+
will not throw any error when empty JSON(`{}`) or JSON with type mismatch(`{ "field": 5 }`) is provided. The default value will be assigned in such case. Also, the memberwise initializer generated will look like this:
172
+
```swift
173
+
init(field: String="some") {
174
+
self.field= field
175
+
}
176
+
```
177
+
</details>
178
+
179
+
See the full [documentation](https://swiftylab.github.io/MetaCodable/documentation/metacodable/) for API details and advanced use cases.
| Linux | 5.9 | Swift Package Manager | Fully Tested |
25
-
| Windows | 5.9 | Swift Package Manager | Fully Tested |
26
-
27
23
## Installation
28
24
29
-
### Swift Package Manager
30
-
31
-
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
32
-
33
-
Once you have your Swift package set up, adding `MetaCodable` as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.
Then you can add the `MetaCodable` module product as dependency to the `target`s of your choosing, by adding it to the `dependencies` value of your `target`s.
The [Swift Package Manager](https://swift.org/package-manager/) is a tool for automating the distribution of Swift code and is integrated into the `swift` compiler.
29
+
30
+
Once you have your Swift package set up, adding `MetaCodable` as a dependency is as easy as adding it to the `dependencies` value of your `Package.swift`.
Then you can add the `MetaCodable` module product as dependency to the `target`s of your choosing, by adding it to the `dependencies` value of your `target`s.
0 commit comments