Skip to content

Commit 3447d45

Browse files
authored
Update README.md
1 parent 15ba51b commit 3447d45

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,38 @@ testServerApi.post("/posts", data: ["body": "something","id": 1, "title": "Some
9191
}
9292
```
9393
Body parameters should comform to `ValidJSONObject` protocol. `Array` and `Dictionary` types implement this by default.
94+
Any custom type can implement `ValidJSONObject`, which requres a function that converts your type to `Data`.
95+
96+
```swift
97+
func JSONFormat() throws -> Data
98+
```
99+
100+
If you don't want that much controll and resposibility, you can implement `JSONConvertible`, which has a property named `parameterValue` which can be any `ValidJSONObject`. Practically you just have to convert your type to a `Dictionary` or an other `ValidJSONObject` just it like this:
101+
102+
```swift
103+
struct ExampleData {
104+
var body: String
105+
var id: Int
106+
var title: String
107+
var userId: Int
108+
}
109+
110+
extension ExampleData: JSONConvertible {
111+
// this is valid json
112+
var parameterValue: [String: Any] {
113+
return ["body": body, "id": id, "title": title, "userId": userId]
114+
}
115+
}
116+
```
117+
Additional fields can be added anytime to this property, also, you can exclude any properties. After implementing it, uploading is simple:
118+
119+
```swift
120+
var uploadData = ExampleData(body: "body", id: 1, title: "title", userId: 2)
121+
122+
testServerApi.post("/posts", data: uploadData) { (error, object) in
123+
//...
124+
}
125+
```
94126

95127
### Authenticating requests
96128

0 commit comments

Comments
 (0)