I am using Scala 2.12.3 and I have a Map[String, Set[String]]
and I convert this Map to a Json String by using mapper.writeValueAsString(map)
.
Later I call a web service for which I send this Json String as Request parameter. But the question is, I don't want any Pair my map should have more than 100 elements for each entry. For this example I am using 2 as limit.
If there are any, then my plan is to split these exceeding elements for the particular Key and prepare Map again, create the Json String again and call the webserver. I need to repeat this until I call the web service for all the entries in Map.
class ABC(productType: String, productValue: String)
val mapper: ObjectMapper = new ObjectMapper()
val keySet = [ABC("abc", "123"), ABC("def", "456"), ABC("abc", "675"), ABC("abc", "982"), ABC("abc", "211"), ABC("def", "321"), ABC("xyz", "908")]
val requestMap = keysSet.foldLeft(Map[String, Set[String]] ()) { (k,v) =>
val type = v.productType match {
case "abc" => "abcList"
case "def" => "defList"
case "xyz" => "xyzList"
}
k+k.get(productType).map(x=> productValue-> (x+v.productType)).getOrElse(productValue-> Set(v.productType))
}
val json = mapper.writeValueAsString(requestMap)
I am expecting , the last line val json = mapper.wrtieValueAsString
should be in a loop with each requestMap
should have at the max 2 elements for each "abc", "def" and "xyz".
type
keyword in identifier position, noval
s inABC
, weird[
-]
-parens for something list-like, bunch of singleton types in all kind of places where it doesn't belong? Are you sure this compiles in 2.12? Also, definitions ofmapper
andclient
are not in the example, are they really relevant for the question? – Andrey Tyukin