@@ -104,6 +104,13 @@ const val WS_CLOSE_ABNORMAL = 1006
104104 */
105105typealias PayloadClosure = () -> Payload ?
106106
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+
107114/* *
108115 * Connects to a Phoenix Server
109116 */
@@ -118,13 +125,15 @@ typealias PayloadClosure = () -> Payload?
118125 * ```
119126 * @param url Url to connect to such as https://example.com/socket
120127 * @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
122130 * @param client Default OkHttpClient to connect with. You can provide your own if needed.
123131 */
124132class Socket (
125133 url : String ,
126134 val paramsClosure : PayloadClosure ,
127- private val gson : Gson = Defaults .gson,
135+ private val encode : EncodeClosure = Defaults .encode,
136+ private val decode : DecodeClosure = Defaults .decode,
128137 private val client : OkHttpClient = OkHttpClient .Builder ().build()
129138) {
130139
@@ -226,15 +235,17 @@ class Socket(
226235 *
227236 * @param url Url to connect to such as https://example.com/socket
228237 * @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
230240 * @param client Default OkHttpClient to connect with. You can provide your own if needed.
231241 */
232242 constructor (
233243 url: String ,
234244 params: Payload ? = null ,
235- gson: Gson = Defaults .gson,
245+ encode: EncodeClosure = Defaults .encode,
246+ decode: DecodeClosure = Defaults .decode,
236247 client: OkHttpClient = OkHttpClient .Builder ().build()
237- ) : this (url, { params }, gson , client)
248+ ) : this (url, { params }, encode, decode , client)
238249
239250 init {
240251 var mutableUrl = url
@@ -387,7 +398,7 @@ class Socket(
387398 ref?.let { body[" ref" ] = it }
388399 joinRef?.let { body[" join_ref" ] = it }
389400
390- val data = gson.toJson (body)
401+ val data = this .encode (body)
391402 connection?.let { transport ->
392403 this .logItems(" Push: Sending $data " )
393404 transport.send(data)
@@ -569,7 +580,7 @@ class Socket(
569580 this .logItems(" Receive: $rawMessage " )
570581
571582 // Parse the message as JSON
572- val message = gson.fromJson (rawMessage, Message :: class .java )
583+ val message = this .decode (rawMessage)
573584
574585 // Clear heartbeat ref, preventing a heartbeat timeout disconnect
575586 if (message.ref == pendingHeartbeatRef) pendingHeartbeatRef = null
0 commit comments