jackson2

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));

Deserialize property file that contains dot with Jackson

◇◆丶佛笑我妖孽 提交于 2020-01-23 11:52:54
问题 In our application we are trying to read a flat property file using Jackson to match the properties to our POJO Everything works fine, but when the property name contains some dot, the wholo POJO is set to null Here is a sample of the property file p.test=Just a test Here is my POJO public class BasicPOJO { @JsonProperty("p.test") private String test; public String getTest() { return test; } public void setTest(String test) { this.test = test; } } And here is how I map it InputStream in =

Jackson custom filter with full POJO data bind

我是研究僧i 提交于 2020-01-14 07:12:49
问题 This question extends this question. While the previous solution works great if you only have a couple of fields, it becomes unmaintainable when you have more than a dozen of fields. Right now, my current set up uses full data binding, so I have a POJO that will be used by Jackson to automatically deserialize JSON. However, as before, certain fields have constraints that need to pass. Essentially, I am looking for an answer similar to this, but without the need to set any properties. Just a

How to parse different ISO date/time formats with Jackson and java.time?

佐手、 提交于 2019-12-30 14:47:28
问题 Our Rest API takes JSON input from several external parties. They all use "ISO-ish" formats, but the formatting of the time zone offset is slightly different. These are some of the most common formats we see: 2018-01-01T15:56:31.410Z 2018-01-01T15:56:31.41Z 2018-01-01T15:56:31Z 2018-01-01T15:56:31+00:00 2018-01-01T15:56:31+0000 2018-01-01T15:56:31+00 Our stack is Spring Boot 2.0 with Jackson ObjectMapper. In our data classes we use the type java.time.OffsetDateTime a lot. Several developers

Extract specific node of JSON response in RESTEasy Client

怎甘沉沦 提交于 2019-12-25 09:05:11
问题 I'm retrieving a JSON string from an API using a RESTEasy client. The JSON payload looks something like this: { "foo1" : "", "foo2" : "", "_bar" : { "items" : [ { "id" : 1 , "name" : "foo", "foo" : "bar" }, { "id" : 2 , "name" : "foo", "foo" : "bar" }, { "id" : 3 , "name" : "foo", "foo" : "bar" }, { "id" : 4 , "name" : "foo", "foo" : "bar" } ] } } Now I'd like to extract only the items node for object mapping. What is the best way to intercept the JSON response body and modify it to have

Remove the automatically display of getter in POJO

谁说我不能喝 提交于 2019-12-25 07:41:36
问题 Below I have given my POJO structure and the current out put and expected output. My requirement is, when I am printing the JSON format the variable called "applicationUsage " automatically included in my output JSON as key, But I dont want to add "applicationUsage " key in my json format and only wants to show the values stored in this field. Can anyone help me with the code. @JsonRootName(value = "MediationUserCacheRequest") @JsonTypeInfo(include = As.WRAPPER_OBJECT, use = Id.NAME)

JsonGenerationException when serializing nested object using custom serializer in Jackson

泪湿孤枕 提交于 2019-12-25 03:33:16
问题 Here is the class that I want to serialize. public class ItemRow<T> { private String id; private List<T> items; } There are two variations that are allowed. ItemRow<String>, ItemRow<ItemRow> . In the latter case, it will be nested. eg: ItemRow item1 = new ItemRow("abc", Arrays.asList("item1", "item2", "item3")); String result = mapper.writeValueAsString(item1); System.out.println(result); should give { "abc":["item1","item2","item3"] } Now, the latter case ItemRow item2 = new ItemRow("cde",