[英]Jackson Scala JSON serialize/deserialize Tuple
Below I'm just trying to a round-trip of a Tuple2 using jackson-module-scala. 下面,我只是尝试使用jackson-module-scala往返Tuple2。 Using Scala 2.10.4.
使用Scala 2.10.4。 Why doesn't it work?
为什么不起作用?
As can be seen, the serializer opts to encode the Tuple2 as a JSON array. 可以看出,序列化程序选择将Tuple2编码为JSON数组。 Why can't the deserializer decode the JSON array back into a Tuple2?
为什么解串器不能将JSON数组解码回Tuple2?
wget http://central.maven.org/maven2/com/fasterxml/jackson/module/jackson-module-scala_2.10/2.4.4/jackson-module-scala_2.10-2.4.4.jar
wget http://central.maven.org/maven2/com/google/guava/guava/15.0/guava-15.0.jar
wget http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.4.4/jackson-core-2.4.4.jar
wget http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-databind/2.4.4/jackson-databind-2.4.4.jar
wget http://central.maven.org/maven2/com/fasterxml/jackson/core/jackson-annotations/2.4.4/jackson-annotations-2.4.4.jar
scala -cp jackson-module-scala_2.10-2.4.4.jar:guava-15.0.jar:jackson-core-2.4.4.jar:jackson-databind-2.4.4.jar:jackson-annotations-2.4.4.jar
val mapper = new com.fasterxml.jackson.databind.ObjectMapper()
mapper.registerModule(com.fasterxml.jackson.module.scala.DefaultScalaModule)
val writer = new java.io.StringWriter()
mapper.writeValue(writer, (1,2))
mapper.readValue(writer.toString,classOf[Tuple2[Integer,Integer]])
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of scala.Tuple2 out of VALUE_NUMBER_INT token
at [Source: [1,2]; line: 1, column: 2]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:762)
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:758)
at com.fasterxml.jackson.module.scala.deser.TupleDeserializer.deserialize(TupleDeserializerModule.scala:61)
at com.fasterxml.jackson.module.scala.deser.TupleDeserializer.deserialize(TupleDeserializerModule.scala:15)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3066)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2161)
Looking at the DeserializerTest trait it seems as if you need to pass a TypeReference
to the readValue
method. 看一下DeserializerTest特性,似乎您需要将
TypeReference
传递给readValue
方法。 This should work: 这应该工作:
mapper.readValue[(Int,Int)](writer.toString, new TypeReference[(Int,Int)]{})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.