Created
May 4, 2020 18:17
-
-
Save jirick1/92bb07683a78e4eac5c57dd1996f3a32 to your computer and use it in GitHub Desktop.
Scala MultipartFormData to Json String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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