Last active
November 13, 2019 18:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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