我的案例类具有超过22个参数。
case class Model(a1: Int,
a2: Int,
a3: Int,
a4: Int,
a5: Int,
a6: Int,
a7: Int,
a8: Int,
a9: Int,
a10: Int,
a11: Int,
a12: Int,
a13: Int,
a14: Int,
a15: Int,
a16: Int,
a17: Int,
a18: Int,
a19: Int,
a20: Int,
a21: Int,
a22: Int,
a23: Int,
a24: Int)
我收到一个json输入,需要将其反序列化为上述Model
case类。但是,我的输入json不包含字段a1和a2。我的json看起来像这样
{
"a3": 3,
"a4": 4,
"a5": 5,
"a6": 6,
"a7": 7,
"a8": 8,
"a9": 9,
"a10": 10,
"a11": 11,
"a12": 12,
"a13": 13,
"a14": 14,
"a15": 15,
"a16": 16,
"a17": 17,
"a18": 18,
"a19": 19,
"a20": 20,
"a21": 21,
"a22": 22,
"a23": 23,
"a24": 24
}
现在,为了适应这种情况,我编写了自定义json读取器,该读取器将为json中的a1和a2字段添加一些虚拟值。
我有一个自定义方法,可将字段添加到读取的输入json中。扩展方法addField
如下
implicit class ReadOps[A](reads: Reads[A]) {
def addField(fieldName: String, value: Int): Reads[A] = Reads {
json: JsValue =>
json
.validate(__.json.update((__ \ fieldName).json.put(JsString(value.toString))))
.map(_.asInstanceOf[A])
}
}
我的隐式json格式如下
implicit val jsonFormat = new OFormat[Model] {
override def reads(json: JsValue): JsResult[Model] = {
Jsonx.formatCaseClass[Model].addField("a1",1).addField("a2",2).reads(json)
}
override def writes(o: Model): JsObject = {
Jsonx.formatCaseClass[Model].writes(o)
}
}
我面临的问题
尽管调用方法addField两次(我试图添加值为1的a1和值为2的a2),但只有值为a1
的字段1
被添加到json中,而a2为忽略了。而且json不会反序列化到模型案例类。
我正在使用以下依赖项
libraryDependencies += "ai.x" %% "play-json-extensions" % "0.30.1"
请让我知道该方法在哪里出错。任何指针将非常有帮助。在此先感谢!!!
注意:请不要建议在模型案例类中添加诸如将a1和a2添加为字段的解决方案。这是毫无疑问的,因为我的实际问题非常复杂,并且关于stackoverflow的问题是当前问题的非常简化的版本。
答案 0 :(得分:0)
通过adding a field in a new branch考虑JsPath.update
:
val js = Json.obj("key1" -> "value1", "key2" -> "value2") js.validate(__.json.update((__ \ 'key3).json.put(JsString("value3")))) => JsSuccess({"key1":"value1","key2":"value2","key3":"value3"},)
这是一个可行的例子
import ai.x.play.json.Jsonx
import play.api.libs.json.Json
import play.api.libs.json._
case class Foo(a1: Int, a2: Int, a3: Int, a4: Int, a5: Int, a6: Int, a7: Int, a8: Int, a9: Int, a10: Int, a11: Int, a12: Int, a13: Int, a14: Int, a15: Int, a16: Int, a17: Int, a18: Int, a19: Int, a20: Int, a21: Int, a22: Int, a23: Int, a24: Int)
object Foo {
implicit val format = Jsonx.formatCaseClass[Foo]
}
object AddFieldsToDeserialisation extends App {
val raw =
"""
|{
| "a3": 3,
| "a4": 4,
| "a5": 5,
| "a6": 6,
| "a7": 7,
| "a8": 8,
| "a9": 9,
| "a10": 10,
| "a11": 11,
| "a12": 12,
| "a13": 13,
| "a14": 14,
| "a15": 15,
| "a16": 16,
| "a17": 17,
| "a18": 18,
| "a19": 19,
| "a20": 20,
| "a21": 21,
| "a22": 22,
| "a23": 23,
| "a24": 24
|}
""".stripMargin
val json = Json.parse(raw)
val updatePutTransformation =
(field: String, value: Int) => __.json.update((__ \ field ).json.put(JsNumber(value)))
val addedFields =
List("a1" -> 1, "a2" -> 2)
.map { case (field, value) => updatePutTransformation(field, value) }
.reduce((putUpdate1, putUpdate2) => putUpdate1 andThen putUpdate2 )
val jsonWithPutValue = json.transform(addedFields)
println(jsonWithPutValue.get)
println(jsonWithPutValue.get.as[Foo])
}
应输出
{ "a1": 1, "a2": 2, "a3": 3, "a4": 4, "a5": 5, "a6": 6, "a7": 7, "a8": 8, "a9": 9, "a10": 10, "a11": 11, "a12": 12, "a13": 13, "a14": 14, "a15": 15, "a16": 16, "a17": 17, "a18": 18, "a19": 19, "a20": 20, "a21": 21, "a22": 22, "a23": 23, "a24": 24 }
Foo(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24)