Cannot deserialize a tuple

泄露秘密 提交于 2021-02-10 05:23:54

问题


If I do the following:

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

I get the following output:

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$3.applyOrElse(Extraction.scala:572)
    at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$3.applyOrElse(Extraction.scala:570)
    at scala.PartialFunction.$anonfun$runWith$1$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$10(Extraction.scala:416)
    at org.json4s.Extraction$.$anonfun$customOrElse$1(Extraction.scala:637)
    at scala.PartialFunction.applyOrElse(PartialFunction.scala:127)
    at scala.PartialFunction.applyOrElse$(PartialFunction.scala:126)
    at scala.PartialFunction$$anon$1.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)

I have included the following in my dependencies:

    "com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.11.3",
    "org.json4s" %% "json4s-jackson" % "3.6.10",
    "org.json4s" %% "json4s-ext" % "3.6.10",

Is this a feature or a bug?


回答1:


I think this is feature because you are trying to create an invalid JSON. Take a look, you want to serialize tuple of two doubles, but what are you want to get after writing? Seems some JSON like: { 5.0 : 5.0 } which is invalid, key can't be a double. If you will replace your first type in tuple on String it works correct:

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)

So, be careful with key types in JSON and invalid cases.

One thing is confusing me in Json4s here - is error message, it's really terrible.




回答2:


I tried with Jackson support, it worked. Json4s is 3.6.8



来源:https://stackoverflow.com/questions/64914995/cannot-deserialize-a-tuple

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!