Java Jackson/Scala不可变案例类:根据JSON中的其他值解析类
我有这样一个JSON:Java Jackson/Scala不可变案例类:根据JSON中的其他值解析类,java,scala,jackson,jackson-module-scala,Java,Scala,Jackson,Jackson Module Scala,我有这样一个JSON: { "switch": "foo", "items": [ {"type": "one"}, {"type": "two"} ] } 我想将其加载到类的结构中,如下所示: case class MyFile( @JsonProperty("switch") _switch: String, @JsonProperty("items") _items: JList[Item], ) { val items: List[Item] =
{
"switch": "foo",
"items": [
{"type": "one"},
{"type": "two"}
]
}
我想将其加载到类的结构中,如下所示:
case class MyFile(
@JsonProperty("switch") _switch: String,
@JsonProperty("items") _items: JList[Item],
) {
val items: List[Item] = _items.toList
}
case class Item(t: String)
object Item {
@JsonCreator
def create(@JsonProperty("type") _type: String): Item = {
Item(t) // <= conversion
}
}
然而,在解析过程中(即在构造函数中或在@JsonCreator
静态方法中),我似乎找不到访问JSON树部分的方法
到目前为止,我只得到了一个全局变量,如:
case class MyFile(
@JsonProperty("switch") _switch: String,
@JsonProperty("items") _items: JList[Item],
) {
MyFile.globalSwitch = _switch
val items: List[Item] = _items.toList
}
object MyFile {
var globalSwitch = ""
}
case class Item(t: String)
object Item {
@JsonCreator
def create(@JsonProperty("type") _type: String): Item = {
Item(t + "_" + MyFile.globalSwitch) // <= conversion
}
}
case类MyFile(
@JsonProperty(“开关”)\u开关:字符串,
@JsonProperty(“项目”)\u项目:JList[项目],
) {
MyFile.globalSwitch=\u开关
val项目:列表[项目]=\u items.toList
}
对象MyFile{
var globalSwitch=“”
}
案例类项目(t:字符串)
对象项{
@JsonCreator
def create(@JsonProperty(“type”)_type:String):项={
Item(t+“”+MyFile.globalSwitch)/我认为它将帮助您:
res:
解析:
parse: NamedList[Item] = NamedList(Alex, List(Item(Moran), Item(Sem)))
切换器可以是~~
implicit class Switcher[A <: Item](data: NamedList[A]){
def getEr = data.name match{
case "Jone" => ("It`s Jone", data)
case "Alex" => ("It`s Alex", data)
}
def getErFunc[T](func : (NamedList[A], String) => T) =
data.name match{
case "Jone" => ("It`s Jone", func(data , "Param"))
case "Alex" => ("It`s Alex", func(data, "Not Param"))
}
}
val res2 = parse.getEr
val res3 = parse.getErFunc((f, s) => (f.items.size, s.toUpperCase))
好吧,可能不会,因为实际上我需要Jackson解析同一数据模型的YAML/XML/JSON表示。但无论如何,从您的示例来看,我看不到一种基于switch
创建项
自定义转换的方法。我在Github上也没有看到类似的示例。您能给我指出正确的方向吗打开?我编辑了我的答案。你还可以看到Spray Doc和Scala Future。这不仅仅是一个问题。所以,基本上,你建议在解析后只转换整个树?如果是这样,那么使用哪个解析引擎并不重要——这是绝对可能的,但我认为在实践中,这比“丑陋”要复杂得多带有全局变量的解决方案(我甚至不敢在这里想到类似于漂亮的错误消息之类的UX)。
res: String =
{
"name":"Alex",
"items":
[
{"t":"Moran"},
{"t":"Sem"}
]
}
parse: NamedList[Item] = NamedList(Alex, List(Item(Moran), Item(Sem)))
implicit class Switcher[A <: Item](data: NamedList[A]){
def getEr = data.name match{
case "Jone" => ("It`s Jone", data)
case "Alex" => ("It`s Alex", data)
}
def getErFunc[T](func : (NamedList[A], String) => T) =
data.name match{
case "Jone" => ("It`s Jone", func(data , "Param"))
case "Alex" => ("It`s Alex", func(data, "Not Param"))
}
}
val res2 = parse.getEr
val res3 = parse.getErFunc((f, s) => (f.items.size, s.toUpperCase))
res2: (String, NamedList[Item]) =
(It`s Alex,NamedList(Alex,List(Item(Moran), Item(Sem))))
res3: (String, (Int, String)) = (It`s Alex,(2,NOT PARAM))