@@ -17,7 +17,7 @@ class UserService: RPCService {
1717 return try await invoke (" vote" , params : [" rating" : rating])
1818 }
1919
20- func create (name : String ) async throws -> UserModel {
20+ func create (name : String ) async throws -> User {
2121 return try await invoke (" create" , params : [" name" : name])
2222 }
2323
@@ -41,58 +41,55 @@ let service = MyService(client: client)
4141try await service.vote (rating : 5 )
4242```
4343
44- #### Result Serialization
44+ #### Result Decoding
4545
46- SwiftJSONRPC provides built-in result serialization for ` Int ` , ` String ` , ` Bool ` types.
47-
48- ##### ` Parcelable ` Protocol
49-
50- To serialize your custom type result from JSON you can implement ` Parcelable ` protocol.
46+ SwiftJSONRPC uses Swift's ` Decodable ` protocol to decode response objects.
5147
5248``` swift
53- protocol Parcelable {
54- init (params : [String : Any ]) throws
49+ struct User : Decodable {
50+ let id: String
51+ let name: String
5552}
53+
54+ class UserService : RPCService {
55+ func getCurrentUser () async throws -> User {
56+ return try await invoke (" getCurrentUser" )
57+ }
58+ }
59+
60+ let user = try await userService.getCurrentUser ()
61+ print (" Current user ID = \( user.id ) , name = \( user.name ) " )
5662```
5763
58- For example:
64+ If you need to modify ` JSONDecoder ` 's behaviour, use ` RPCClient.coder.resultDecoder ` for that.
5965
6066``` swift
61- struct UserModel : Parcelable {
62- let id: String
63- let name: String
64-
65- required init (params : [String : Any ]) throws {
66- // Parse params to struct
67- // ...
68- }
69- }
67+ client.coder .resultDecoder .dateDecodingStrategy = .iso8601
7068```
7169
72- > You can use libraries like [ ObjectMapper ] ( https://github.com/Hearst-DD/ObjectMapper ) , [ MAPPER ] ( https://github.com/LYFT/MAPPER ) or other to adapt ` Parcelable ` protocol. Or you can adapt Swift 4 ` Decodable ` .
70+ #### Params Encoding
7371
74- After that use this struct as ` RPCService.Result ` generic parameter:
72+ SwiftJSONRPC uses Swift's ` Encodable ` protocol to encode request params.
7573
7674``` swift
77- class UserService : RPCService {
78- func create (name : String ) async throws -> UserModel {
79- return try await invoke (" create" , params : [" name" : name])
75+ struct Message : Encodable {
76+ let text: String
77+ }
78+
79+ class MessageService : RPCService {
80+ func send (message : Message) async throws {
81+ return try await invoke (" sendMessage" , params : message)
8082 }
8183}
82- ```
83- ``` swift
84- let user = try await service.create (name : " testuser" )
85- print (" User created with ID = \( user.id ) " )
84+
85+ let message = Message (text : " Hello World" )
86+ try await messageService.send (message : message)
8687```
8788
88- Using array of ` Parcelable ` objects is also supported:
89+ If you need to modify ` JSONEncoder ` 's behaviour, use ` RPCClient.coder.paramsEncoder ` for that.
8990
9091``` swift
91- extension UserService {
92- func allUsers () async throws -> [UserModel] {
93- return try await invoke (" all_users" )
94- }
95- }
92+ client.coder .paramsEncoder .dateDecodingStrategy = .iso8601
9693```
9794
9895## Advanced Usage
@@ -129,7 +126,6 @@ dependencies: [
129126## ToDo
130127
131128- [ ] Add support for notification request object without an "id" member.
132- - [ ] Remove ` Parcelable ` protocol and use ` Decodable ` .
133129
134130## Author
135131
0 commit comments