Skip to content

test StreamReadCapability.DUPLICATE_PROPERTIES (IntMap/LongMap) #607

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,18 @@ private[deser] object IntMapDeserializerResolver extends Deserializers.Base {

override def put(k: Object, v: Object): Object = {
k match {
case n: Number => baseMap += (n.intValue() -> v)
case s: String => baseMap += (s.toInt -> v)
case n: Number => {
val i = n.intValue()
val oldValue = baseMap.get(i)
baseMap += (i -> v)
oldValue.orNull
}
case s: String => {
val i = s.toInt
val oldValue = baseMap.get(i)
baseMap += (i -> v)
oldValue.orNull
}
case _ => {
val typeName = Option(k) match {
case Some(n) => n.getClass.getName
Expand All @@ -86,7 +96,6 @@ private[deser] object IntMapDeserializerResolver extends Deserializers.Base {
throw new IllegalArgumentException(s"IntMap does not support keys of type $typeName")
}
}
v
}

// Used by the deserializer when using readerForUpdating
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,18 @@ private[deser] object LongMapDeserializerResolver extends Deserializers.Base {

override def put(k: Object, v: Object): Object = {
k match {
case n: Number => baseMap += (n.longValue() -> v)
case s: String => baseMap += (s.toLong -> v)
case n: Number => {
val l = n.longValue()
val oldValue = baseMap.get(l)
baseMap += (l -> v)
oldValue.orNull
}
case s: String => {
val l = s.toLong
val oldValue = baseMap.get(l)
baseMap += (l -> v)
oldValue.orNull
}
case _ => {
val typeName = Option(k) match {
case Some(n) => n.getClass.getName
Expand All @@ -128,7 +138,6 @@ private[deser] object LongMapDeserializerResolver extends Deserializers.Base {
throw new IllegalArgumentException(s"LongMap does not support keys of type $typeName")
}
}
v
}

// Used by the deserializer when using readerForUpdating
Expand All @@ -150,8 +159,18 @@ private[deser] object LongMapDeserializerResolver extends Deserializers.Base {

override def put(k: Object, v: Object): Object = {
k match {
case n: Number => baseMap += (n.longValue() -> v)
case s: String => baseMap += (s.toLong -> v)
case n: Number => {
val l = n.longValue()
val oldValue = baseMap.get(l)
baseMap += (l -> v)
oldValue.orNull
}
case s: String => {
val l = s.toLong
val oldValue = baseMap.get(l)
baseMap += (l -> v)
oldValue.orNull
}
case _ => {
val typeName = Option(k) match {
case Some(n) => n.getClass.getName
Expand All @@ -160,7 +179,6 @@ private[deser] object LongMapDeserializerResolver extends Deserializers.Base {
throw new IllegalArgumentException(s"LongMap does not support keys of type $typeName")
}
}
v
}

// Used by the deserializer when using readerForUpdating
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.fasterxml.jackson.module.scala.deser

import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.module.scala.{ClassTagExtensions, DefaultScalaModule}
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.deser.IntMapDeserializerTest.{Event, IntMapWrapper}

import java.util.UUID
Expand Down Expand Up @@ -87,4 +87,41 @@ class IntMapDeserializerTest extends DeserializerTest {
read(1) shouldEqual "true"
read(2) shouldEqual Map("id" -> event.id.toString, "description" -> event.description)
}

it should "deserialize IntMap (Object values, duplicate keys - default mode)" in {
val mapper = newMapper
val json = """{"1": 123, "2": 123, "2": 123.456}"""
val read = mapper.readValue(json, classOf[IntMap[Any]])
read(1) shouldEqual 123
read(2) shouldEqual 123.456
}

it should "deserialize IntMap (Object values, duplicate keys - optional mode)" in {
val mapper = newMapper
val json = """{"1": 123, "2": 123, "2": 123.456}"""
val parser = new WithDupsParser(mapper.createParser(json))
val read = try {
mapper.readValue(parser, classOf[IntMap[Any]])
} finally {
parser.close()
}
read(1) shouldEqual 123
val expected = new java.util.ArrayList[Object]()
expected.add(java.lang.Integer.valueOf(123))
expected.add(java.lang.Double.valueOf(123.456))
read(2) shouldEqual expected
}

it should "deserialize IntMap (Double values, duplicate key mode is ignored)" in {
val mapper = newMapper
val json = """{"1": "123", "2": "123", "2": "123.456"}"""
val parser = new WithDupsParser(mapper.createParser(json))
val read = try {
mapper.readValue(parser, new TypeReference[IntMap[String]]{})
} finally {
parser.close()
}
read(1) shouldEqual "123"
read(2) shouldEqual "123.456"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package com.fasterxml.jackson.module.scala.deser

import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import com.fasterxml.jackson.module.scala.deser.IntMapDeserializerTest.Event

import java.util.UUID
import scala.collection.{immutable, mutable}

class LongMapDeserializerTest extends DeserializerTest {
Expand Down Expand Up @@ -45,6 +47,54 @@ class LongMapDeserializerTest extends DeserializerTest {
read(402) shouldBe true
}

it should "deserialize immutable LongMap (Object values)" in {
val event = Event(UUID.randomUUID(), "event1")
val map = mutable.LongMap(0L -> false, 1L -> "true", 2L -> event)
val mapper = newMapper
val json = mapper.writeValueAsString(map)
val read = mapper.readValue(json, classOf[immutable.LongMap[Any]])
read(0) shouldBe false
read(1) shouldEqual "true"
read(2) shouldEqual Map("id" -> event.id.toString, "description" -> event.description)
}

it should "deserialize immutable LongMap (Object values, duplicate keys - default mode)" in {
val mapper = newMapper
val json = """{"1": 123, "2": 123, "2": 123.456}"""
val read = mapper.readValue(json, classOf[immutable.LongMap[Any]])
read(1) shouldEqual 123
read(2) shouldEqual 123.456
}

it should "deserialize immutable LongMap (Object values, duplicate keys - optional mode)" in {
val mapper = newMapper
val json = """{"1": 123, "2": 123, "2": 123.456}"""
val parser = new WithDupsParser(mapper.createParser(json))
val read = try {
mapper.readValue(parser, classOf[immutable.LongMap[Any]])
} finally {
parser.close()
}
read(1) shouldEqual 123
val expected = new java.util.ArrayList[Object]()
expected.add(java.lang.Integer.valueOf(123))
expected.add(java.lang.Double.valueOf(123.456))
read(2) shouldEqual expected
}

it should "deserialize immutable LongMap (Double values, duplicate key mode is ignored)" in {
val mapper = newMapper
val json = """{"1": "123", "2": "123", "2": "123.456"}"""
val parser = new WithDupsParser(mapper.createParser(json))
val read = try {
mapper.readValue(parser, new TypeReference[immutable.LongMap[String]] {})
} finally {
parser.close()
}
read(1) shouldEqual "123"
read(2) shouldEqual "123.456"
}

it should "deserialize mutable LongMap" in {
val map = mutable.LongMap(1L -> "one", 2L -> "two")

Expand Down Expand Up @@ -80,4 +130,52 @@ class LongMapDeserializerTest extends DeserializerTest {
read(0) shouldBe false
read(402) shouldBe true
}

it should "deserialize mutable LongMap (Object values)" in {
val event = Event(UUID.randomUUID(), "event1")
val map = mutable.LongMap(0L -> false, 1L -> "true", 2L -> event)
val mapper = newMapper
val json = mapper.writeValueAsString(map)
val read = mapper.readValue(json, classOf[mutable.LongMap[Any]])
read(0) shouldBe false
read(1) shouldEqual "true"
read(2) shouldEqual Map("id" -> event.id.toString, "description" -> event.description)
}

it should "deserialize mutable LongMap (Object values, duplicate keys - default mode)" in {
val mapper = newMapper
val json = """{"1": 123, "2": 123, "2": 123.456}"""
val read = mapper.readValue(json, classOf[mutable.LongMap[Any]])
read(1) shouldEqual 123
read(2) shouldEqual 123.456
}

it should "deserialize mutable LongMap (Object values, duplicate keys - optional mode)" in {
val mapper = newMapper
val json = """{"1": 123, "2": 123, "2": 123.456}"""
val parser = new WithDupsParser(mapper.createParser(json))
val read = try {
mapper.readValue(parser, classOf[mutable.LongMap[Any]])
} finally {
parser.close()
}
read(1) shouldEqual 123
val expected = new java.util.ArrayList[Object]()
expected.add(java.lang.Integer.valueOf(123))
expected.add(java.lang.Double.valueOf(123.456))
read(2) shouldEqual expected
}

it should "deserialize mutable LongMap (Double values, duplicate key mode is ignored)" in {
val mapper = newMapper
val json = """{"1": "123", "2": "123", "2": "123.456"}"""
val parser = new WithDupsParser(mapper.createParser(json))
val read = try {
mapper.readValue(parser, new TypeReference[mutable.LongMap[String]] {})
} finally {
parser.close()
}
read(1) shouldEqual "123"
read(2) shouldEqual "123.456"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fasterxml.jackson.module.scala.deser

import com.fasterxml.jackson.core.{JsonParser, StreamReadCapability}
import com.fasterxml.jackson.core.util.{JacksonFeatureSet, JsonParserDelegate}

class WithDupsParser(p: JsonParser) extends JsonParserDelegate(p) {
override def getReadCapabilities: JacksonFeatureSet[StreamReadCapability] = {
super.getReadCapabilities.`with`(StreamReadCapability.DUPLICATE_PROPERTIES)
}
}