此代码应该将JSON数组或JSON对象反序列化为其各自的scala类。我遇到的问题是这一行
case x if x <:< typeOf[Array[_]] => deserialize[TypeTag[_]](s.getJSONArray(i))'
我希望函数检测X类型参数是否是某个东西的数组,并将该东西作为类型参数传递给递归函数调用。
因此,如果我致电deserialize[Array[Int]](JSONArray)
,则应返回Array[Array[Int]]
。我需要以某种方式从Int
中提取Array[Int]
并将其传递给deserialize[???](JSONArray)
。
def deserialize[X: TypeTag](s: JSONObject): X = {
val m = ru.runtimeMirror(getClass.getClassLoader)
val classType = ru.typeOf[X].typeSymbol.asClass
val cm = m.reflectClass(classType)
val constructor = typeTag.tpe.decl(ru.termNames.CONSTRUCTOR).asMethod
val constructorMethod = cm.reflectConstructor(constructor)
val params = constructor.asMethod.paramLists.head
val args = new Array[Any](params.length)
for(i <- params.indices) {
val name = params(i).name.decodedName.toString
println(params(i).typeSignature.toString)
args(i) = params(i).typeSignature match {
case t if t =:= typeOf[String] => s.getString(name)
case t if t =:= typeOf[Int] => s.getInt(name)
case t if t =:= typeOf[Double] => s.getDouble(name)
case t if t =:= typeOf[Boolean] => s.getBoolean(name)
case t if t =:= typeOf[Long] => s.getLong(name)
case t if t =:= typeOf[Array[_]] => deserialize[WeakTypeTag[_]](s.getJSONArray(name))
case t => deserialize[t.type](s.getJSONObject(name))
}
}
constructorMethod(args:_*).asInstanceOf[X]
}
def deserialize[X: ClassTag: TypeTag](s: JSONArray): Array[X] = {
val arr = new Array[X](s.length())
for(i <- 0 until s.length) {
arr(i) = (typeOf[X] match {
case x if x =:= typeOf[String] => s.getString(i)
case x if x =:= typeOf[Int] => s.getInt(i)
case x if x =:= typeOf[Double] => s.getInt(i)
case x if x =:= typeOf[Boolean] => s.getInt(i)
case x if x =:= typeOf[Long] => s.getInt(i)
case x if x <:< typeOf[Array[_]] => deserialize[TypeTag[_]](s.getJSONArray(i))
case x => deserialize[X](s.getJSONObject(i))
}).asInstanceOf[X]
}
arr
}
答案 0 :(得分:1)
因此,虽然我不太明白为什么它可行,但我能够根据一堆其他stackoverflow问题一起破解一个有效的解决方案。
def deserialize[X](s: JSONObject)(implicit tTag: TypeTag[X]): X = {
val m = ru.runtimeMirror(getClass.getClassLoader)
val classType = ru.typeOf[X].typeSymbol.asClass
val cm = m.reflectClass(classType)
val constructor = typeTag.tpe.decl(ru.termNames.CONSTRUCTOR).asMethod
val constructorMethod = cm.reflectConstructor(constructor)
val params = constructor.asMethod.paramLists.head
val args = new Array[Any](params.length)
for(i <- params.indices) {
val name = params(i).name.decodedName.toString
args(i) = params(i).typeSignature match {
case t if t =:= typeOf[String] => s.getString(name)
case t if t =:= typeOf[Int] => s.getInt(name)
case t if t =:= typeOf[Double] => s.getDouble(name)
case t if t =:= typeOf[Boolean] => s.getBoolean(name)
case t if t =:= typeOf[Long] => s.getLong(name)
case t if t <:< typeOf[Array[_]] => {
deserialize(s.getJSONArray(name))(getSubInfo(0)(typeToTypeTag(t, m)))
}
case t => deserialize(s.getJSONObject(name))(typeToTypeTag(t, m))
}
}
constructorMethod(args:_*).asInstanceOf[X]
}
def deserialize[X](s: JSONArray)(implicit tTag: TypeTag[X]): Array[X] = {
val mirror = runtimeMirror(getClass.getClassLoader)
implicit val xClassTag = ClassTag[X](mirror.runtimeClass(tTag.tpe))
val arr = new Array[X](s.length())
for(i <- 0 until s.length) {
arr(i) = (typeOf[X] match {
case x if x =:= typeOf[String] => s.getString(i)
case x if x =:= typeOf[Int] => s.getInt(i)
case x if x =:= typeOf[Double] => s.getInt(i)
case x if x =:= typeOf[Boolean] => s.getInt(i)
case x if x =:= typeOf[Long] => s.getInt(i)
case x if x <:< typeOf[Array[_]] => {
deserialize(s.getJSONArray(i))(getSubInfo[X](0))
}
case x => {
deserialize[X](s.getJSONObject(i))
}
}).asInstanceOf[X]
}
arr
}
def typeToTypeTag[T](tpe: Type, mirror: reflect.api.Mirror[reflect.runtime.universe.type]): TypeTag[T] = {
TypeTag(mirror, new reflect.api.TypeCreator {
def apply[U <: reflect.api.Universe with Singleton](m: reflect.api.Mirror[U]) = {
assert(m eq mirror, s"TypeTag[$tpe] defined in $mirror cannot be migrated to $m.")
tpe.asInstanceOf[U#Type]
}
})
}
def getSubInfo[X](i: Int)(implicit tTag: TypeTag[X]): TypeTag[_] = {
typeToTypeTag(tTag.tpe.asInstanceOf[TypeRefApi].args(i), tTag.mirror)
}
解决方案的本质是最后的功能。 getSubInfo [X](i:Int)将在i处返回type参数的TypeTag。因此,调用getSubInfoArray [Int]将返回Int的TypeTag。 typeToTypeTag将反射#type作为参数,并使用黑魔法创建一个类型标签。