3

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)?

Laurence Geng
  • 424
  • 3
  • 9
  • Anything speaks against parsing it to proper scala data structures, work with these, and then serializing them again? – Reactormonk Feb 02 '17 at 09:54
  • considering performance, i don't want to serialize and then deserialize. I want to tranform the json string wihtout case class. – Laurence Geng Feb 02 '17 at 10:29
  • You're deserializing anyway. You'd just add in an extra decoding step. How much data do you want to process? – Reactormonk Feb 02 '17 at 10:31
  • anyway, decoding is not my expected answer. – Laurence Geng Feb 02 '17 at 12:02
  • Well... you have some mis-conception about what exactly are you doing. you have already deserialized/decoded the Json String into Cerce's Scala objects. And the best "performant" way or doing what you want is to use some intermediate case class. – sarveshseri Feb 02 '17 at 15:45

2 Answers2

5

You can do it with circe cursors, but i think its easier to just convert to a case class and operate on that.

 val json =
  """
    |{
    |    "metric":"my-metric",
    |    "dps":{"1484214238":5,"1484214239":1,"1484214240":4,"1484214241":11}
    |  }
  """.stripMargin

val doc = parse(json).getOrElse(Json.Null)
val cursor = doc.hcursor
val dps = cursor.downField("dps")
val result = dps.withFocus { json: Json =>
  json.mapObject { jsonObject =>
    JsonObject.fromMap(jsonObject.toMap.mapValues { x =>
      Json.fromString(x.asNumber.get.toString)
    })
  }
}
Bradley Kaiser
  • 776
  • 4
  • 16
1

Naive solution:

val doubleValue: Json => Json = root.each.dps.each.json.modify(x =>
  Json.fromString(
    (x.asNumber.getOrElse(throw new Exception("ups")).truncateToInt * 2).toString
  )
)
tonykoval
  • 958
  • 7
  • 18