我遇到的问题与此处描述的非常相似:组合类型和字段序列化器
case class(id: Option[UUID], otherValue:String, … )
Run Code Online (Sandbox Code Playgroud)
相关问题的答案将允许我编写自己的格式,但我很好奇是否已经有一个UUID格式隐藏在json4s中的某个地方.我猜想正确的导入可以解决UUID的这个问题,我有点惊讶没有找到任何东西.
我想念它,还是不存在?
我有一个包含许多成员的案例类,其中两个是非原始的:
import com.twitter.util.Duration
case class Foo(
a: Int,
b: Int,
...,
y: Int,
z: Int,
timeoutSeconds: Duration,
runtimeMinutes: Duration)
Run Code Online (Sandbox Code Playgroud)
我想将以下JSON反序列化为此case类的实例:
{
"a": 1,
"b": 2,
// ...
"y": 42,
"z": 43,
"timeoutSeconds": 30,
"runtimeMinutes": 12,
}
Run Code Online (Sandbox Code Playgroud)
通常,我会写json.extract[Foo]
.然而,MappingException
由于timeoutSeconds
和,我得到了明显的runtimeMinutes
.
我看过FieldSerializer
,它允许在AST上进行字段转换.但是,它不够,因为它只允许AST转换.
我也看过扩展CustomSerializer[Duration]
,但没有办法反省哪个JSON密钥正在被处理(timeoutSeconds
或runtimeMinutes
).
我也可以尝试延长CustomSerializer[Foo]
,但后来我将有很多的样板代码,用于提取值a
,b
,..., z
.
理想情况下,我需要一些PartialFunction[JField, T]
作为反序列化器的东西,以便我可以写:
{
case ("timeoutSeconds", JInt(timeout) => timeout.seconds
case ("runtimeMinutes", JInt(runtime) => …
Run Code Online (Sandbox Code Playgroud)