-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
918ffaf
commit 5cd381d
Showing
9 changed files
with
232 additions
and
20 deletions.
There are no files selected for viewing
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
4 changes: 2 additions & 2 deletions
4
src/main/scala/com/fasterxml/jackson/module/scala/SetModule.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package com.fasterxml.jackson.module.scala | ||
|
||
import com.fasterxml.jackson.module.scala.deser.UnsortedSetDeserializerModule | ||
import com.fasterxml.jackson.module.scala.deser.{SortedSetDeserializerModule, UnsortedSetDeserializerModule} | ||
|
||
trait SetModule extends UnsortedSetDeserializerModule { | ||
trait SetModule extends UnsortedSetDeserializerModule with SortedSetDeserializerModule { | ||
|
||
} |
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
88 changes: 88 additions & 0 deletions
88
src/main/scala/com/fasterxml/jackson/module/scala/deser/SortedSetDeserializerModule.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.fasterxml.jackson.module.scala.deser | ||
|
||
import com.fasterxml.jackson.module.scala.modifiers.SetTypeModifierModule | ||
import com.fasterxml.jackson.databind.deser.{ValueInstantiator, ContextualDeserializer, Deserializers} | ||
import com.fasterxml.jackson.databind.deser.std.{StdValueInstantiator, CollectionDeserializer, ContainerDeserializerBase} | ||
import com.fasterxml.jackson.databind._ | ||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer | ||
import com.fasterxml.jackson.core.JsonParser | ||
import scala.collection.{immutable, SortedSet, mutable} | ||
import com.fasterxml.jackson.databind.`type`.CollectionLikeType | ||
import java.util.AbstractCollection | ||
import com.fasterxml.jackson.module.scala.introspect.OrderingLocator | ||
import java.lang.Object | ||
|
||
private class SortedSetBuilderWrapper[E](val builder: mutable.Builder[E, _ <: collection.SortedSet[E]]) extends AbstractCollection[E] { | ||
|
||
override def add(e: E) = { builder += e; true } | ||
|
||
// Required by AbstractCollection, but the deserializer doesn't care about them. | ||
def iterator() = null | ||
def size() = 0 | ||
} | ||
|
||
private object SortedSetDeserializer { | ||
def orderingFor = OrderingLocator.locate _ | ||
|
||
def builderFor(cls: Class[_], valueType: JavaType): mutable.Builder[AnyRef, SortedSet[AnyRef]] = | ||
if (classOf[mutable.TreeSet[_]].isAssignableFrom(cls)) mutable.TreeSet.newBuilder[AnyRef](orderingFor(valueType)) else | ||
if (classOf[mutable.SortedSet[_]].isAssignableFrom(cls)) mutable.SortedSet.newBuilder[AnyRef](orderingFor(valueType)) else | ||
if (classOf[immutable.TreeSet[_]].isAssignableFrom(cls)) immutable.TreeSet.newBuilder[AnyRef](orderingFor(valueType)) else | ||
immutable.SortedSet.newBuilder[AnyRef](orderingFor(valueType)) | ||
} | ||
|
||
private class SortedSetInstantiator(config: DeserializationConfig, collectionType: Class[_], valueType: JavaType) | ||
extends StdValueInstantiator(config, collectionType) { | ||
|
||
override def canCreateUsingDefault = true | ||
|
||
override def createUsingDefault(ctxt: DeserializationContext) = | ||
new SortedSetBuilderWrapper[AnyRef](SortedSetDeserializer.builderFor(collectionType, valueType)) | ||
} | ||
|
||
|
||
private class SortedSetDeserializer(collectionType: JavaType, containerDeserializer: CollectionDeserializer) | ||
extends ContainerDeserializerBase[collection.SortedSet[_]](collectionType) | ||
with ContextualDeserializer | ||
{ | ||
def this(collectionType: JavaType, valueDeser: JsonDeserializer[Object], valueTypeDeser: TypeDeserializer, valueInstantiator: ValueInstantiator) = | ||
this(collectionType, new CollectionDeserializer(collectionType, valueDeser, valueTypeDeser, valueInstantiator)) | ||
|
||
def createContextual(ctxt: DeserializationContext, property: BeanProperty) = { | ||
val newDelegate = containerDeserializer.createContextual(ctxt, property) | ||
new SortedSetDeserializer(collectionType, newDelegate) | ||
} | ||
|
||
override def getContentType = containerDeserializer.getContentType | ||
|
||
override def getContentDeserializer = containerDeserializer.getContentDeserializer | ||
|
||
override def deserialize(jp: JsonParser, ctxt: DeserializationContext): collection.SortedSet[_] = | ||
containerDeserializer.deserialize(jp, ctxt) match { | ||
case wrapper: SortedSetBuilderWrapper[_] => wrapper.builder.result() | ||
} | ||
} | ||
|
||
private object SortedSetDeserializerResolver extends Deserializers.Base { | ||
private final val SORTED_SET = classOf[collection.SortedSet[_]] | ||
|
||
override def findCollectionLikeDeserializer(collectionType: CollectionLikeType, | ||
config: DeserializationConfig, | ||
beanDesc: BeanDescription, | ||
elementTypeDeserializer: TypeDeserializer, | ||
elementDeserializer: JsonDeserializer[_]): JsonDeserializer[_] = { | ||
val rawClass = collectionType.getRawClass | ||
|
||
if (!SORTED_SET.isAssignableFrom(rawClass)) null | ||
else { | ||
val deser = elementDeserializer.asInstanceOf[JsonDeserializer[AnyRef]] | ||
val instantiator = new SortedSetInstantiator(config, rawClass, collectionType.containedType(0)) | ||
new SortedSetDeserializer(collectionType, deser, elementTypeDeserializer, instantiator) | ||
} | ||
} | ||
|
||
} | ||
|
||
trait SortedSetDeserializerModule extends SetTypeModifierModule { | ||
this += (_ addDeserializers SortedSetDeserializerResolver) | ||
} |
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
41 changes: 41 additions & 0 deletions
41
src/main/scala/com/fasterxml/jackson/module/scala/introspect/OrderingLocator.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.fasterxml.jackson.module.scala.introspect | ||
|
||
import com.fasterxml.jackson.databind.JavaType | ||
|
||
object OrderingLocator { | ||
val ORDERINGS = Map( | ||
classOf[Unit] -> Ordering.Unit, | ||
classOf[Boolean] -> Ordering.Boolean, | ||
classOf[Byte] -> Ordering.Byte, | ||
classOf[Char] -> Ordering.Char, | ||
classOf[Short] -> Ordering.Short, | ||
classOf[Int] -> Ordering.Int, | ||
classOf[Long] -> Ordering.Long, | ||
classOf[Float] -> Ordering.Float, | ||
classOf[Double] -> Ordering.Double, | ||
classOf[BigInt] -> Ordering.BigInt, | ||
classOf[BigDecimal] -> Ordering.BigDecimal, | ||
classOf[String] -> Ordering.String | ||
) | ||
|
||
def locate(javaType: JavaType): Ordering[AnyRef] = { | ||
def matches(other: Class[_]) = other.isAssignableFrom(javaType.getRawClass) | ||
val ordering = | ||
ORDERINGS.find(e => matches(e._1)).map(_._2).getOrElse { | ||
if (matches(classOf[Option[_]])) { | ||
val delegate = locate(javaType.containedType(0)) | ||
Ordering.Option(delegate) | ||
} | ||
else if (matches(classOf[Comparable[_]])) | ||
new Ordering[AnyRef] { | ||
def compare(x: AnyRef, y: AnyRef): Int = { | ||
x.asInstanceOf[Comparable[AnyRef]].compareTo(y) | ||
} | ||
} | ||
|
||
else throw new IllegalArgumentException("Unsupported value type: " + javaType.getRawClass.getCanonicalName) | ||
} | ||
|
||
ordering.asInstanceOf[Ordering[AnyRef]] | ||
} | ||
} |
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
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
78 changes: 78 additions & 0 deletions
78
src/test/scala/com/fasterxml/jackson/module/scala/deser/SortedSetDeserializerTest.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.fasterxml.jackson.module.scala.deser | ||
|
||
import org.junit.runner.RunWith | ||
import org.scalatest.junit.JUnitRunner | ||
import org.scalatest.matchers.ShouldMatchers | ||
import scala.collection.mutable | ||
import scala.collection.SortedSet | ||
import scala.collection.immutable.TreeSet | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize | ||
import com.fasterxml.jackson.core.`type`.TypeReference | ||
|
||
object SortedSetDeserializerTest | ||
{ | ||
case class LongSetHolder(@JsonDeserialize(contentAs = classOf[java.lang.Long]) value: SortedSet[Long]) | ||
|
||
case class ComparableBean(value: Long) extends Comparable[ComparableBean] { | ||
def compareTo(o: ComparableBean): Int = value compareTo o.value | ||
} | ||
} | ||
|
||
@RunWith(classOf[JUnitRunner]) | ||
class SortedSetDeserializerTest extends DeserializationFixture with ShouldMatchers { | ||
import SortedSetDeserializerTest._ | ||
|
||
behavior of "SortedSetDeserializer" | ||
|
||
it should "deserialize a list into a SortedSet" in { f => | ||
val result = f.readValue[SortedSet[String]](setJson) | ||
result should be === setScala | ||
} | ||
|
||
it should "deserialize a list into a mutable SortedSet" in { f => | ||
val result = f.readValue[mutable.SortedSet[String]](setJson) | ||
result should be === setScala | ||
} | ||
|
||
it should "deserialize a list into a TreeSet" in { f => | ||
val result = f.readValue[TreeSet[String]](setJson) | ||
result should be === setScala | ||
} | ||
|
||
it should "deserialize a list into a mutable TreeSet" in { f => | ||
val result = f.readValue[mutable.TreeSet[String]](setJson) | ||
result should be === setScala | ||
} | ||
|
||
it should "deserialize a list of Ints into a SortedSet" in { f => | ||
val result = f.readValue[SortedSet[Int]](intSetJson) | ||
result should be === intSetScala | ||
} | ||
|
||
it should "deserialize a list of Longs into a SortedSet" in { f => | ||
val result = f.readValue[LongSetHolder](longSetHolderJson) | ||
result should be === longSetHolderScala | ||
} | ||
|
||
it should "deserialize a list of Comparables into a SortedSet" in { f => | ||
val result = f.readValue[SortedSet[ComparableBean]](comparableSetJson) | ||
result should be === comparableSetScala | ||
} | ||
|
||
it should "deserialize a lit of Ints into a SortedSet of Options" in { f => | ||
// NB: This is `java.lang.Integer`, because of GH-104 | ||
val result = f.readValue[SortedSet[Option[Integer]]](intSetJson, new TypeReference[SortedSet[Option[Integer]]]{}) | ||
result should be === optionIntSetScala | ||
} | ||
|
||
val setJson = """[ "one", "two", "three" ]""" | ||
val setScala = SortedSet("one", "two", "three") | ||
val intSetJson = """[ 1, 2, 3 ]""" | ||
val intSetScala = SortedSet(3, 2, 1) | ||
val longSetHolderJson = """{"value":[1,2,3]}""" | ||
val longSetHolderScala = LongSetHolder(SortedSet(3,2,1)) | ||
val comparableSetJson = """[{"value": 3},{"value": 1},{"value": 2}]""" | ||
val comparableSetScala = SortedSet(ComparableBean(2), ComparableBean(3), ComparableBean(1)) | ||
val optionIntSetScala = SortedSet(Option(2), Option(3), Option(1)) | ||
|
||
} |