jackson

Jackson xml 2.9.0: @JacksonXmlElementWrapper not working with @JsonCreator & @JsonProperty constructor

时光总嘲笑我的痴心妄想 提交于 2021-01-29 03:06:26
问题 I would like that my ParentClass has final fields, 'brokenChildList' list is wrapped xml element and list items have different tag than the list ( <brokenChildList><brokenChild/></brokenChildList> ). Here is a snippet of code to reproduce my issues (imports are partially truncated, setters and getters omitted) import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml

How to convert below JSON to POJO using ObjectMapper of Jackson

走远了吗. 提交于 2021-01-29 03:03:41
问题 I am trying below code to convert below JSON to POJO using ObjectMapper class of Jackson but it's throwing exception. Could anyone help me to resolve this issue. Actually JSON is given by UI so can't change format of it. I need to parse this JSON to java object using Jackson library. JSON: data.json { "0": { "location": "6", "userType": "1", "isActive": "1", "userId": "Shailesh@gmail.com" }, "1": { "location": "7", "userType": "2", "isActive": "1", "userId": "Vikram@gmail.com" } } DTOs:

SpringMVC Json自定义序列化和反序列化

我的梦境 提交于 2021-01-28 21:40:44
需求背景 需求一:SpringMVC构建的微服务系统,数据库对日期的存储是Long类型的时间戳,前端之前是默认使用Long类型时间,现在前端框架改动,要求后端响应数据时,Long类型的时间自动变成标准时间格式(yyyy-MM-dd HH:mm:ss)。 涉及到这个转换的范围挺大,所有的实体表都有创建时间createTime和修改时间updateTime,目前的主要诉求也是针对这两个字段,并且在实体详情数据和列表数据都存在,需要一个统一的方法,对这两个字段进行处理。 需求二:前端请求上传的JSON报文,String类型的内容,可能会出现前后有空格的现象,如果前端框架未对此问题进行处理,后端收到的JSON请求反序列化为对象时,就会出现String类型的值,前后有空格,现需要一个统一的处理方法,对接收的String类型属性执行trim方法。 解决方案 SpringMVC默认的JSON框架为jackson,也可以使用fastjson。 jackson框架 自定义序列化 如果项目使用jackson框架做json序列化,推荐的方案是使用@JsonSerialize注解,示例代码如下: @JsonSerialize(using = CustomDateSerializer.class) private Long createTime; @JsonSerialize(using =

Jackson serialization issue. Only first object of the same entity serializes well

喜欢而已 提交于 2021-01-28 15:31:36
问题 I develop a REST voting system where users can vote on restaurants. I have a Vote class which contains User, Restaurant and Date. public class Vote extends AbstractBaseEntity { @NotNull @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User user; @NotNull @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "restaurant_id") private Restaurant restaurant; @Column(name = "date", nullable = false) @NotNull private LocalDate date; } I need to find all votes of the day.

Spring Boot custom serializer for Collection class

旧街凉风 提交于 2021-01-28 13:42:36
问题 I was trying to implement a custom serializer for one of the properties of my object to get a different JSON structure when I return it from my REST controller. My constraints are I cannot change the interface of the REST controller or the model classes (so I cannot add extra annotation etc, that would maybe make this easier). The only thing I could think of, making it render different than described in the model is a custom serializer, if there are any better approaches for this, please don

Jackson date deserialization - “invalid” day of month

此生再无相见时 提交于 2021-01-28 12:20:21
问题 I am using Spring Boot with Jackson serializing/deserializing JSON requests/responses. I have come accross a behaviour when trying to deserialize Date in ISO format that I would like to avoid. When I use an invalid day of month or month of year, Jackson handles it by adding the extra number of days/months to the date. For instance { "date": "2018-02-40T15:00:00+01:00" } is deserialized as Mon Mar 12 15:00:00 CET 2018 Or { "date": "2018-14-20T15:00:00+01:00" } as Wed Feb 20 15:00:00 CET 2019

Jackson SAX parser throws exception when parsing huge JSON

情到浓时终转凉″ 提交于 2021-01-28 12:14:38
问题 I'm trying to implement JSON array iterator with Jackson SAX parser (please, don't ask why). My app should work with huge files (up to 5 MiB), and that's a problem. That's how I initialize JsonParser and call iterator creation. I create InputStream initialized with JSON placed in \raw folder. private JsonArrayIterator getIterator(String needle) throws IOException { InputStream inputStream = getApplicationContext().getResources().openRawResource(R.raw.products); inputStream.mark(-1); try {

Why Jackson JSON mapping exception when Serializing/Deserializing Geometry type

依然范特西╮ 提交于 2021-01-28 11:59:56
问题 When I create a User defined class "Asset". public class Asset { private UUID id; private String name; } And set an object of this class as a response. @GetMapping("/testSerialization") public Asset testSerialization() { return new Asset() } This controller works successfully. But when the same controller uses Geometry Types the request fails, import com.vividsolutions.jts.geom.Point; // Does not work @GetMapping("/testSerialization") public Point testSerialization() { GeometryFactory

Why Jackson JSON mapping exception when Serializing/Deserializing Geometry type

大城市里の小女人 提交于 2021-01-28 11:39:14
问题 When I create a User defined class "Asset". public class Asset { private UUID id; private String name; } And set an object of this class as a response. @GetMapping("/testSerialization") public Asset testSerialization() { return new Asset() } This controller works successfully. But when the same controller uses Geometry Types the request fails, import com.vividsolutions.jts.geom.Point; // Does not work @GetMapping("/testSerialization") public Point testSerialization() { GeometryFactory

Jackson JSON custom class instantiator for third-party class

笑着哭i 提交于 2021-01-28 11:27:20
问题 I am using a third-party POJO class RetryOptions that can only be created using a builder. The builder can only be instantiated using a static method RetryOptions.newBuilder() , or by calling options.toBuilder() on an existing instance. I would like to create custom de/serializers for the third-party POJO ( RetryOptions ). My first approach was to write the object as a builder, then read the object as a builder and return the built result: class RetryOptionsSerializer extends StdSerializer