score:-1

Accepted answer

my problem was a race condition due to not using chaining in my mapper configuration singleton.

my old code was more like this:

private var mapper: objectmapper with scalaobjectmapper = _

def getmapper: objectmapper with scalaobjectmapper = {
  if (mapper == null) {
    mapper = new objectmapper() with scalaobjectmapper
    mapper.registermodule(defaultscalamodule)
    mapper.disable(deserializationfeature.fail_on_unknown_properties)
  }
  mapper
}

as you can see, if one thread initializes the mapper, but hasn't yet disabled unknown properties failure, a second thread could return and use a mapper that hasn't had that flag set yet, which explains why i was seeing the error only some of the time.

the correct code uses chaining so that the mapper singleton is set with all of the configuration:

private var mapper: objectmapper = _

 def getmapper: objectmapper = {
   if (mapper == null) {
     mapper = new objectmapper()
       .registermodule(defaultscalamodule)
       .disable(deserializationfeature.fail_on_unknown_properties)
   }
   mapper
 }

(i also removed the experimental scalaobjectmapper mix-in.)

score:-1

try jsoniter-scala and you will enjoy how it can be handy, safely, and efficient to parse and serialize json these days with scala: https://github.com/plokhotnyuk/jsoniter-scala

one of it's crucial features is an ability to generate codecs in compile time and even print their sources. you will have no any runtime magic like reflection or byte code replacement that will affect your code.

score:0

i made one simple test like this:

case class testclass (counts: mutable.hashmap[string, long])

and i converted it like:

val objectmapper = new objectmapper() with scalaobjectmapper
objectmapper.registermodule(defaultscalamodule)

val r3 = objectmapper.readvalue("{\"counts\":{\"foo\":1,\"bar\":2}}", classof[testclass])

and apparently it works for me. maybe it's something about the version you're using of jackson, or scala. have you tried different versions of jackson for example?


Related Query

More Query from same tag