根据其他字段指定的类型读取中的JSON字段
我的JSON如下所示:根据其他字段指定的类型读取中的JSON字段,json,scala,playframework,Json,Scala,Playframework,我的JSON如下所示: { "properties" : { "timestamp" : "1970-01-01T01:00:00+01:00", "attributes" : [ { "name" : "Weather", "value" : "Cloudy", "fieldDataType" : "string" }, { "name" : "pH", "v
{
"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-libs解析它。我已经能够处理“timestamp”,但是在解析“value”字段时遇到了困难,因为它的类型是由“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”是“integer”,则将“value”读取为整数等。首先,我允许自己将您的
AttributeInt
声明更改为:
case class AttributeInt(value: Int) extends AttributeValue
使用默认的Int
解析器
接下来,您可以定义这样的读取
提供程序:
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
不仅允许它处理特定的数据类型,而且还提供关于它知道什么数据类型和不知道什么数据类型的信息,这对于读取非常有用。collect
方法,而产生的读取
可能是flatMap
现在,您可以按如下方式更改您的属性READS
:
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 _)
}