从泛型类型参数中获取类型参数并在递归函数调用中使用



这段代码应该将JSON数组或JSON对象反序列化到其各自的scala类中。我遇到的问题是这一行

case x if x <:< typeOf[Array[_]] => deserialize[TypeTag[_]](s.getJSONArray(i))'

我想让函数检测X类型参数是否为某个东西的数组,并将该东西作为类型参数传递给递归函数调用。如果我调用deserialize[Array[Int]](JSONArray),它应该返回一个Array[Array[Int]]。我需要从Array[Int]中提取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
}

所以,虽然我不太明白为什么它的工作原理,我能够破解一个工作的解决方案在一起基于一堆其他的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的TypeTag。因此调用getSubInfoArray[Int]将返回Int的TypeTag。typeToTypeTag接受一个反射#类型作为参数,并使用黑魔法创建一个typetag

最新更新