我有以下JSON:
{
"properties" : {
"timestamp" : "1970-01-01T01:00:00+01:00",
"attributes" : [
{
"name" : "Weather",
"value" : "Cloudy",
"fieldDataType" : "string"
},
{
"name" : "pH",
"value" : 7.2,
"fieldDataType" : "double"
},
{
"name" : "Quality Indicator",
"value" : 2,
"fieldDataType" : "integer"
}
]
}
我希望使用Play JSON库解析它。我已经能够处理“时间戳”但是在解析“值”字段时遇到困难,因为它的类型由“fieldDataType”决定。到目前为止,我有:
sealed trait AttributeValue
case class AttributeInt(value: Integer) extends AttributeValue
case class AttributeDouble(value: Double) extends AttributeValue
case class AttributeString(value: String) extends AttributeValue
case class Attribute (name: String, value: AttributeValue)
object Attribute {
implicit val attributeReads: Reads[Attribute] = (
(JsPath \ "name").read[String] and
(JsPath \ "fieldDataType").read[String] // ???
)(Attribute.apply _)
}
我希望能够读取“fieldDataType”,然后根据其值读取“value”字段。因此,如果“fieldDataType”是字符串,则将“value”作为字符串读取,如果“fieldDataType”是“整数”,则将“value”读作整数等。
答案 0 :(得分:3)
首先,我允许自己将AttributeInt
声明更改为:
case class AttributeInt(value: Int) extends AttributeValue
使用默认的Int
解析器
接下来,您可以定义此类Reads
提供商:
val attributeByDatatype: PartialFunction[String, JsPath => Reads[AttributeValue]] = {
case "integer" => _.read[Int].map(AttributeInt)
case "double" => _.read[Double].map(AttributeDouble)
case "string" => _.read[String].map(AttributeString)
}
PartialFunction
允许它不仅处理特定的数据类型,而且还提供有关它知道的数据类型和不知道的数据类型的信息,这对于Reads.collect
方法非常有用,同时生成Reads
可能是flatMap
现在您可以按如下方式更改attributeReads
:
object Attribute {
implicit val attributeReads: Reads[Attribute] = (
(JsPath \ "name").read[String] and
(JsPath \ "fieldDataType")
.read[String]
.collect(ValidationError("datatype unknown"))(attributeByDatatype)
.flatMap(_(JsPath \ "value"))
)(Attribute.apply _)
}