Skip to content

Commit 186a8a1

Browse files
authored
Merge pull request #313 from ProjectMapK/296
Added type match check to readValues
2 parents e86a449 + 3890b71 commit 186a8a1

File tree

2 files changed

+15
-10
lines changed
  • src

2 files changed

+15
-10
lines changed

src/main/kotlin/io/github/projectmapk/jackson/module/kogera/Extensions.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,19 @@ internal inline fun <reified T> Any?.checkTypeMismatch(): T {
8282
public inline fun <reified T> ObjectMapper.readValue(jp: JsonParser): T = readValue(jp, jacksonTypeRef<T>())
8383
.checkTypeMismatch()
8484

85-
// TODO: After importing 2.19, import the changes in kotlin-module and uncomment the tests.
86-
public inline fun <reified T> ObjectMapper.readValues(
87-
jp: JsonParser
88-
): MappingIterator<T> = readValues(jp, jacksonTypeRef<T>())
85+
/**
86+
* Shorthand for [ObjectMapper.readValues].
87+
* @throws RuntimeJsonMappingException Especially if [T] is non-null and the value read is null.
88+
* Other cases where the read value is of a different type than [T]
89+
* due to an incorrect customization to [ObjectMapper].
90+
*/
91+
public inline fun <reified T> ObjectMapper.readValues(jp: JsonParser): MappingIterator<T> {
92+
val values = readValues(jp, jacksonTypeRef<T>())
93+
94+
return object : MappingIterator<T>(values) {
95+
override fun nextValue(): T = super.nextValue().checkTypeMismatch()
96+
}
97+
}
8998

9099
/**
91100
* Shorthand for [ObjectMapper.readValue].

src/test/kotlin/io/github/projectmapk/jackson/module/kogera/zPorted/ReadValuesTest.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ class ReadValuesTest {
3838
val itr = mapper.readValues<String>(src)
3939

4040
assertEquals("foo", itr.next())
41-
// TODO: It is expected to be checked after importing 2.19.
42-
// assertThrows<RuntimeJsonMappingException> {
43-
assertDoesNotThrow {
41+
assertThrows<RuntimeJsonMappingException> {
4442
itr.next()
4543
}
4644
}
@@ -51,9 +49,7 @@ class ReadValuesTest {
5149
val itr = mapper.readValues<String>(src)
5250

5351
assertEquals("foo", itr.nextValue())
54-
// TODO: It is expected to be checked after importing 2.19.
55-
// assertThrows<RuntimeJsonMappingException> {
56-
assertDoesNotThrow {
52+
assertThrows<RuntimeJsonMappingException> {
5753
itr.nextValue()
5854
}
5955
}

0 commit comments

Comments
 (0)