我正在编写一个 Scala restful API 并使用 SprayJson 来解析在 Post 调用期间传入的 JSON。例如,我们有一个如下所示的 JSON 结构:
{"a", "b", "c", "d", "e", "f", "g", "h"}
字段 a、b、c 和 h 是必需的,但其他字段不是。我的案例类有一个自定义 JSON 格式化程序。由于各种原因,我需要构建案例类的方式需要我执行自定义 JSON 格式化程序。
这是我在格式化程序中读取函数的代码片段:
def read(value: JsValue) = {
value.asJsObject.getFields("a", "b", "c", "d", "e", "f", "g", "h")
case Seq(JsString(a),JsString(b),JsString(c),JsString(d),JsString(e),JsString(f),JsString(g),JsString(h))
new Object(a,b,c,d,e,f,g,h)
case _ => throw new DeserializationException("Object expected")
}
如何在没有大量案例字符串匹配可能出现的每个可能的字段排列的情况下实现上述内容?