Tag: 杰克逊

Kotlin – 杰克逊忽略空值

我解析与杰克逊JSON的麻烦,我想忽略空属性。 这是我的代码。 @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) data class ParsedSurvey( val items: List<ParsedSurveyItem> = listOf() ) @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_NULL) data class ParsedSurveyItem( val type: String = "", val text: String = "", val instructions: String = "", val prog: String = "", val `var`: String = "", val columns: List<ParsedSurveyAnswer> = listOf(), val rows: List<ParsedSurveyAnswer> […]

为什么Spring MVC JSON的Kotlin返回一个空对象?

我一直在玩服务器端Kotlin,Spring MVC和Jackson。 我使用http://start.spring.io/构建了一个简单的应用程序,但是我可能在JsonView注释中犯了一个错误。 这个: @RestController class MyRestController { @RequestMapping("/user") @JsonView(User::class) fun getUser() : User = User("Fred",50) } data class User(val name: String, val age: Int) …当被卷曲调用时 ph@sleek ~ $ curl -X GET http://localhost:8080/user; echo {} ph@sleek ~ $ …当我期望{"name":"Fred","age":50}时,结果是{} {"name":"Fred","age":50} 。 有什么我做错了吗?

Spring数据Elasticsearch与Kotlin数据类的脚本字段导致Jackson序列化问题

当试图从Elasticsearch获取查询结果并将其映射到使用@org.springframework.data.elasticsearch.annotations.ScriptedField批注的Kotlin数据类时,结果实例似乎只用脚本字段创建,而不是其他字段被同时获取,因此导致.MissingKotlinParameterException 。 查询是通过ElasticsearchRepository#search方法完成的,该方法随Spring Data Elasticsearch提供,下面是相关的代码片段: val searchQuery = NativeSearchQueryBuilder() .withQuery(geoDistanceQuery("location").point(53.0, 10.0).distance("100km")) .withScriptField(ScriptField("distance", Script(ScriptType.INLINE, "expression", "doc['location'].arcDistance(params.latitude, params.longitude)", emptyMap()))) .withFilter(boolQuery().must(existsQuery("title"))) .withSort(SortBuilders.geoDistanceSort("location", 53.0, 10.0).order(SortOrder.ASC)) .withPageable(PageRequest.of(0, 10)) .build() val nearby = eventRepository.search(searchQuery) 具有更多上下文的堆栈跟踪: Caused by: com.fasterxml.jackson.module.kotlin.MissingKotlinParameterException: Instantiation of [simple type, class de.nava.demo.events.model.Event] value failed for JSON property id due to missing (therefore NULL) value for creator parameter id which […]

注释Kotlin中的属性时,注释的默认目标是什么?

Kotlin中的注释可以具有不同的使用地点目标,如下所述: https : //kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets 我的问题是:当use-site没有明确定义的时候,像下面这个例子那样在注释一个类的属性的时候默认的目标是什么? class Test { @SomeAnnotation var someProperty: String? = null } 背景 我正在尝试Jongo作为Kotlin中的MongoDB客户端,并注意到id字段的问题。 Jongo没有正确映射id属性,如下所示: @MongoId @MongoObjectId var id: String? = null 这些注释只是Jackson的元注释。 但是,当我注释这样的属性,它似乎工作,指示使用现场问题: @field:[MongoId MongoObjectId] var id: String? = null 我期望@field是默认的使用网站,但它似乎不是。

在Kotlin中,为什么杰克逊在某些情况下未能解组未注释的对象,而不是在其他情况下

我使用Kotlin和Jongo来访问MongoDB。 Jongo使用Jackson来序列化/反序列化对象,以便从MongoDB保存和读取它们。 我使用Jackson-Kotlin模块来帮助使用构造函数序列化Kotlin数据类。 下面是一个序列化好的数据类的例子: data class Workflow ( @field:[MongoId MongoObjectId] @param:MongoId var id: String? = null, val name: String, val states: Map<String, State> ) 下面是一个不能反序列化的类的例子: data class Session ( @field:[MongoObjectId MongoId] @param:MongoId var id: String? = null, var status: CallStatus, var currentState: String, var context: MutableMap<String, Any?>, val events: MutableMap<String, Event> ) Jongo抛出了以下例外,因为Jackson的反序列化失败了: org.jongo.marshall.MarshallingException: Unable […]

在Kotlin中,Jackson数据类的反序列化错误

不确定如何最好地解释这个问题,所以我讽刺了java和kotlin相同的代码来更好的演示。 当我读取JSON时,即使参数甚至不是json的一部分,数据bean默认值是缺少字段的值,但它似乎强制数据bean值为NULL。 在Java中它正常工作,从来没有试图取消从未提供开始的值。 在Kotlin,它似乎打破了,因为它试图取消一个不可空的字段。 在Kotlin data class Pojo(val name: String, val age: Int, val list: List<String> = emptyList(), val ts: Date = Date()) private val mapper: ObjectMapper = ObjectMapper().registerModule(KotlinModule()) .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) fun main(args: Array<String>) { mapper.readValue("""{"name": "John Doe", "list": ["yellow", "green"], "age": 42}""", Pojo::class.java) } 哪个抛出异常 java.lang.IllegalArgumentException: Parameter specified as non-null is null: method Pojo.<init>, parameter […]

Kotlin Jackson从JSON生成对象

请帮忙! 我试图从杰克逊kotlin模块的JSON生成对象。 这里是json源码: { "name": "row", "type": "layout", "subviews": [{ "type": "horizontal", "subviews": [{ "type": "image", "icon": "ic_no_photo", "styles": { "view": { "gravity": "center" } } }, { "type": "vertical", "subviews": [{ "type": "text", "fields": { "text": "Some text 1" } }, { "type": "text", "fields": { "text": "Some text 2" } }] }, { […]

Kotlin:数据类的JsonProperty.Access.READ_ONLY

我想存档这样的东西: data class Task( @JsonProperty("id", access = READ_ONLY) val id: String? … <other params> … ) 不幸的是,这导致以下异常: com.fasterxml.jackson.databind.JsonMappingException: Argument #0 of constructor [constructor for Task, annotations: [null]] has no property name annotation; must have name when multiple-parameter constructor annotated as Creator at [Source: {"id":"AT-5", …}; line: 1, column: 1] at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:305) at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:268) at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244) […]

Kotlin数据类杰克逊@JsonProperty没有兑现

我使用Kotlin数据类将Feign连接到POST。 我打电话的API期望{…“brandInfo”:{“TPID”:1} …} 我的理解是,如果jackson-module-kotlin的依赖没有被正确拾取,Feign将根本无法开启POST,因为Jackson编码将会彻底失败。 然而,Feign能够发布,杰克逊能够编码,但不管我做什么,发布的是{…“brandInfo”:{“tpid”:1} …},尽管brandInfo val被注释@JsonProperty(“TPID”)。 我错过了什么? @JsonIgnoreProperties(ignoreUnknown = true) data class KBrandInfo ( @JsonProperty("TPID") //not honored val TPID: Long ) interface KConversationServiceClient { @RequestLine("POST v2/conversations") @Headers("Content-Type: application/json") fun createConversation(createConversation: KCreateConversation): String } @Provides public KConversationServiceClient getKConversationServiceClient( @Named("conversationServiceUrl") String baseUri, Feign.Builder builder) { return builder .logLevel(Logger.Level.FULL) .decoder(new StringDecoder()) .encoder(new JacksonEncoder(jacksonObjectMapper())) //does this need some […]