使用Scala / Play将JSON转换为带有嵌套对象的case类

ElG*_*abe 3 parsing json scala playframework

说我正在使用的JSON响应的格式如下:

[
    {
       "make": "Tesla",
       "model": "Model S",
       "year": 2017,
       "color": "red",
       "owner": "Bob",
       "max_speed": 200,
       "wheel_size": 30,
       "is_convertible": true,
       "license": "ABC123",
       "cost": 50000,
       "down_payment": 2500,
       "other_property_1": 1,
       "other_property_2": 2,
       "other_property_3": 3,
       "other_property_4": 4,
       "other_property_5": 5,
       "other_property_6": 6,
       "other_property_7": 7,
       "other_property_8": 8,
       "other_property_9": 9,
       "other_property_10": 10,
       "other_property_11": 11
    }
]
Run Code Online (Sandbox Code Playgroud)

此处的JSON是汽车对象的数组(为简单起见,仅为1),我正在尝试使用JSON Reads转换器将其转换为模型。假设我有一个Car案例类来表示每个对象,并且该类具有嵌套的FinancialInfo案例类,以在逻辑上拆分属性的数量,从而避免了Scala的22参数限制。

import play.api.libs.functional.syntax._
import play.api.libs.json._

case class Car(
    make: String,
    model: String,
    year: Int,
    color: String,
    owner: String,
    maxSpeed: Int,
    wheelSize: Int,
    isConvertible: Boolean,
    license: String,
    financialInfo: FinancialInfo, // nested case class to avoid 22 param limit
    otherProperty1: Int,
    otherProperty2: Int,
    otherProperty3: Int,
    otherProperty4: Int,
    otherProperty5: Int,
    otherProperty6: Int,
    otherProperty7: Int,
    otherProperty8: Int,
    otherProperty9: Int,
    otherProperty10: Int,
    otherProperty11: Int
)

object Car {
    implicit val reads: Reads[Car] = (
        (__ \ "make").read[String] and
        (__ \ "model").read[String] and
        (__ \ "year").read[Int] and
        (__ \ "color").read[String] and
        (__ \ "owner").read[String] and
        (__ \ "max_speed").read[Int] and
        (__ \ "wheel_size").read[Int] and
        (__ \ "is_convertible").read[Boolean] and
        (__ \ "license").read[String] and
        (__ \ "financialInfo").read[FinancialInfo] and
        (__ \ "other_property_1").read[Int] and
        (__ \ "other_property_2").read[Int] and
        (__ \ "other_property_3").read[Int] and
        (__ \ "other_property_4").read[Int] and
        (__ \ "other_property_5").read[Int] and
        (__ \ "other_property_6").read[Int] and
        (__ \ "other_property_7").read[Int] and
        (__ \ "other_property_8").read[Int] and
        (__ \ "other_property_9").read[Int] and
        (__ \ "other_property_10").read[Int] and
        (__ \ "other_property_11").read[Int]
    )(Car.apply _)
}

case class FinancialInfo(
   cost: BigDecimal,
   downPayment: BigDecimal
)

object FinancialInfo {
    implicit val reads: Reads[FinancialInfo] = (
        (__ \ "cost").read[BigDecimal] and
        (__ \ "down_payment").read[BigDecimal]
    )(FinancialInfo.apply _)
}
Run Code Online (Sandbox Code Playgroud)

但是,我猜测是因为JSON中没有称为的属性financialInfo,因此无法正确解析它。在我的实际应用程序中,当我使用时遇到此错误response.json.validate[List[Car]]

JsError(List(((0)/financialInfo,List(JsonValidationError(List(error.path.missing),WrappedArray()))))) 
Run Code Online (Sandbox Code Playgroud)

总而言之,在该示例中,cost并且down_payment不包含在嵌套对象中,即使对于Car case类,我也必须包括一个称为的嵌套模型financialInfo。什么是要解决这个错误的最好方式,并确保为值cost,并down_payment可以对其进行解析?任何帮助或见识将不胜感激!

Ant*_*tot 5

Reads 可以合并并相互包含。

因此,具有:

implicit val fiReads: Reads[FinancialInfo] = (
  (JsPath \ "cost").read[BigDecimal] and
  (JsPath \ "down_payment").read[BigDecimal]
  )(FinancialInfo.apply _)
Run Code Online (Sandbox Code Playgroud)

我们可以将其包含到父对象中Reads

implicit val carReads: Reads[Car] = (
  (JsPath \ "make").read[String] and
  (JsPath \ "model").read[String] and
  fiReads  // <--- HERE!
)(Car.apply _)
Run Code Online (Sandbox Code Playgroud)

现在,使用以下JSON:

private val json =
  """
    |[
    |    {
    |       "make": "Tesla",
    |       "model": "Model S",
    |       "cost": 50000,
    |       "down_payment": 2500
    |    },
    |    {
    |       "make": "Tesla",
    |       "model": "Model D",
    |       "cost": 30000,
    |       "down_payment": 1500
    |    }
    |]
  """.stripMargin

val parsedJsValue = Json.parse(json)
val parsed = Json.fromJson[List[Car]](parsedJsValue)

println(parsed)
Run Code Online (Sandbox Code Playgroud)

它被正确解析:

JsSuccess(List(Car(Tesla,Model S,FinancialInfo(50000,2500)), Car(Tesla,Model D,FinancialInfo(30000,1500))),)
Run Code Online (Sandbox Code Playgroud)

ps Reads原始问题中的不需要包装成不同的objects。在相同范围内,相关的隐式值会更好,更接近它们的实际使用位置。