无法让 json4s 呈现我的案例 class?

Can't get json4s to render my case class?

我设置了一个 SBT 控制台,例如...

import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.JsonDSL._
case class TagOptionOrNull(tag: String, optionUuid: Option[java.util.UUID], uuid: java.util.UUID)
val t1 = new TagOptionOrNull("t1", Some(java.util.UUID.randomUUID), java.util.UUID.randomUUID)
val t2 = new TagOptionOrNull("t2", None, null)

我正在尝试查看 json4s 围绕 null 与 Option[UUID] 的行为。但是我无法弄清楚如何调用它来使我的案例 class 成为 JSON.

的字符串
scala> implicit val formats = DefaultFormats
formats: org.json4s.DefaultFormats.type = org.json4s.DefaultFormats$@614275d5

scala> compact(render(t1))
<console>:23: error: type mismatch;
 found   : TagOptionOrNull
 required: org.json4s.JValue
    (which expands to)  org.json4s.JsonAST.JValue
       compact(render(t1))

我错过了什么?

Serialization.write 应该能够像这样 class 序列化

import org.json4s.native.Serialization.write
implicit val formats = DefaultFormats ++ JavaTypesSerializers.all
println(write(t1))

应该输出

{"tag":"t1","optionUuid":"95645021-f60c-4708-8bf3-9d5609559fdb","uuid":"19cc4979-5836-4edf-aedd-dcb3e96f17d6"}

注意序列化 UUID 我们需要来自

JavaTypeSerializers 格式
libraryDependencies += "org.json4s" %% "json4s-ext" % version