Circe:Json 对象到自定义对象
Circe: Json object to Custom Object
我去了这个json节点
...
"businessKeys": [
"product://color?product_code",
"product://?code"
]
...
经过一系列计算后,我得到了一个 Json 距离,但我无法理解如何将该对象转换为我的对象列表。所以 Json (istance) 到 List[MyClass].
MyClass(root: ParameterType, children: List[ParameterType] = List.empty)
ParameterType(parameter: String)
我有一个将 String 转换为 MyClass 实例的方法,所以我需要将 Json 解码为 List[String] 然后调用我的函数,或者有一个直接的方法?
谢谢
您需要实现我们自己的 io.circe.Decoder
,在您的情况下,它可以从解码器转换为字符串。
请在下面找到一些示例代码:
import io.circe._
import io.circe.generic.auto._
object CirceExample {
class ParameterType(parameter: String) {
override def toString: String = parameter
}
class MyClass(root: ParameterType, children: List[ParameterType] = List.empty) {
override def toString: String = s"MyClass($root, $children)"
}
object MyClass {
/**
* Parsing logic from string goes here, for sake of example, it just returns empty result
*/
def parse(product: String): Either[String, MyClass] = {
Right(new MyClass(new ParameterType("example")))
}
// This is what need - just declare own decoder, so the rest of circe infrastructure can use it to parse json to your own class.
implicit val decoder: Decoder[MyClass] = Decoder[String].emap(parse)
}
case class BusinessKeys(businessKeys: List[MyClass])
def main(args: Array[String]): Unit = {
val json = "{ \"businessKeys\": [\n \"product://color?product_code\",\n \"product://?code\"\n ] }"
println(parser.parse(json).map(_.as[BusinessKeys]))
}
}
在我的例子中产生了下一个输出:
Right(Right(BusinessKeys(List(MyClass(example, List()), MyClass(example, List())))))
希望对您有所帮助!
我去了这个json节点
...
"businessKeys": [
"product://color?product_code",
"product://?code"
]
...
经过一系列计算后,我得到了一个 Json 距离,但我无法理解如何将该对象转换为我的对象列表。所以 Json (istance) 到 List[MyClass].
MyClass(root: ParameterType, children: List[ParameterType] = List.empty)
ParameterType(parameter: String)
我有一个将 String 转换为 MyClass 实例的方法,所以我需要将 Json 解码为 List[String] 然后调用我的函数,或者有一个直接的方法? 谢谢
您需要实现我们自己的 io.circe.Decoder
,在您的情况下,它可以从解码器转换为字符串。
请在下面找到一些示例代码:
import io.circe._
import io.circe.generic.auto._
object CirceExample {
class ParameterType(parameter: String) {
override def toString: String = parameter
}
class MyClass(root: ParameterType, children: List[ParameterType] = List.empty) {
override def toString: String = s"MyClass($root, $children)"
}
object MyClass {
/**
* Parsing logic from string goes here, for sake of example, it just returns empty result
*/
def parse(product: String): Either[String, MyClass] = {
Right(new MyClass(new ParameterType("example")))
}
// This is what need - just declare own decoder, so the rest of circe infrastructure can use it to parse json to your own class.
implicit val decoder: Decoder[MyClass] = Decoder[String].emap(parse)
}
case class BusinessKeys(businessKeys: List[MyClass])
def main(args: Array[String]): Unit = {
val json = "{ \"businessKeys\": [\n \"product://color?product_code\",\n \"product://?code\"\n ] }"
println(parser.parse(json).map(_.as[BusinessKeys]))
}
}
在我的例子中产生了下一个输出:
Right(Right(BusinessKeys(List(MyClass(example, List()), MyClass(example, List())))))
希望对您有所帮助!