I'm converting a json-like string into json, and the following code works in the scala repl

import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.JsonDSL.WithDouble._
import org.json4s.native.JsonMethods._

val value = "{100:1.50;500:1.00;1000:0.50}"

val data = value.stripPrefix("{").stripSuffix("}").split(";").map(a => {
  val b = a.split(":")
  (b(0),b(1))
}).toMap
compact(render(data))

But when it is compiled, I'm getting the following error

[error] ... type mismatch;
[error]  found   : scala.collection.immutable.Map[String,String]
[error]  required: org.json4s.JValue
[error]     (which expands to)  org.json4s.JsonAST.JValue
[error]       compact(render(data))
[error]                      ^

Why is this, and how might I fix it?

I suspect something with the type system that is over my head.

 

 

up vote2down voteaccepted

render() is imported from JsonMethods._ and it actually requires a JValue. You have imported an implicit map2jvalue twice from those two imports import org.json4s.JsonDSL._ and import org.json4s.JsonDSL.WithDouble._.

I suspect that the compiler didn't find the implicit due to the ambiguous imports, try to be more selective: the 3rd import seems redundant (the one with JsonDSL.WithDouble._).

Sometimes you can run scalac with -Xlog-implicits to see why implicits are not used.

相关文章:

  • 2021-06-08
  • 2022-12-23
  • 2021-07-05
  • 2021-12-15
  • 2021-09-25
  • 2022-03-09
  • 2021-08-21
  • 2021-10-05
猜你喜欢
  • 2022-12-23
  • 2021-05-09
  • 2021-07-18
  • 2022-02-14
  • 2021-10-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案