在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):FasterXML/jackson-module-kotlin开源软件地址(OpenSource Url):https://github.com/FasterXML/jackson-module-kotlin开源编程语言(OpenSource Language):Kotlin 95.2%开源软件介绍(OpenSource Introduction):OverviewModule that adds support for serialization/deserialization of Kotlin classes and data classes. Previously a default constructor must have existed on the Kotlin object for Jackson to deserialize into the object. With this module, single constructor classes can be used automatically, and those with secondary constructors or static factories are also supported. Status
Releases require that you have included Kotlin stdlib and reflect libraries already. Gradle:
Maven: <dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.13.2</version>
</dependency> UsageFor any Kotlin class or data class constructor, the JSON property names will be inferred from the parameters using Kotlin runtime type information. To use, just register the Kotlin module with your ObjectMapper instance: // With Jackson 2.12 and later
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
...
val mapper = jacksonObjectMapper()
// or
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
...
val mapper = ObjectMapper().registerKotlinModule()
// or
import com.fasterxml.jackson.module.kotlin.jsonMapper
import com.fasterxml.jackson.module.kotlin.kotlinModule
...
val mapper = jsonMapper {
addModule(kotlinModule())
} Jackson versions prior to 2.10–2.11import com.fasterxml.jackson.databind.json.JsonMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
...
val mapper = JsonMapper.builder().addModule(KotlinModule()).build() Jackson versions prior to 2.10import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinModule
...
val mapper = ObjectMapper().registerModule(KotlinModule()) A simple data class example: import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
data class MyStateObject(val name: String, val age: Int)
...
val mapper = jacksonObjectMapper()
val state = mapper.readValue<MyStateObject>(json)
// or
val state: MyStateObject = mapper.readValue(json)
// or
myMemberWithType = mapper.readValue(json) All inferred types for the extension functions carry in full generic information (reified generics).
Therefore, using Also, there are some convenient operator overloading extension functions for JsonNode inheritors. import com.fasterxml.jackson.databind.node.ArrayNode
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.module.kotlin.*
// ...
val objectNode: ObjectNode = JsonNodeFactory.instance.objectNode()
objectNode.put("foo1", "bar").put("foo2", "baz").put("foo3", "bax")
objectNode -= "foo1"
objectNode -= listOf("foo2")
println(objectNode.toString()) // {"foo3":"bax"}
// ...
val arrayNode: ArrayNode = JsonNodeFactory.instance.arrayNode()
arrayNode += "foo"
arrayNode += true
arrayNode += 1
arrayNode += 1.0
arrayNode += "bar".toByteArray()
println(arrayNode.toString()) // ["foo",true,1,1.0,"YmFy"] AnnotationsYou can intermix non-field values in the constructor and @JsonInclude(JsonInclude.Include.NON_EMPTY)
class StateObjectWithPartialFieldsInConstructor(val name: String, @JsonProperty("age") val years: Int) {
@JsonProperty("address") lateinit var primaryAddress: String // set after construction
var createdDt: DateTime by Delegates.notNull() // set after construction
var neverSetProperty: String? = null // not in JSON so must be nullable with default
} Note that using Caveats
Support for Kotlin Built-in classesThese Kotlin classes are supported with the following fields for serialization/deserialization (and other fields are hidden that are not relevant):
(others are likely to work, but may not be tuned for Jackson) Sealed classes without @JsonSubTypesSubclasses can be detected automatically for sealed classes, since all possible subclasses are known
at compile-time to Kotlin. This makes @JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
sealed class SuperClass{
class A: SuperClass()
class B: SuperClass()
}
...
val mapper = jacksonObjectMapper()
val root: SuperClass = mapper.readValue(json)
when(root){
is A -> "It's A"
is B -> "It's B"
} ConfigurationThe Kotlin module may be given a few configuration parameters at construction time; see the inline documentation for details on what options are available and what they do. val mapper = JsonMapper.builder()
.addModule(KotlinModule(strictNullChecks = true))
.build() If your KotlinModule kotlinModule = new KotlinModule.Builder()
.strictNullChecks(true)
.build();
ObjectMapper objectMapper = JsonMapper.builder()
.addModule(kotlinModule)
.build(); DevelopmentMaintainersFollowing developers have committer access to this project.
You may at-reference them as necessary but please keep in mind that all maintenance work is strictly voluntary (no one gets paid to work on this or any other Jackson components) so there is no guarantee for timeliness of responses. All Pull Requests should be reviewed by at least one of active maintainers; bigger architectural/design questions should be agreed upon by majority of active maintainers (at this point meaning both Drew and Vyacheslav :) ). Releases & BranchesThis module follows the release schedule of the rest of Jackson—the current version is consistent across all Jackson components & modules. See the jackson-databind README for details. ContributingWe welcome any contributions—reports of issues, ideas for enhancements, and pull requests related to either of those. See the main Jackson contribution guidlines for more details. BranchesIf you are going to write code, choose the appropriate base branch:
Failing testsThere are a number of tests for functionality that is broken, mostly in the failing
package but a few as part of other test suites. Instead of ignoring these tests (with JUnit's See the tests readme for more information. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论