Skip to content

Commit

Permalink
Introduce kotlin #554 code.
Browse files Browse the repository at this point in the history
  • Loading branch information
k163377 committed Apr 15, 2023
1 parent 858542d commit 4fca25c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public inline fun <reified T> ObjectReader.readValuesTyped(jp: JsonParser): Iter
public inline fun <reified T> ObjectReader.treeToValue(n: TreeNode): T? =
readValue(this.treeAsTokens(n), jacksonTypeRef<T>())

public inline fun <reified T, reified U> ObjectMapper.addMixIn(): ObjectMapper =
this.addMixIn(T::class.java, U::class.java)
public inline fun <reified T, reified U> JsonMapper.Builder.addMixIn(): JsonMapper.Builder =
this.addMixIn(T::class.java, U::class.java)

public operator fun ArrayNode.plus(element: Boolean) { add(element) }
public operator fun ArrayNode.plus(element: Short) { add(element) }
public operator fun ArrayNode.plus(element: Int) { add(element) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.fasterxml.jackson.module.kotlin._ported.test

import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.module.kotlin.addMixIn
import com.fasterxml.jackson.module.kotlin.contains
import com.fasterxml.jackson.module.kotlin.convertValue
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.jsonMapper
import com.fasterxml.jackson.module.kotlin.minusAssign
import com.fasterxml.jackson.module.kotlin.plusAssign
import com.fasterxml.jackson.module.kotlin.readValue
Expand Down Expand Up @@ -82,4 +86,17 @@ private class TestExtensionMethods {
val convertValueResult: List<Person> = mapper.convertValue(tree)
assertEquals(listOf(Person("Neo")), convertValueResult)
}

@Test fun mixInExtensionTest() {
data class Person(val name: String)
abstract class PersonMixIn {
@JsonIgnore
var name: String = ""
}

val mapper: JsonMapper = jsonMapper { addMixIn<Person, PersonMixIn>() }
val serializedPerson: String = mapper.writeValueAsString(Person("test"))

assertEquals("{}", serializedPerson)
}
}

0 comments on commit 4fca25c

Please sign in to comment.