I need capture int values of a json and change it to a string value by a certain mapping table. I use circe, i had known how to modify a value without changing value type, for example:
package com.accenture.soh.driver
import io.circe._, io.circe.parser._
import cats.syntax.either._
import io.circe._, io.circe.parser._
import io.circe.optics.JsonPath._
/**
* Created by laurence.geng on 2017/2/2.
*/
object CirceTest extends App {
val json: Json = parse(
"""
[
{
"metric":"my-metric",
"dps":{"1484214238":5,"1484214239":1,"1484214240":4,"1484214241":11}
}
]
""").getOrElse(Json.Null)
val doubleValue: Json => Json = root.each.dps.each.int.modify(_ * 2)
println(doubleValue(json).toString())
}
The output is:
[
{
"metric" : "my-metric",
"dps" : {
"1484214238" : 10,
"1484214239" : 2,
"1484214240" : 8,
"1484214241" : 22
}
}
]
But, actully, what i need to do is change the int value a certain string value, i.e. 1 -> "A", 2 -> "B", and so on, the problem is: the "modify" method only accept a function which return the same type to input value, so, i can NOT code as following:
val intToString: Json => Json = root.each.dps.each.int.modify(_.toString)
and my expected output may looks as following:
[
{
"metric" : "my-metric",
"dps" : {
"1484214238" : "10",
"1484214239" : "2",
"1484214240" : "8",
"1484214241" : "22"
}
}
]
Can anybody give me a workround (based on Circe)?