我的Runner
代码看起来像
import java.io.File
import org.json4s._
import org.json4s.native.JsonMethods._
object WorkOrderParser {
implicit val formats = DefaultFormats
def get(workOrderJson: File): List[WorkOrderItem] = {
parse(workOrderJson).extract[List[WorkOrderItem]]
}
def main(args: Array[String]) {
println(WorkOrderParser.get(new File("src/main/resources/workConfigSample.json")))
}
}
我的案例类看起来像
import java.io.File
sealed trait FilePostProcessingAction
case object delete extends FilePostProcessingAction
case object move extends FilePostProcessingAction
case class WorkOrderItem(configName: String,
logSource: File,
logType: String,
afterProcessingFileAction: FilePostProcessingAction,
recursiveFind: Option[Boolean] = Some(false),
processZipFiles: Option[Boolean] = Some(false))
我的示例JSON看起来像
[
{
"configName": "bluecoat",
"logSource": "/root/fw1/logs/bc",
"logType": "bluecoat",
"recursiveFind": true,
"processZipFiles": false,
"afterProcessingFileAction": "delete"
},
{
"configName": "mcAfee",
"logSource": "/root/fw1/logs/mcafee",
"logType": "mcafee",
"recursiveFind": true,
"processZipFiles": true,
"afterProcessingFileAction": "delete"
}
]
当我运行时,我得到了
Exception in thread "main" org.json4s.package$MappingException: No usable value for logSource
Parsed JSON values do not match with class constructor
args=
arg types=
constructor=org.json4s.reflect.Executable@1fa268de
at org.json4s.reflect.package$.fail(package.scala:93)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:509)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$14.apply(Extraction.scala:529)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$14.apply(Extraction.scala:529)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:245)
at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
at scala.collection.TraversableLike$class.map(TraversableLike.scala:245)
at scala.collection.AbstractTraversable.map(Traversable.scala:104)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$instantiate(Extraction.scala:517)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:564)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:559)
at org.json4s.Extraction$.org$json4s$Extraction$$customOrElse(Extraction.scala:573)
at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:559)
at org.json4s.Extraction$.extract(Extraction.scala:394)
at org.json4s.Extraction$CollectionBuilder$$anonfun$6.apply(Extraction.scala:403)
at org.json4s.Extraction$CollectionBuilder$$anonfun$6.apply(Extraction.scala:403)
at scala.collection.immutable.List.map(List.scala:273)
at org.json4s.Extraction$CollectionBuilder.mkCollection(Extraction.scala:403)
at org.json4s.Extraction$CollectionBuilder.result(Extraction.scala:423)
at org.json4s.Extraction$.extract(Extraction.scala:377)
at org.json4s.Extraction$.extract(Extraction.scala:43)
at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
at com.logprocessor.processor.workOrder.WorkOrderParser$.get(WorkOrderParser.scala:11)
at com.logprocessor.processor.workOrder.WorkOrderParser$.main(WorkOrderParser.scala:15)
at com.logprocessor.processor.workOrder.WorkOrderParser.main(WorkOrderParser.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.json4s.package$MappingException: Parsed JSON values do not match with class constructor
args=
arg types=
constructor=org.json4s.reflect.Executable@1fa268de
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$instantiate(Extraction.scala:542)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:564)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$6.apply(Extraction.scala:559)
at org.json4s.Extraction$.org$json4s$Extraction$$customOrElse(Extraction.scala:573)
at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:559)
at org.json4s.Extraction$.extract(Extraction.scala:394)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:497)
... 30 more
但是,当我将我的案例类中的类型更改为String
(logSource
和afterProcessingFileAction
)时,它可以正常工作
我做错了什么? 我正在使用https://github.com/json4s/json4s来实现此目标
答案 0 :(得分:0)
也许几年为时已晚,但是我看到2k的视图,所以让我们回答一下:)
这是因为json4s正在寻找如何对List [WorkOrderItem]进行反序列化,并且找不到与extract [T]相匹配的构造函数,在您的情况下,它是extract [List [WorkOrderItem]]。
我相信您需要创建一个自定义解串器并将其注册为受支持的格式之一。注意:可能还有其他方法,例如使用提示或其他功能,但是我昨天才开始使用json4s,所以我有点菜鸟。
例如-
case class DynamicStringList(values: List[String]) { def show() { values.foreach(println) } }
case object DynamicStringListSerializer extends CustomSerializer[DynamicStringList] (format => ({ case JString(stringValue) => DynamicStringList(List(stringValue)) case JArray(stringValues) => { implicit val formats = DefaultFormats def jsonJValueToString(jsonString: JValue)(implicit formats: Formats) : String = { jsonString.extract[String] } val dynamicList = stringValues.map(jsonJValueToString).toList DynamicStringList(dynamicList) //DynamicStringArray.toArray(stringValues) } }, { // TODO - This is the serialize function to JArray, however we will probably never use it so not done for now case dynamicList: DynamicStringList => JNothing } ))
import org.json4s.DefaultFormats import com.myserializers.DynamicStringListSerializer object MyJSONUtils { implicit val formats = DefaultFormats + DynamicStringListSerializer
parse(json_message_as_string).extract[DynamicStringList]