[英]Is it possible to write a "zipAll" function for scala inductive list (shapeless.HList in scala 2 or Tuple in scala 3)?
[英]How to convert JSON to scala shapeless.hlist?
我得到了像 {"name":"susan","age":25} 这样的 json,以及像 "name:String,age:Int" 这样的 json 键集的提示,如何从那个 json 创建一个 HList?
根据您添加然后删除的代码,似乎有一个运行时字符串提示"name:String,age:Int"
和运行时字符串 json {"name":"susan","age":25}
,您想要获得一个带有 运行时反射的HList
。 您可以按如下方式执行此操作
import shapeless.HList
import scala.reflect.runtime
import scala.reflect.runtime.universe._
import scala.tools.reflect.ToolBox
val tb = runtime.currentMirror.mkToolBox()
val jsonStr = """{"name":"susan","age":25}"""
val hint = "name:String,age:Int"
val classType = tb.define(tb.parse(s"case class Test($hint)").asInstanceOf[ImplDef]).asClass.toType
val hlist = tb.eval(q"""
import io.circe.generic.auto._
import io.circe.parser.decode
val classInstance = decode[$classType]($jsonStr)
import shapeless.Generic
Generic[$classType].to(classInstance.toOption.get)
""").asInstanceOf[HList]
println(hlist) // susan :: 25 :: HNil
请注意,您在运行时执行所有操作,因此您将无法在编译时访问String :: Int :: HNil
类型,而hlist
静态类型仅为HList
(不是String :: Int :: HNil
),而HList
实际上并不好不仅仅是List[Any]
。
生成.sbt
libraryDependencies ++= Seq(
scalaOrganization.value % "scala-reflect" % scalaVersion.value,
scalaOrganization.value % "scala-compiler" % scalaVersion.value,
"com.chuusai" %% "shapeless" % "2.4.0-M1",
"io.circe" %% "circe-core" % "0.13.0",
"io.circe" %% "circe-parser" % "0.13.0",
"io.circe" %% "circe-generic" % "0.13.0"
)
实际上,我想我们做了一些奇怪的事情。 我们使用的是高度的类型级库(不成形,瑟茜目的是静态类型安全),然后在与反射无视类型安全和实际得到的运行时运行这些List[Any]
( HList
)。
我想如果List[Any]
(字段值列表)对你来说足够了,那么你只需要使用更多的运行时库。 例如,使用json4s
import org.json4s.{JInt, JObject, JString, JValue}
import org.json4s.jackson.JsonMethods._
val jsonStr: String = """{"name":"susan","age":25}"""
val json: JValue = parse(jsonStr) //JObject(List((name,JString(susan)), (age,JInt(25))))
val l: List[JValue] = json.asInstanceOf[JObject].obj.map(_._2) //List(JString(susan), JInt(25))
val res: List[Any] = l.map {
case JString(s) => s
case JInt(n) => n
} //List(susan, 25)
生成.sbt
libraryDependencies += "org.json4s" %% "json4s-jackson" % "3.6.9"
其实同样可以用 Circe 来完成,只是用parse
而不是decode[A]
import io.circe.{Json, JsonNumber}
import io.circe.parser.parse
val jsonStr: String = """{"name":"susan","age":25}"""
val json: Json = parse(jsonStr).toOption.get //{"name":"susan","age":25}
val l: List[Json] = json.asObject.get.values.toList //List("susan", 25)
val res: List[Any] = l.map(_.fold[Any](null, null, (_: JsonNumber).toInt.get, identity[String], null, null)) //List(susan, 25)
如果您需要 case 类或元组的实例而不是HList
替换
tb.eval(q"""
import io.circe.generic.auto._
import io.circe.parser.decode
val classInstance = decode[$classType]($jsonStr)
import shapeless.Generic
Generic[$classType].to(classInstance.toOption.get)
""").asInstanceOf[HList] // susan :: 25 :: HNil
和
tb.eval(q"""
import io.circe.generic.auto._
import io.circe.parser.decode
decode[$classType]($json).toOption.get
""").asInstanceOf[Product] //Test(susan,25)
或者
tb.eval(q"""
import io.circe.generic.auto._
import io.circe.parser.decode
val classInstance = decode[$classType]($json)
import shapeless.Generic
Generic[$classType].to(classInstance.toOption.get).tupled
""").asInstanceOf[Product] //(susan,25)
相应地。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.