EzDevInfo.com

jackson-module-scala

Add-on module for Jackson (<a href="http://wiki.fasterxml.com/JacksonHome">http://wiki.fasterxml.com/JacksonHome</a>) to support Scala-specific datatypes JacksonModuleScala - FasterXML Wiki

jackson why do I need JsonTypeName annotation on subclasses

At this link

I'm trying to understand why do I (may) need @JsonTypeName on subclasses (like all 'internet; sujests to put) if it works without it ?

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "aType")
@JsonSubTypes(Array(
  new Type(value = classOf[ModelA], name = "ModelA"),
  new Type(value = classOf[ModelB], name = "ModelB")
))
class BaseModel(val modelName:String)

//@JsonTypeName("SomeModel")  // Commented. Do I need this?
class ModelA(val a:String, val b:String, val c:String, commonData:String)  extends BaseModel(commonData) {
  def this() = this("default", "default", "default" ,"default")
}
//@JsonTypeName("SomeModel") // Commented. Do I need this?
class ModelB(val a:String, val b:String, val c:String, commonData:String)  extends BaseModel(commonData) {
  def this() = this("default", "default", "default" ,"default")
}

Source: (StackOverflow)

CXF jackson post produces 500 error. No explanation in logs

CXF and Jackson simple web app:

 @POST
  @Consumes(Array(MediaType.APPLICATION_JSON))
  @Produces(Array(MediaType.APPLICATION_JSON))
  def addTicket(ticket: Ticket): Ticket = {
    val id = "2"
    ticketMap.put(id,ticket)
    ticket //
  }

Ticket get processed from json to object. But Produces seems fails.

ID: 1
Response-Code: 500
Content-Type: application/json
Headers: {Content-Type=[application/json], Date=[Thu, 08 Oct 2015 01:44:06 GMT]}

Q: How to get explanaton of 500 error in logs?

in cxf/spring config:

    <jaxrs:features>
          <bean class="org.apache.cxf.feature.LoggingFeature" />
    </jaxrs:features>

UPDATE: If I register outFaultInterceptors, then in:

val fault = new Fault(message.getContent(classOf[Exception]))

fault does not explain reason either.

but reason in @Produces(Array(MediaType.APPLICATION_JSON)) because if I chane to plain-text then it works- will return some text.


Source: (StackOverflow)

Advertisements

Jackson - serialize and deserialize Map as Seq[Tuple2]?

I'm trying to serialize maps with arbitrary key types (best effort) using jackson and jackson-module-scala. I did plenty of searching of course, I've found cases for custom key deserializers, but they won't do here - and I'm also trying to avoid writing a custom map (de)serializer, if possible.

My basic idea is to serialize map as an array of entries, entries being pairs (length 2 arrays) of objects - I know this is not quite map semantics, but I think this is the closest I can get without converting keys to strings and then parsing them back to objects on a per-type basis.

When learning about jackson's converters I was delighted, thinking I could get away with writing just a pair of them - since jackson-module-scala happily (de)serializes both Seqs and Tuples, I thought it could be this simple:

class MapToSeq extends StdConverter[Map[_,_], Seq[(_,_)]] {
  override def convert(value: Map[_,_]): Seq[(_,_)] = value.toSeq
}

class SeqToMap extends StdConverter[Seq[(_,_)], Map[_,_]] {
  override def convert(value: Seq[(_,_)]): Map[_,_] = value.toMap
}

All that was left is applying this as a global setting, I did that via a mixin (side question: is there a better way for this?)

@JsonSerialize(converter = classOf[MapToSeq])
@JsonDeserialize(converter = classOf[SeqToMap])
class Mixin
...
mapper.addMixin(classOf[Map[_,_]], classOf[Mixin])

The serialization side works nicely, the json output is exactly as described above, however on deserialization results in a weird phenomenon: it does not fail, but the insides of the map are of the wrong type. So, serializing a Map[MyType1,MyType2] works ok, but then deserializing the result I get something that's more like a Map[String, Any] (the value being a simple type or another map for the entries).

I suspect there is some type information lost in the process, so I also tried to specializing all the type paramteres in the code (replacing every , with MyType1,MyType2 in the above effectively) and interestingly it seemed to work ok! (obviously, this solution is not generic)

So my question is: is there a way to make this work (or any other way to achieve the same)? I feel like it's reasonable close but I can't seem to figure it out


Source: (StackOverflow)

NoSuchElementException trying to serialize my Enumeration object

I'm experiencing some problems serializing an enumeration object. It seems like the jackson tries to get the '$outer' of my superclass, but it's not there. I've got an enumeration GeneralErrorCode which is one implementation of a common interface ErrorCode. I compiled a small example my problem.

I created a sbt-project demonstrating the problem at https://www.dropbox.com/s/e9xwp2vkyt3v9f1/enumeration-jackson-problem.tar.bz2?dl=0

In that example this code is used to raise the problem:

def getObjectMapper: ObjectMapper = {
    val mapper = new ObjectMapper
    mapper.configure(SerializationFeature.INDENT_OUTPUT, true)
    mapper.registerModule(DefaultScalaModule)
}

val provider = new JacksonJsonProvider
provider.setMapper(getObjectMapper)

val stream = new ByteArrayOutputStream()
provider.writeTo(GeneralErrorCodes.GEN001, classOf[ErrorCode], 
    null, Array(), MediaType.APPLICATION_JSON_TYPE, null, stream)

Error:

[info]   com.fasterxml.jackson.databind.JsonMappingException: None.get
[info]   at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:141)                                                                                                                       
[info]   at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1387)
[info]   at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:889)
[info]   at com.fasterxml.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:635)
[info]   at Test$$anonfun$1.apply$mcV$sp(Test.scala:34)
[info]   at Test$$anonfun$1.apply(Test.scala:32)
[info]   at Test$$anonfun$1.apply(Test.scala:32)
[info]   at org.scalatest.Transformer$$anonfun$apply$1.apply$mcV$sp(Transformer.scala:22)
[info]   at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info]   ...
[info]   Cause: java.util.NoSuchElementException: None.get
[info]   at scala.None$.get(Option.scala:347)
[info]   at scala.None$.get(Option.scala:345)
[info]   at com.fasterxml.jackson.module.scala.ser.EnumerationSerializer.serialize(EnumerationSerializerModule.scala:29)
[info]   at com.fasterxml.jackson.module.scala.ser.EnumerationSerializer.serialize(EnumerationSerializerModule.scala:27)
[info]   at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
[info]   at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1387)
[info]   at com.fasterxml.jackson.databind.ObjectWriter.writeValue(ObjectWriter.java:889)
[info]   at com.fasterxml.jackson.jaxrs.base.ProviderBase.writeTo(ProviderBase.java:635)
[info]   at Test$$anonfun$1.apply$mcV$sp(Test.scala:34)
[info]   at Test$$anonfun$1.apply(Test.scala:32)
[info]   ...

Source: (StackOverflow)