无法反序列化元组
Cannot deserialize a tuple
如果我执行以下操作:
import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization.{read, write}
implicit val formats = DefaultFormats
val tuple = (5.0, 5.0)
val json = write(tuple)
println("Write: " + json)
println("Read: " + read[(Double, Double)](json))
我得到以下输出:
Write: {"_1$mcD$sp":5.0,"_2$mcD$sp":5.0}
Exception in thread "main" org.json4s.package$MappingException: No usable value for _1
Did not find value which can be converted into double
at org.json4s.reflect.package$.fail(package.scala:95)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:548)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun.applyOrElse(Extraction.scala:572)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun.applyOrElse(Extraction.scala:570)
at scala.PartialFunction.$anonfun$runWith$adapted(PartialFunction.scala:145)
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
at scala.collection.TraversableLike.collect(TraversableLike.scala:406)
at scala.collection.TraversableLike.collect$(TraversableLike.scala:404)
at scala.collection.AbstractTraversable.collect(Traversable.scala:108)
at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:570)
at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:630)
at org.json4s.Extraction$.$anonfun$extract(Extraction.scala:416)
at org.json4s.Extraction$.$anonfun$customOrElse(Extraction.scala:637)
at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)
at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)
at scala.PartialFunction$$anon.applyOrElse(PartialFunction.scala:257)
at org.json4s.Extraction$.customOrElse(Extraction.scala:637)
at org.json4s.Extraction$.extract(Extraction.scala:408)
at org.json4s.Extraction$.extract(Extraction.scala:40)
at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
at org.json4s.jackson.Serialization$.read(Serialization.scala:50)
at org.json4s.Serialization.read(Serialization.scala:25)
at org.json4s.Serialization.read$(Serialization.scala:25)
at org.json4s.jackson.Serialization$.read(Serialization.scala:17)
我在依赖项中包含了以下内容:
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.11.3",
"org.json4s" %% "json4s-jackson" % "3.6.10",
"org.json4s" %% "json4s-ext" % "3.6.10",
这是功能还是错误?
我在 Jackson 的支持下尝试过,它成功了。 Json4s 是 3.6.8
我认为这是功能,因为您正试图创建一个无效的 JSON。
你看,你想序列化两个double的元组,但是你写完之后想得到什么?似乎有些 JSON 像:{ 5.0 : 5.0 }
是无效的,key 不能是 double
。
如果您将在 String
上替换元组中的第一个类型,它会正常工作:
val tuple: (String, Double) = ("5.0", 5.0)
val json = write(tuple)
println("Write: " + json) // Write: {"5.0":5.0} - this is valid JSON
println("Read: " + read[(String, Double)](json))
// Read: (5.0,5.0) // and type will be (String, Double)
因此,请注意 key 类型 JSON 和无效情况。
这里的 Json4s 中有一件事让我感到困惑 - 错误消息,这真的很糟糕。
如果我执行以下操作:
import org.json4s.DefaultFormats
import org.json4s.jackson.Serialization.{read, write}
implicit val formats = DefaultFormats
val tuple = (5.0, 5.0)
val json = write(tuple)
println("Write: " + json)
println("Read: " + read[(Double, Double)](json))
我得到以下输出:
Write: {"_1$mcD$sp":5.0,"_2$mcD$sp":5.0}
Exception in thread "main" org.json4s.package$MappingException: No usable value for _1
Did not find value which can be converted into double
at org.json4s.reflect.package$.fail(package.scala:95)
at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:548)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun.applyOrElse(Extraction.scala:572)
at org.json4s.Extraction$ClassInstanceBuilder$$anonfun.applyOrElse(Extraction.scala:570)
at scala.PartialFunction.$anonfun$runWith$adapted(PartialFunction.scala:145)
at scala.collection.mutable.ResizableArray.foreach(ResizableArray.scala:62)
at scala.collection.mutable.ResizableArray.foreach$(ResizableArray.scala:55)
at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:49)
at scala.collection.TraversableLike.collect(TraversableLike.scala:406)
at scala.collection.TraversableLike.collect$(TraversableLike.scala:404)
at scala.collection.AbstractTraversable.collect(Traversable.scala:108)
at org.json4s.Extraction$ClassInstanceBuilder.instantiate(Extraction.scala:570)
at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:630)
at org.json4s.Extraction$.$anonfun$extract(Extraction.scala:416)
at org.json4s.Extraction$.$anonfun$customOrElse(Extraction.scala:637)
at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)
at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)
at scala.PartialFunction$$anon.applyOrElse(PartialFunction.scala:257)
at org.json4s.Extraction$.customOrElse(Extraction.scala:637)
at org.json4s.Extraction$.extract(Extraction.scala:408)
at org.json4s.Extraction$.extract(Extraction.scala:40)
at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
at org.json4s.jackson.Serialization$.read(Serialization.scala:50)
at org.json4s.Serialization.read(Serialization.scala:25)
at org.json4s.Serialization.read$(Serialization.scala:25)
at org.json4s.jackson.Serialization$.read(Serialization.scala:17)
我在依赖项中包含了以下内容:
"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.11.3",
"org.json4s" %% "json4s-jackson" % "3.6.10",
"org.json4s" %% "json4s-ext" % "3.6.10",
这是功能还是错误?
我在 Jackson 的支持下尝试过,它成功了。 Json4s 是 3.6.8
我认为这是功能,因为您正试图创建一个无效的 JSON。
你看,你想序列化两个double的元组,但是你写完之后想得到什么?似乎有些 JSON 像:{ 5.0 : 5.0 }
是无效的,key 不能是 double
。
如果您将在 String
上替换元组中的第一个类型,它会正常工作:
val tuple: (String, Double) = ("5.0", 5.0)
val json = write(tuple)
println("Write: " + json) // Write: {"5.0":5.0} - this is valid JSON
println("Read: " + read[(String, Double)](json))
// Read: (5.0,5.0) // and type will be (String, Double)
因此,请注意 key 类型 JSON 和无效情况。
这里的 Json4s 中有一件事让我感到困惑 - 错误消息,这真的很糟糕。