如何将JSON转换为scala-shapeless.hlist



我得到了类似于{quot;name":quot;susan"25}的json,以及类似于"name:字符串,age:Int",如何从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]好。

build.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"
)

事实上,我想我们做了一些奇怪的事情。我们使用旨在静态类型安全的高度类型级库(shapeless,cire(,然后在运行时运行它们,反射忽略类型安全,实际得到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)

build.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)

如果需要事例类或元组的实例而不是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)

相应地。

最新更新