jackson

YAML or JSON library that supports inheritance

点点圈 提交于 2021-01-28 11:04:42
问题 We are building a service. It has to read config from a file. We are currently using YAML and Jackson for deserializing the YAML. We have a situation where our YAML file needs to inherit/extend another YAML file(s). E.g., something like: extends: base.yaml appName: my-awesome-app ... thus part of the config is stored in base.yaml . Is there any library that has support for this? Bonus points if it allows to inherit from more than one file. We could change to using JSON instead of YAML. 回答1:

@JsonSerialize - How to create a wrapper at runtime and use default serialization for the object fields?

末鹿安然 提交于 2021-01-28 10:54:47
问题 I want to add a wrapper which named is determined at runtime, because it depends of the class name (I could use @JsonRootName but I don't want to because I would have to use it on every sub class, which is not efficient). I suppose I should use @JsonSerialize to override the default serializer, but I want that just to create the wrapper; I don't want to serialize the object fields myself (also I am in an abstract class, so I don't even know the fields of the sub class!). I don't care about

Configuring ObjecMapper null serialization after first serialization does not have effect

强颜欢笑 提交于 2021-01-28 08:40:24
问题 Stumbled on this behavior when making some experiments with ObjectMapper . See Junit5 test cases below and what those print for me in comments. class TestIt { private ObjectMapper om = new ObjectMapper(); private TestClass testClass = new TestClass(); @Getter @Setter public class TestClass { private final String value = LocalDate.now().toString(); private String valueLeftNull; } @Test void defaultMapping() throws JsonProcessingException { System.out.println(om.writeValueAsString(testClass));

mapping rest responses with Jackson JSON

亡梦爱人 提交于 2021-01-28 07:00:38
问题 I have a REST call that can return either AccountPojo or ErrorCodePojo . Each of these have specific fields. How can I use the Jackson JSON libary to easily parse the response and fetch AccountPojo or the ErrorCodePojo ? I have something like: ObjectMapper mapper = new ObjectMapper(); try { ret = mapper.readValue(json, AccountPojo.class); } catch (JsonProcessingException e) { ret = mapper.readValue(json, ErrorCodePojo.class); } but I don't think this is nice. Thanks! 回答1: You can also use

How to deserialize extra properties of json document into a map or other field with jackson?

为君一笑 提交于 2021-01-28 06:09:40
问题 I have a class/interface pair defined like this in Kotlin: @JsonDeserialize(`as` = SimpleEvent::class) interface Event { val organizationId: UUID val userId: UUID val eventTime: LocalDateTime val eventId: UUID @get:JsonAnyGetter @get:JsonAnySetter val details: Map<String, JsonNode> } data class SimpleEvent( override val organizationId: UUID, override val userId: UUID, override val eventTime: LocalDateTime, override val eventId: UUID, override val details: Map<String, JsonNode> ) : Event My

Deserializing from JSON using foreign key

徘徊边缘 提交于 2021-01-28 06:07:49
问题 I have a many to one relationship: A *<-->1 B and I want to deserialize A from a JSON having B 's primary key ( B exists in db with that primary key): { "b": 1 } I have tried the following: @Entity @Table(name = "table_a") @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class A implements Serializable { @JsonIgnore @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name = "b", unique = true, nullable = false) private B b; } and @Entity @Table(name =

How to convert a JsonNode instance to an actual pojo

旧城冷巷雨未停 提交于 2021-01-28 06:02:47
问题 At a certain point in my code, I have parse a JSON document, represented as a string, to a JsonNode , because I don't know yet the actual target pojo class type. Now, some time later, I know the Class instance of the pojo and I want to convert this JsonNode to an actual pojo of that class (which is annotated with the proper @JsonProperty annotations). Can this be done? If so, how? I am working with Jackson 2.10.x. 回答1: In this case you can use two methods: treeToValue convertValue See below

Deserializing from JSON using foreign key

我怕爱的太早我们不能终老 提交于 2021-01-28 06:00:49
问题 I have a many to one relationship: A *<-->1 B and I want to deserialize A from a JSON having B 's primary key ( B exists in db with that primary key): { "b": 1 } I have tried the following: @Entity @Table(name = "table_a") @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id") public class A implements Serializable { @JsonIgnore @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name = "b", unique = true, nullable = false) private B b; } and @Entity @Table(name =

Jackson/Gson serialize and deserialize JavaFX Properties to json

两盒软妹~` 提交于 2021-01-28 05:30:29
问题 I have added a BooleanProperty into a DAO class which is to be serialized into JSON and sent across to a server to be saved in a MySQL database. The reason I am using the BooleanProperty is because I want to use data binding for the 'isActive' field in my JavaFX desktop application. The class to be serialized: package com.example.myapplication import lombok.Data; import javafx.beans.property.BooleanProperty; import javafx.beans.property.SimpleBooleanProperty; @Data public class MyDAO {

Jooq fetchInto class java.util.LinkedHashMap cannot be cast to class

随声附和 提交于 2021-01-28 04:45:41
问题 Giving the last example given in this SO thread. I get this error: java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.example.dtos.UserElement (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.example.dtos.UserElement is in unnamed module of loader 'app') Am I supposed to use a special mapper that jooq will be happy with? Any help is welcome Edit: Jooq version: 3.14.3 Postgres: 11 ctx.select( USERS.ID.`as`("id"), USERS.USERNAME.`as