Skip to content

Commit

Permalink
fix up the json parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Nov 30, 2023
1 parent 25541cc commit e77a713
Showing 1 changed file with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import java.net.URL
class ContentJson(
val type: ContentTypeId,
val content: Any?,
val encodedContent: EncodedContent? = null,
private val encodedContent: EncodedContent? = null,
) {
constructor(encoded: EncodedContent) : this(
type = encoded.type,
Expand Down Expand Up @@ -173,13 +173,19 @@ class ContentJson(
)

else -> {
val gson = GsonBuilder().create()
val encodedContentJSON = try {
gson.toJson(encodedContent).toString()
} catch (e: Exception) {
null
val json = JsonObject()
encodedContent?.let {
val gson = GsonBuilder().create()
val encodedType = gson.toJson(encodedContent.type)
val parameters = gson.toJson(encodedContent.parametersMap)


json.addProperty("fallback", encodedContent.fallback)
json.add("parameters", JsonParser.parseString(parameters))
json.add("type", JsonParser.parseString(encodedType))
}
if (!encodedContentJSON.isNullOrBlank()) {
val encodedContentJSON = json.toString()
if (encodedContentJSON.isNotBlank()) {
mapOf("encoded" to encodedContentJSON)
} else {
mapOf(
Expand All @@ -191,4 +197,4 @@ class ContentJson(
}
}
}
}
}

0 comments on commit e77a713

Please sign in to comment.