Skip to content

Commit

Permalink
refactor: move serialization details to JsonSerializer
Browse files Browse the repository at this point in the history
`SendEvents` shouldn't know details about structure of JSON payload.
  • Loading branch information
wzieba committed Nov 11, 2023
1 parent 940b241 commit fa73ccd
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@ import java.io.IOException
import java.io.StringWriter

internal object JsonSerializer {

fun toParselyEventsPayload(eventsToSend: List<Map<String, Any?>?>): String {
val batchMap: MutableMap<String, Any> = HashMap()
batchMap["events"] = eventsToSend
return toJson(batchMap).orEmpty()
}
/**
* Encode an event Map as JSON.
*
* @param map The Map object to encode as JSON.
* @return The JSON-encoded value of `map`.
*/
fun toJson(map: Map<String, Any>): String? {
private fun toJson(map: Map<String, Any>): String? {
val mapper = ObjectMapper()
var ret: String? = null
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.parsely.parselyandroid

import com.parsely.parselyandroid.JsonSerializer.toJson
import com.parsely.parselyandroid.JsonSerializer.toParselyEventsPayload
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand All @@ -20,9 +20,7 @@ internal class SendEvents(
}
ParselyTracker.PLog("Sending request with %d events", eventsToSend.size)

val batchMap: MutableMap<String, Any> = HashMap()
batchMap["events"] = eventsToSend
val jsonPayload = toJson(batchMap).orEmpty()
val jsonPayload = toParselyEventsPayload(eventsToSend)

ParselyTracker.PLog("POST Data %s", jsonPayload)

Expand Down

0 comments on commit fa73ccd

Please sign in to comment.