Skip to content

Instantly share code, notes, and snippets.

@jirick1
Created May 4, 2020 18:17
Show Gist options
  • Save jirick1/92bb07683a78e4eac5c57dd1996f3a32 to your computer and use it in GitHub Desktop.
Save jirick1/92bb07683a78e4eac5c57dd1996f3a32 to your computer and use it in GitHub Desktop.
Scala MultipartFormData to Json String
def multipartToJsonString(req: Option[MultipartFormData[Files.TemporaryFile]]): String = {
val jsFields = req.head.dataParts.flatMap {
case (key, values) => values.map { value =>
if (key == "product_id" || !isAllDigits(value)) s""" "$key": "$value" """ // treat product_id as a string
else s""" "$key": ${value.toInt} """
}
}
s"{${jsFields.mkString(",")}}"
}
private def isAllDigits(x: String): Boolean = x forall Character.isDigit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment