jackson2

Deserializing attributes of same name but different types in Jackson?

此生再无相见时 提交于 2019-12-01 02:44:00
问题 I have a REST API which returns a JSON response as: { "channel" : "JHBHS" } and sometimes it returns: { "channel": { "id": 12321, "name": "Some channel" } } I have a POJO like: public class Event { private String channel; @JsonProperty("channel") private Channel channelObj; } public class Channel { private int id; private String name; } So, is there a way (other than writing your own custom deserializer ) in Jackson2 which will help me map channel in JSON to String type when it's a String and

Converting Nested json into dot notation json

橙三吉。 提交于 2019-11-29 08:25:05
I have a service from where I get a json string response like as shown below { "id": "123", "name": "John" } I consume the rest call using HttpClient and converts the json string to Map<String, String> like as shown below. String url= "http://www.mocky.io/v2/5979c2f5110000f4029edc93"; HttpClient client = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); httpGet.setHeader("Content-Type", "application/json"); HttpResponse httpresponse = client.execute(httpGet); String response = EntityUtils.toString(httpresponse.getEntity()); ObjectMapper mapper = new ObjectMapper(); Map

Jackson, deserialize class with private fields and arg-constructor without annotations

独自空忆成欢 提交于 2019-11-28 09:53:26
It is possible to deserialize to a class with private fields and a custom argument constructor without using annotations and without modifying the class, using Jackson? I know it's possible in Jackson when using this combination: 1) Java 8, 2) compile with "-parameters" option, and 3) the parameters names match JSON. But it's also possible in GSON by default without all these restrictions. For example: public class Person { private final String firstName; private final String lastName; private final int age; public Person(String firstName, String lastName, int age) { this.firstName = firstName

Converting Nested json into dot notation json

拈花ヽ惹草 提交于 2019-11-28 01:41:37
问题 I have a service from where I get a json string response like as shown below { "id": "123", "name": "John" } I consume the rest call using HttpClient and converts the json string to Map<String, String> like as shown below. String url= "http://www.mocky.io/v2/5979c2f5110000f4029edc93"; HttpClient client = HttpClientBuilder.create().build(); HttpGet httpGet = new HttpGet(url); httpGet.setHeader("Content-Type", "application/json"); HttpResponse httpresponse = client.execute(httpGet); String

How to use @JsonIdentityInfo with circular references?

戏子无情 提交于 2019-11-27 13:47:44
I am trying to use the @JsonIdentityInfo from Jackson 2 as described here . For testing purposes I created the following two classes: public class A { private B b; // constructor(s) and getter/setter omitted } public class B { private A a; // see above } Of course, the naive approach failes: @Test public void testJacksonJr() throws Exception { A a = new A(); B b = new B(a); a.setB(b); String s = JSON.std.asString(a);// throws StackOverflowError Assert.assertEquals("{\"@id\":1,\"b\":{\"@id\":2,\"a\":1}}", s); } Adding @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class,

How to make default time zone apply in Spring Boot Jackson Date serialization

霸气de小男生 提交于 2019-11-27 03:18:14
问题 I have configured my Spring Boot application to serialize dates as ISO8601 strings: spring: jackson: serialization: write-dates-as-timestamps: false This is what I am getting: "someDate": "2017-09-11T07:53:27.000+0000" However my time zone is Europe/Madrid. In fact if I print TimeZone.getDefault() that's what I get. How can I make Jackson serialize those datetime values using the actual timezone? GMT+2 "someDate": "2017-09-11T09:53:27.000+0200" 回答1: You can set timezone for whole application

Jackson, deserialize class with private fields and arg-constructor without annotations

一曲冷凌霜 提交于 2019-11-27 03:15:12
问题 It is possible to deserialize to a class with private fields and a custom argument constructor without using annotations and without modifying the class, using Jackson? I know it's possible in Jackson when using this combination: 1) Java 8, 2) compile with "-parameters" option, and 3) the parameters names match JSON. But it's also possible in GSON by default without all these restrictions. For example: public class Person { private final String firstName; private final String lastName;

How to use @JsonIdentityInfo with circular references?

Deadly 提交于 2019-11-26 18:17:25
问题 I am trying to use the @JsonIdentityInfo from Jackson 2 as described here. For testing purposes I created the following two classes: public class A { private B b; // constructor(s) and getter/setter omitted } public class B { private A a; // see above } Of course, the naive approach failes: @Test public void testJacksonJr() throws Exception { A a = new A(); B b = new B(a); a.setB(b); String s = JSON.std.asString(a);// throws StackOverflowError Assert.assertEquals("{\"@id\":1,\"b\":{\"@id\":2,