Skip to content

Instantly share code, notes, and snippets.

@jam01
Last active November 13, 2019 18:45
Show Gist options
  • Save jam01/427e97f78ff2bb31676f3525e1e241a0 to your computer and use it in GitHub Desktop.
Save jam01/427e97f78ff2bb31676f3525e1e241a0 to your computer and use it in GitHub Desktop.
ObjectMapper that deserializes JSON to mutable Maps and Seqs while also maintaining order
object ATR extends AbstractTypeResolver {
override def findTypeMapping(config: DeserializationConfig, javaType: JavaType) = {
val result = Some(javaType) collect {
case mapLikeType: MapLikeType =>
if (javaType.getRawClass.equals(classOf[collection.Map[_, _]])) {
TypeFactory.defaultInstance().constructSpecializedType(javaType, classOf[mutable.LinkedHashMap[_, _]])
} else null
case colType: CollectionLikeType =>
if (javaType.getRawClass.equals(classOf[collection.Seq[_]])) {
TypeFactory.defaultInstance().constructSpecializedType(javaType, classOf[mutable.LinearSeq[_]])
} else null
}
result.orNull
}
}
object AtrModule extends JacksonModule {
this += (_ addAbstractTypeResolver ATR)
}
val mapper = new ObjectMapper() with ScalaObjectMapper
mapper.registerModule(DefaultScalaModule)
mapper.registerModule(AtrModule)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment