Skip to content
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

treeToValue extensions function should not have type erasure #497

Merged
merged 3 commits into from
Sep 8, 2021
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
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Authors:

Contributors:

Fedor Bobin (Fuud@github)
* #496, #45: Fix treeToValue extension function should not have type erasure
(2.13)

Mikhael Sokolov (sokomishalov@github)
* #489: JsonNode, ArrayNode and ObjectNode extension functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ inline fun <reified T> ObjectMapper.readValue(src: Reader): T = readValue(src, j
inline fun <reified T> ObjectMapper.readValue(src: InputStream): T = readValue(src, jacksonTypeRef<T>())
inline fun <reified T> ObjectMapper.readValue(src: ByteArray): T = readValue(src, jacksonTypeRef<T>())

inline fun <reified T> ObjectMapper.treeToValue(n: TreeNode): T? = treeToValue(n, T::class.java)
inline fun <reified T> ObjectMapper.treeToValue(n: TreeNode): T = readValue(this.treeAsTokens(n), jacksonTypeRef<T>())
inline fun <reified T> ObjectMapper.convertValue(from: Any): T = convertValue(from, jacksonTypeRef<T>())

inline fun <reified T> ObjectReader.readValueTyped(jp: JsonParser): T = readValue(jp, jacksonTypeRef<T>())
inline fun <reified T> ObjectReader.readValuesTyped(jp: JsonParser): Iterator<T> = readValues(jp, jacksonTypeRef<T>())
inline fun <reified T> ObjectReader.treeToValue(n: TreeNode): T? = treeToValue(n, T::class.java)
inline fun <reified T> ObjectReader.treeToValue(n: TreeNode): T? = readValue(this.treeAsTokens(n), jacksonTypeRef<T>())

operator fun ArrayNode.plus(element: Boolean) = Unit.apply { add(element) }
operator fun ArrayNode.plus(element: Short) = Unit.apply { add(element) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,19 @@ class TestExtensionMethods {
(4 downTo 0).forEach { arrayNode -= it }
assertThat(arrayNode.size(), `is`(0))
}

@Test fun noTypeErasure(){
data class Person(val name: String)
val source = """[ { "name" : "Neo" } ]"""
val tree = mapper.readTree(source)

val readValueResult: List<Person> = mapper.readValue(source)
assertThat(readValueResult, `is`(listOf(Person("Neo"))))

val treeToValueResult: List<Person> = mapper.treeToValue(tree)
assertThat(treeToValueResult, `is`(listOf(Person("Neo"))))

val convertValueResult: List<Person> = mapper.convertValue(tree)
assertThat(convertValueResult, `is`(listOf(Person("Neo"))))
}
}