我正在使用scala案例类和播放框架来处理一些数据。 这是我的代码:
case class myClass(
datastores: Option[List[DataStores]])
case class DataStores(
name: Option[String],
utilization: Option[DataStoreUtilization],
luns: Option[List[DataStoreLuns]]
)
case class DataStoreUtilization(
freeBytes: Option[Long],
sizeBytes: Option[Long],
usedBytes: Option[Long])
case class DataStoreLuns(
model: Option[String],
canonicalName: Option[String],
vendor: Option[String])
object myClass extends ModelCompanion[myClass, ObjectId] {
val collection = MongoClient("mongoserver", 27017)("myDB")("myCollection")
val dao = new SalatDAO[myClass, ObjectId](collection = collection) {}
def apply(rawData: JsValue): myClass = {
val datastoreList = getDataStoreList(rawData)
myClass(datastoreList)
}
private def getDataStoreList(rawData: JsValue): Option[List[DataStores]] = {
val commandInfo = (rawData \ "Commands").as[JsValue]
(commandInfo \ "dataStores").asOpt[List[JsObject]].map { dataStores =>
dataStores map {
dataStoreJson =>
val name = (dataStoreJson \ "name").asOpt[String]
val utilization = getDataStoreUtilization(dataStoreJson)
val luns = getDataStoreLuns(dataStoreJson)
// val virtualMachines = getDataStoreVirtualMachines(dataStoreJson)
DataStores(name, utilization, luns)
}
}
}
private def getDataStoreUtilization(dataStoreJson: JsObject): Option[DataStoreUtilization] = {
(dataStoreJson \ "utilization").asOpt[JsObject].map {
case utilizationJson =>
val freeBytes = (utilizationJson \ "freeBytes").asOpt[Long]
val sizeBytes = (utilizationJson \ "sizeBytes").asOpt[Long]
val usedBytes = (utilizationJson \ "usedBytes").asOpt[Long]
DataStoreUtilization(freeBytes, sizeBytes, usedBytes)
}
}
private def getDataStoreLuns(dataStoreJson: JsValue): Option[List[DataStoreLuns]] = {
(dataStoreJson \ "luns").asOpt[List[JsObject]].map { luns =>
luns map {
lunJson =>
val model = (lunJson \ "model").asOpt[String]
val canonicalName = (lunJson \ "CanonicalName").asOpt[String]
val vendor = (lunJson \ "vendor").asOpt[String]
DataStoreLuns(model, canonicalName, vendor)
}
}
}
}
我收到以下错误:
play - Cannot invoke the action, eventually got an error: java.lang.IllegalArgumentException: can't serialize class ....models.DataStoreUtilization
如何解决此错误?