2
votes

Jerkson started throwing a really strange error that I haven't seen before.

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class scala.runtime.BoxedUnit and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: scala.collection.MapWrapper["data"])

I'm parsing some basic data from an API. The class I've defined is:

case class Segmentation(
  @(JsonProperty@field)("legend_size")
  val legend_size: Int,

  @(JsonProperty@field)("data")
  val data: Data

) 

and Data looks like:

case class Data(
  @(JsonProperty@field)("series")
  val series: List[String],

  @(JsonProperty@field)("values")
  val values: Map[String, Map[String, Any]]

)

Any clue why this would be triggering errors? Seems like a simple class that Jerkson can handle.

Edit: sample data:

{"legend_size": 1, "data": {"series": ["2013-04-06", "2013-04-07", "2013-04-08", "2013-04-09", "2013-04-10", "2013-04-11", "2013-04-12", "2013-04-13", "2013-04-14", "2013-04-15"], "values": {"datapoint": {"2013-04-12": 0, "2013-04-15": 4, "2013-04-14": 0, "2013-04-08":
0, "2013-04-09": 0, "2013-04-11": 0, "2013-04-10": 0, "2013-04-13": 0, "2013-04-06": 0, "2013-04-07": 0}}}}
3
I'm having the same problem with serializing MapWrapper. Looking at some other java (non-scala) threads on this issue, I'm wondering if its because MapWrapper has a getter but not a setter and that since it doesn't have both Jackson doesn't think its fully serializable stackoverflow.com/questions/8367312/…Andrew Norman
Curious if you've tried json4s yet? Jerkson is now pretty much retired.crockpotveggies

3 Answers

3
votes

this isn't the answer to the above example, but I'm going to offer it because it was the answer to my similar "BoxedUnit" scenario:

No serializer found for class scala.runtime.BoxedUnit and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

In my case Jackson was complaining about deserializing an instance of a scala.runtime.BoxedUnit object.

Q: So what is scala.runtime.BoxedUnit?

A: It's scala java representation for "Unit". The core part of Jackson (which is java code) is attempting to deserialize a java representation of the scala Unit non-entity.

Q: So why was this happening?

A: In my case this was a downstream side effect caused by a buggy method with an undeclared return value. The method in question wrapped a match clause that (unintentionally) didn't return a value for each case. Because of the buggy code described above, Scala dynamically declared the var capturing the result of this method as "Unit". Later on in the code when this var gets serialized into json, the jackson error occurs.

So if you are getting an issue like this, my advice would be to examine any implicitly typed vars / methods with non-defined return values and ensure they are doing what you think they are doing.

0
votes

I had the same exception. what caused it in my case is that I defined an apply method in the companion object without '=':

object Somthing {
   def apply(s: SomthingElse)  {
      ...
   }
}

instead of

object Somthing {
   def apply(s: SomthingElse) = {
      ...
   }
}

That caused the apply method return type to be Unit which caused the exception when I passed the object to jackson. Not sure if that is the case in your code or if this question is still relevant but this might help others with this kind of problem.

0
votes

It's been a while since I first posted this question. The solution as of writing this answer appears to be moving on from Jerkson and using the Jackson-module-scala or Json4s with the Jackson backend. Many Scala types are included in the default serialized and are natively handled.

In addition, the reason why I'm seeing BoxedUnit is because the explicit type Jerkson was seeing was Any (a part of Map[String, Map[String, Any]]). Any is a base type and doesn't give Jerkson/Jackson information about what it's deserializing. Therefore, it complains about a missing serializer.