我想知道如何使用Json来实现以下目标:
case class A(s: String, b: B)
object A {
implicit val format = Json.writes[A]
}
case class B(i: Int)
object B {
implicit val format = Json.writes[B]
}有趣的是,Json.writes[A]的扩展使用了B的隐式编写器。
在我的例子中,我写了类似于:
case class MyWriter[T](f: (T) => String)
def myMacro[T]: c.Expr[MyWriter[T]] = ... {
q"""
val i = implicitly[typeOf[MyWriter[$tpe]]
...
"""
}但我得到了couldn't find implicit value for。我已经意识到,如果在声明A之前声明B及其对象,它是有效的,但这肯定不是我期望的原因。
发布于 2019-12-12 13:42:30
您可以使用如下表达式:
def getType(typeTree: Tree): Type = c.typecheck(typeTree, c.TYPEmode).tpe
c.inferImplicitValue(getType(tq"_root_.my_package.MyWriter[$tpe]"), silent = false))推断隐式JSON编解码器的用法的一个示例是here。
https://stackoverflow.com/questions/59296610
复制相似问题