jackson2

jackson 2 object to json ignore lazy loading

半城伤御伤魂 提交于 2019-12-08 04:28:20
问题 I am using jackson 2 to parse an object to json format I am having problem when I parse lazy loading fields. I want to get a null reference if the object cannot be loaded. How can I do that? I want to discuss with you a solution and I want to know if there is a problem when I use it? I have found that during parsing jackson uses getters method to get attributes So my solution is to modify the getters methods of lazy loading fields suppose that post is a lazy loaded field this is the getter of

jackson 2 object to json ignore lazy loading

纵饮孤独 提交于 2019-12-08 04:04:25
I am using jackson 2 to parse an object to json format I am having problem when I parse lazy loading fields. I want to get a null reference if the object cannot be loaded. How can I do that? I want to discuss with you a solution and I want to know if there is a problem when I use it? I have found that during parsing jackson uses getters method to get attributes So my solution is to modify the getters methods of lazy loading fields suppose that post is a lazy loaded field this is the getter of that fields public Collection<Post> getPosts() { try{ posts.size(); return posts; } catch (Exception e

Configuring Jackson mixin in Spring Boot application

元气小坏坏 提交于 2019-12-08 03:36:36
问题 I created a mixin for my class. The mixin itself works fine, it's not the issue that most people have where they mix faterxml/codehaus annotations. I tested it in a unit test, creating the ObjectMapper "by hand" while using the addMixIn method - it worked just fine. I want to use that mixin to modify the response jsons returned from my REST endpoints. I've tried to customize Spring Boot's ObjectMapper in many different ways: BuilderCustomizer: @Bean public

Serialize JsonNode to a very specific JSON format in Jackson

一世执手 提交于 2019-12-06 13:24:22
I have JsonNode result that I want to print out. So far, I am using: ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); File outputFile = new File( getCurOutputDir(), String.format("out.json", getClass().getSimpleName()) ); mapper.writeValue(new FileOutputStream(outputFile), resultNode); which outputs something like: { "A" : [ { "Ai" : { "Ai1" : 42, "Ai2" : 55 } } ], "B" : [ 86 ] } but I need it to be in this specific format: { "A" : [ { "Ai" : { "Ai1" : 42, "Ai2" : 55 } } ], "B" : [ 86 ] } For context, I am transitioning from JSONObject to Jackson, so

How to use Jackson 2 in Payara 5?

耗尽温柔 提交于 2019-12-05 02:42:29
问题 I'm using Jackson 2 with Payara 4 and I would liked to use Jackson 2 in Payara 5 . Using JAX-RS, I also would like to avoid changing annotations and so on... In Payara 5 the default Jsonb provider is Yasson. Any ideas to disable it and use Jackson instead? All comments/ideas are welcome :-) NB: Yasson is very interesting but handle abstract class or interface serialization/deserialization is a little more complex than putting a Jackson annotation. My current understanding is that it requires

Force Milliseconds When Serializing Instant to ISO8601 using Jackson

岁酱吖の 提交于 2019-12-05 01:22:53
I have some questions related to JSON serialization using Jackson in a project where I use Spring Boot 2.0.0.M6 , Spring Framework 5.0.1.RELEASE and Jackson 2.9.2 . I have configured the following Jackson-related settings in application.properties : spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS=false Serialization works mostly as I need. Nevertheless, I have noticed that Jackson seems to cut-off milliseconds if they are 000 . Test 1: Serialize Instant with milliseconds set to 000 : Initialize Instant field using Instant.parse("2017-09-14T04:28:48.000Z") Serialize it using Jackson

Configure a Jackson's DeserializationProblemHandler in Spring environment [duplicate]

天大地大妈咪最大 提交于 2019-12-02 03:54:09
This question already has an answer here: Can't set ProblemHandler to ObjectMapper in Spring Boot 1 answer As I understood, Spring is already providing a bean for Jackson ObjectMapper . Therefore, instead of creating a new bean, I'm trying to customize this bean. From this blog post , and then this Github project I used Jackson2ObjectMapperBuilder bean to achieve this customization. @Bean public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder(ApplicationContext context) { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); builder.findModulesViaServiceLoader(true);

Wrapping Json fields into instance variable of a pojo

半世苍凉 提交于 2019-12-02 02:24:03
问题 i am trying to map certain json fields to a class instance variable. My sample Person class looks like: public class Person { private String name; private Address address; //many more fields //getters and setters } The sample Address class is: public class Address { private String street; private String city; //many more fields // getters and setters } The json object to be deserialized to my Person class doesn't contain "address" field. It looks like: { "name":"Alexander", "street":"abc 12",

Wrapping Json fields into instance variable of a pojo

故事扮演 提交于 2019-12-01 23:06:15
i am trying to map certain json fields to a class instance variable. My sample Person class looks like: public class Person { private String name; private Address address; //many more fields //getters and setters } The sample Address class is: public class Address { private String street; private String city; //many more fields // getters and setters } The json object to be deserialized to my Person class doesn't contain "address" field. It looks like: { "name":"Alexander", "street":"abc 12", "city":"London" } Is there a way to deserialize the json to the Person pojo where the Address fields

Deserializing attributes of same name but different types in Jackson?

不想你离开。 提交于 2019-12-01 05:25:10
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 Channel type when it's a JSON Object? Or in other words, is there a way in Jackson which maps by type