Skip to content

Commit 2b594f9

Browse files
committed
update example
1 parent 07ba626 commit 2b594f9

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,42 @@ enum PaymentMethod {
229229
// encodes as: {"payment_type": "apple_pay", "payment_data": {"token": "..."}}
230230
```
231231

232+
#### Combining with @AllOfCodable
233+
234+
Use `@AllOfCodable` to compose a `@TaggedCodable` enum together with other fields into one flat JSON object:
235+
236+
```swift
237+
struct UserInfo: Codable {
238+
let userId: String
239+
let locale: String
240+
}
241+
242+
@TaggedCodable
243+
@CodedAt("type", caseStyle: .screamingSnakeCase)
244+
@ContentAt("data")
245+
enum PaymentMethod {
246+
case card(number: String, expiry: String)
247+
case applePay(token: String)
248+
}
249+
250+
@AllOfCodable
251+
struct PaymentRequest {
252+
let userInfo: UserInfo
253+
let payment: PaymentMethod
254+
}
255+
```
256+
257+
`PaymentRequest` encodes as a single flat object — `userInfo`'s fields and `payment`'s tag/params all at the top level:
258+
259+
```json
260+
{
261+
"userId": "u_123",
262+
"locale": "en-US",
263+
"type": "CARD",
264+
"data": {"number": "4242424242424242", "expiry": "12/26"}
265+
}
266+
```
267+
232268
### Annotations
233269

234270
#### @CodingKey

0 commit comments

Comments
 (0)