I have below case class
case class Foo(code: Int, msg: String, headers: Map[String,String] = Map.empty)
Below is the code that I've tried so far -
import scodec._
import scodec.codecs._
implicit val mapCodec: Codec[List[(String, String)]] = sizedList()
implicit val fooCodec : Codec[Foo] = {
("code" | int32) :: ("msg" | cstring) :: ("headers" | mapCodec)
}.as[Foo]
I don't know how to write Codec for Map[String, String]
. I checked online documentation but it is still in TODO.
Any idea how can I write the codec for Map[String, String]
?
What you would need to do is to define
Codec
for tuple of strings, which then you will need to use to create codec forList[(String, String)]
which can be converted toMap[String, String]
and vice versa, hence covertCodec
usingxmap
function.So the final solution might look like:
Hope this helps!