@@ -104,6 +104,13 @@ const val WS_CLOSE_ABNORMAL = 1006
104
104
*/
105
105
typealias PayloadClosure = () -> Payload ?
106
106
107
+ /* * A closure that will encode a Map<String, Any> into a JSON String */
108
+ typealias EncodeClosure = (Map <String , Any >) -> String
109
+
110
+ /* * A closure that will decode a JSON String into a [Message] */
111
+ typealias DecodeClosure = (String ) -> Message
112
+
113
+
107
114
/* *
108
115
* Connects to a Phoenix Server
109
116
*/
@@ -118,13 +125,15 @@ typealias PayloadClosure = () -> Payload?
118
125
* ```
119
126
* @param url Url to connect to such as https://example.com/socket
120
127
* @param paramsClosure Closure which allows to change parameters sent during connection.
121
- * @param gson Default GSON Client to parse JSON. You can provide your own if needed.
128
+ * @param encode Optional. Provide a custom JSON encoding implementation
129
+ * @param decode Optional. Provide a custom JSON decoding implementation
122
130
* @param client Default OkHttpClient to connect with. You can provide your own if needed.
123
131
*/
124
132
class Socket (
125
133
url : String ,
126
134
val paramsClosure : PayloadClosure ,
127
- private val gson : Gson = Defaults .gson,
135
+ private val encode : EncodeClosure = Defaults .encode,
136
+ private val decode : DecodeClosure = Defaults .decode,
128
137
private val client : OkHttpClient = OkHttpClient .Builder ().build()
129
138
) {
130
139
@@ -226,15 +235,17 @@ class Socket(
226
235
*
227
236
* @param url Url to connect to such as https://example.com/socket
228
237
* @param params Constant parameters to send when connecting. Defaults to null
229
- * @param gson Default GSON Client to parse JSON. You can provide your own if needed.
238
+ * @param encode Optional. Provide a custom JSON encoding implementation
239
+ * @param decode Optional. Provide a custom JSON decoding implementation
230
240
* @param client Default OkHttpClient to connect with. You can provide your own if needed.
231
241
*/
232
242
constructor (
233
243
url: String ,
234
244
params: Payload ? = null ,
235
- gson: Gson = Defaults .gson,
245
+ encode: EncodeClosure = Defaults .encode,
246
+ decode: DecodeClosure = Defaults .decode,
236
247
client: OkHttpClient = OkHttpClient .Builder ().build()
237
- ) : this (url, { params }, gson , client)
248
+ ) : this (url, { params }, encode, decode , client)
238
249
239
250
init {
240
251
var mutableUrl = url
@@ -387,7 +398,7 @@ class Socket(
387
398
ref?.let { body[" ref" ] = it }
388
399
joinRef?.let { body[" join_ref" ] = it }
389
400
390
- val data = gson.toJson (body)
401
+ val data = this .encode (body)
391
402
connection?.let { transport ->
392
403
this .logItems(" Push: Sending $data " )
393
404
transport.send(data)
@@ -569,7 +580,7 @@ class Socket(
569
580
this .logItems(" Receive: $rawMessage " )
570
581
571
582
// Parse the message as JSON
572
- val message = gson.fromJson (rawMessage, Message :: class .java )
583
+ val message = this .decode (rawMessage)
573
584
574
585
// Clear heartbeat ref, preventing a heartbeat timeout disconnect
575
586
if (message.ref == pendingHeartbeatRef) pendingHeartbeatRef = null
0 commit comments