scala - json4s 使用默认值提取
问题描述
这是我的测试用例
case class WithDefaultValueHolder(values: Seq[WithDefaultValue])
case class WithDefaultValue(name: String, gender: String = "male")
it("default with valid json"){
val res = WithDefaultValueHolder(Seq(WithDefaultValue("Bob")))
parse("""{"values":[{"name":"Bob","gender":"male"}]}""").extract[WithDefaultValueHolder](
DefaultFormats, Manifest.classType(classOf[WithDefaultValueHolder])) shouldBe (res)
}
下面的测试也通过了。它将格式错误的 json 视为默认值的有效 json。为什么?如何限制这种行为。如何在测试用例下失败?
it("default with invalid json"){
val res = WithDefaultValueHolder(Seq(WithDefaultValue("Bob")))
parse("""{"values":[{"name":"Bob","xyz":"abc"}]}""").extract[WithDefaultValueHolder](
DefaultFormats, Manifest.classType(classOf[WithDefaultValueHolder])) shouldBe (res)
}
解决方案
推荐阅读
- jpeg2000 - jpeg2000解码器的最低内存要求是多少?
- sql-server - 存储过程用于显示表变量中的错误在另一个存储过程中不起作用
- javascript - Ajax返回多个输出?
- c# - Unity,重新加载场景后保持变量的值
- scala - Scala调用多个期货 - 可用时使用队列
- python-3.x - 如何访问
旁边的标签
最后声明:
- javascript - 任何人都可以为 victor 仓库问题提供 js 或 php 解决方案吗?
- sql - 如何在 Athena (Presto) 中使用上个月的每月间隔?
- asp.net - 在服务器内同时使用 WordPress 和 ASP.NET Framework
- javascript - 努力从 discord.js 中删除特定用户的反应。说删除未定义