|
| 1 | +package com.fasterxml.jackson.module.scala.deser |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonParser |
| 4 | +import com.fasterxml.jackson.databind.`type`.MapLikeType |
| 5 | +import com.fasterxml.jackson.databind.deser.{ContextualDeserializer, Deserializers} |
| 6 | +import com.fasterxml.jackson.databind.deser.std.{ContainerDeserializerBase, MapDeserializer, StdValueInstantiator} |
| 7 | +import com.fasterxml.jackson.databind.jsontype.TypeDeserializer |
| 8 | +import com.fasterxml.jackson.databind._ |
| 9 | +import com.fasterxml.jackson.module.scala.{DefaultScalaModule, IteratorModule} |
| 10 | + |
| 11 | +import java.util |
| 12 | +import scala.collection.{immutable, mutable} |
| 13 | +import scala.collection.JavaConverters._ |
| 14 | + |
| 15 | +/** |
| 16 | + * Adds support for deserializing Scala [[scala.collection.immutable.LongMap]]s and [[scala.collection.mutable.LongMap]]s. |
| 17 | + * Scala LongMaps can already be serialized using [[IteratorModule]] or [[DefaultScalaModule]]. |
| 18 | + * |
| 19 | + * @since 2.14.0 |
| 20 | + */ |
| 21 | +private[deser] object LongMapDeserializerResolver extends Deserializers.Base { |
| 22 | + |
| 23 | + private val immutableLongMapClass = classOf[immutable.LongMap[_]] |
| 24 | + private val mutableLongMapClass = classOf[mutable.LongMap[_]] |
| 25 | + |
| 26 | + override def findMapLikeDeserializer(theType: MapLikeType, |
| 27 | + config: DeserializationConfig, |
| 28 | + beanDesc: BeanDescription, |
| 29 | + keyDeserializer: KeyDeserializer, |
| 30 | + elementTypeDeserializer: TypeDeserializer, |
| 31 | + elementDeserializer: JsonDeserializer[_]): JsonDeserializer[_] = { |
| 32 | + if (immutableLongMapClass.isAssignableFrom(theType.getRawClass)) { |
| 33 | + val mapDeserializer = new MapDeserializer(theType, new ImmutableLongMapInstantiator(config, theType), keyDeserializer, |
| 34 | + elementDeserializer.asInstanceOf[JsonDeserializer[AnyRef]], elementTypeDeserializer) |
| 35 | + new ImmutableLongMapDeserializer(theType, mapDeserializer) |
| 36 | + } else if (mutableLongMapClass.isAssignableFrom(theType.getRawClass)) { |
| 37 | + val mapDeserializer = new MapDeserializer(theType, new MutableLongMapInstantiator(config, theType), keyDeserializer, |
| 38 | + elementDeserializer.asInstanceOf[JsonDeserializer[AnyRef]], elementTypeDeserializer) |
| 39 | + new MutableLongMapDeserializer(theType, mapDeserializer) |
| 40 | + } else { |
| 41 | + None.orNull |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private class ImmutableLongMapDeserializer[V](mapType: MapLikeType, containerDeserializer: MapDeserializer) |
| 46 | + extends ContainerDeserializerBase[immutable.LongMap[V]](mapType) with ContextualDeserializer { |
| 47 | + |
| 48 | + override def getContentType: JavaType = containerDeserializer.getContentType |
| 49 | + |
| 50 | + override def getContentDeserializer: JsonDeserializer[AnyRef] = containerDeserializer.getContentDeserializer |
| 51 | + |
| 52 | + override def createContextual(ctxt: DeserializationContext, property: BeanProperty): JsonDeserializer[_] = { |
| 53 | + val newDelegate = containerDeserializer.createContextual(ctxt, property).asInstanceOf[MapDeserializer] |
| 54 | + new ImmutableLongMapDeserializer(mapType, newDelegate) |
| 55 | + } |
| 56 | + |
| 57 | + override def deserialize(jp: JsonParser, ctxt: DeserializationContext): immutable.LongMap[V] = { |
| 58 | + containerDeserializer.deserialize(jp, ctxt) match { |
| 59 | + case wrapper: ImmutableMapWrapper => wrapper.asLongMap() |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + override def deserialize(jp: JsonParser, ctxt: DeserializationContext, intoValue: immutable.LongMap[V]): immutable.LongMap[V] = { |
| 64 | + val newMap = deserialize(jp, ctxt) |
| 65 | + if (newMap.isEmpty) { |
| 66 | + intoValue |
| 67 | + } else { |
| 68 | + intoValue ++ newMap |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + override def getEmptyValue(ctxt: DeserializationContext): Object = immutable.LongMap.empty[V] |
| 73 | + } |
| 74 | + |
| 75 | + private class MutableLongMapDeserializer[V](mapType: MapLikeType, containerDeserializer: MapDeserializer) |
| 76 | + extends ContainerDeserializerBase[mutable.LongMap[V]](mapType) with ContextualDeserializer { |
| 77 | + |
| 78 | + override def getContentType: JavaType = containerDeserializer.getContentType |
| 79 | + |
| 80 | + override def getContentDeserializer: JsonDeserializer[AnyRef] = containerDeserializer.getContentDeserializer |
| 81 | + |
| 82 | + override def createContextual(ctxt: DeserializationContext, property: BeanProperty): JsonDeserializer[_] = { |
| 83 | + val newDelegate = containerDeserializer.createContextual(ctxt, property).asInstanceOf[MapDeserializer] |
| 84 | + new MutableLongMapDeserializer(mapType, newDelegate) |
| 85 | + } |
| 86 | + |
| 87 | + override def deserialize(jp: JsonParser, ctxt: DeserializationContext): mutable.LongMap[V] = { |
| 88 | + containerDeserializer.deserialize(jp, ctxt) match { |
| 89 | + case wrapper: MutableMapWrapper => wrapper.asLongMap() |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + override def deserialize(jp: JsonParser, ctxt: DeserializationContext, intoValue: mutable.LongMap[V]): mutable.LongMap[V] = { |
| 94 | + val newMap = deserialize(jp, ctxt) |
| 95 | + if (newMap.isEmpty) { |
| 96 | + intoValue |
| 97 | + } else { |
| 98 | + intoValue ++ newMap |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + override def getEmptyValue(ctxt: DeserializationContext): Object = mutable.LongMap.empty[V] |
| 103 | + } |
| 104 | + |
| 105 | + private class ImmutableLongMapInstantiator(config: DeserializationConfig, mapType: MapLikeType) extends StdValueInstantiator(config, mapType) { |
| 106 | + override def canCreateUsingDefault = true |
| 107 | + override def createUsingDefault(ctxt: DeserializationContext) = new ImmutableMapWrapper |
| 108 | + } |
| 109 | + |
| 110 | + private class MutableLongMapInstantiator(config: DeserializationConfig, mapType: MapLikeType) extends StdValueInstantiator(config, mapType) { |
| 111 | + override def canCreateUsingDefault = true |
| 112 | + |
| 113 | + override def createUsingDefault(ctxt: DeserializationContext) = new MutableMapWrapper |
| 114 | + } |
| 115 | + |
| 116 | + private class ImmutableMapWrapper extends util.AbstractMap[Object, Object] { |
| 117 | + var baseMap = immutable.LongMap[Object]() |
| 118 | + |
| 119 | + override def put(k: Object, v: Object): Object = { |
| 120 | + k match { |
| 121 | + case n: Number => baseMap += (n.longValue() -> v) |
| 122 | + case s: String => baseMap += (s.toLong -> v) |
| 123 | + case _ => { |
| 124 | + val typeName = Option(k) match { |
| 125 | + case Some(n) => n.getClass.getName |
| 126 | + case _ => "null" |
| 127 | + } |
| 128 | + throw new IllegalArgumentException(s"IntMap does npt support keys of type $typeName") |
| 129 | + } |
| 130 | + } |
| 131 | + v |
| 132 | + } |
| 133 | + |
| 134 | + // Used by the deserializer when using readerForUpdating |
| 135 | + override def get(key: Object): Object = key match { |
| 136 | + case n: Number => baseMap.get(n.longValue()).orNull |
| 137 | + case s: String => baseMap.get(s.toInt).orNull |
| 138 | + case _ => None.orNull |
| 139 | + } |
| 140 | + |
| 141 | + // Isn't used by the deserializer |
| 142 | + override def entrySet(): java.util.Set[java.util.Map.Entry[Object, Object]] = |
| 143 | + baseMap.asJava.entrySet().asInstanceOf[java.util.Set[java.util.Map.Entry[Object, Object]]] |
| 144 | + |
| 145 | + def asLongMap[V](): immutable.LongMap[V] = baseMap.asInstanceOf[immutable.LongMap[V]] |
| 146 | + } |
| 147 | + |
| 148 | + private class MutableMapWrapper extends util.AbstractMap[Object, Object] { |
| 149 | + var baseMap = mutable.LongMap[Object]() |
| 150 | + |
| 151 | + override def put(k: Object, v: Object): Object = { |
| 152 | + k match { |
| 153 | + case n: Number => baseMap += (n.longValue() -> v) |
| 154 | + case s: String => baseMap += (s.toLong -> v) |
| 155 | + case _ => { |
| 156 | + val typeName = Option(k) match { |
| 157 | + case Some(n) => n.getClass.getName |
| 158 | + case _ => "null" |
| 159 | + } |
| 160 | + throw new IllegalArgumentException(s"IntMap does npt support keys of type $typeName") |
| 161 | + } |
| 162 | + } |
| 163 | + v |
| 164 | + } |
| 165 | + |
| 166 | + // Used by the deserializer when using readerForUpdating |
| 167 | + override def get(key: Object): Object = key match { |
| 168 | + case n: Number => baseMap.get(n.longValue()).orNull |
| 169 | + case s: String => baseMap.get(s.toInt).orNull |
| 170 | + case _ => None.orNull |
| 171 | + } |
| 172 | + |
| 173 | + // Isn't used by the deserializer |
| 174 | + override def entrySet(): java.util.Set[java.util.Map.Entry[Object, Object]] = |
| 175 | + baseMap.asJava.entrySet().asInstanceOf[java.util.Set[java.util.Map.Entry[Object, Object]]] |
| 176 | + |
| 177 | + def asLongMap[V](): mutable.LongMap[V] = baseMap.asInstanceOf[mutable.LongMap[V]] |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments