Skip to content

Commit d6fcc0d

Browse files
committed
Create SeqWithNumberDeserializerTest.scala
1 parent 3cf503d commit d6fcc0d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.fasterxml.jackson.module.scala.deser
2+
3+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
4+
import com.fasterxml.jackson.module.scala.DefaultScalaModule
5+
import com.fasterxml.jackson.module.scala.introspect.ScalaAnnotationIntrospector
6+
import org.scalatest.BeforeAndAfterEach
7+
8+
object SeqWithNumberDeserializerTest {
9+
case class AnnotatedSeqLong(@JsonDeserialize(contentAs = classOf[java.lang.Long]) longs: Seq[Long])
10+
case class AnnotatedSeqPrimitiveLong(@JsonDeserialize(contentAs = classOf[Long]) longs: Seq[Long])
11+
case class SeqLong(longs: Seq[Long])
12+
case class SeqJavaLong(longs: Seq[java.lang.Long])
13+
case class SeqBigInt(longs: Seq[BigInt])
14+
}
15+
16+
class SeqWithNumberDeserializerTest extends DeserializerTest with BeforeAndAfterEach {
17+
lazy val module: DefaultScalaModule.type = DefaultScalaModule
18+
import SeqWithNumberDeserializerTest._
19+
20+
private def sumSeqLong(v: Seq[Long]): Long = v.sum
21+
private def sumSeqJavaLong(v: Seq[java.lang.Long]): Long = v.map(_.toLong).sum
22+
private def sumSeqBigInt(v: Seq[BigInt]): Long = v.sum.toLong
23+
24+
override def afterEach(): Unit = {
25+
super.afterEach()
26+
ScalaAnnotationIntrospector.clearRegisteredReferencedTypes()
27+
}
28+
29+
"JacksonModuleScala" should "support AnnotatedSeqLong" in {
30+
val v1 = deserialize("""{"longs":[151,152,153]}""", classOf[AnnotatedSeqLong])
31+
v1 shouldBe AnnotatedSeqLong(Seq(151L, 152L, 153L))
32+
sumSeqLong(v1.longs) shouldBe 456L
33+
}
34+
35+
it should "support AnnotatedSeqPrimitiveLong" in {
36+
val v1 = deserialize("""{"longs":[151,152,153]}""", classOf[AnnotatedSeqPrimitiveLong])
37+
v1 shouldBe AnnotatedSeqPrimitiveLong(Seq(151L, 152L, 153L))
38+
sumSeqLong(v1.longs) shouldBe 456L
39+
}
40+
41+
it should "support SeqLong" in {
42+
ScalaAnnotationIntrospector.registerReferencedType(classOf[SeqLong], "longs", classOf[Long])
43+
val v1 = deserialize("""{"longs":[151,152,153]}""", classOf[SeqLong])
44+
v1 shouldBe SeqLong(Seq(151L, 152L, 153L))
45+
//this will next call will fail with a Scala unboxing exception unless you ScalaAnnotationIntrospector.registerReferencedType
46+
//or use one of the equivalent classes in SeqWithNumberDeserializerTest
47+
sumSeqLong(v1.longs) shouldBe 456L
48+
}
49+
50+
it should "support SeqJavaLong" in {
51+
val v1 = deserialize("""{"longs":[151,152,153]}""", classOf[SeqJavaLong])
52+
v1 shouldBe SeqJavaLong(Seq(151L, 152L, 153L))
53+
sumSeqJavaLong(v1.longs) shouldBe 456L
54+
}
55+
56+
it should "support SeqBigInt" in {
57+
val v1 = deserialize("""{"longs":[151,152,153]}""", classOf[SeqBigInt])
58+
v1 shouldBe SeqBigInt(Seq(151L, 152L, 153L))
59+
sumSeqBigInt(v1.longs) shouldBe 456L
60+
}
61+
}

0 commit comments

Comments
 (0)