Do not include empty object to Jackson
up vote
-1
down vote
favorite
I am trying to create json with spring boot.
class:
public class Person {
private String name;
private PersonDetails details;
// getters and setters...
}
impletentation:
Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());
So there is a instance of Person
with empty details
and this is exactly what Jackson is returning:
"person": {
"name": "Apple",
"details": {}
}
I want to have json without empty brackets {}
:
"person": {
"name": "Apple"
}
This question's didn't helped me:
- How to tell Jackson to ignore empty object during deserialization?
- How to ignore "null" or empty properties in json, globally, using Spring configuration
Update 1:
I'm using Jackson 2.9.6
java json spring-boot
add a comment |
up vote
-1
down vote
favorite
I am trying to create json with spring boot.
class:
public class Person {
private String name;
private PersonDetails details;
// getters and setters...
}
impletentation:
Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());
So there is a instance of Person
with empty details
and this is exactly what Jackson is returning:
"person": {
"name": "Apple",
"details": {}
}
I want to have json without empty brackets {}
:
"person": {
"name": "Apple"
}
This question's didn't helped me:
- How to tell Jackson to ignore empty object during deserialization?
- How to ignore "null" or empty properties in json, globally, using Spring configuration
Update 1:
I'm using Jackson 2.9.6
java json spring-boot
Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g.@JsonInclude(Include.NON_NULL)
not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
– h0nzan
Nov 10 at 11:38
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I am trying to create json with spring boot.
class:
public class Person {
private String name;
private PersonDetails details;
// getters and setters...
}
impletentation:
Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());
So there is a instance of Person
with empty details
and this is exactly what Jackson is returning:
"person": {
"name": "Apple",
"details": {}
}
I want to have json without empty brackets {}
:
"person": {
"name": "Apple"
}
This question's didn't helped me:
- How to tell Jackson to ignore empty object during deserialization?
- How to ignore "null" or empty properties in json, globally, using Spring configuration
Update 1:
I'm using Jackson 2.9.6
java json spring-boot
I am trying to create json with spring boot.
class:
public class Person {
private String name;
private PersonDetails details;
// getters and setters...
}
impletentation:
Person person = new Person();
person.setName("Apple");
person.setDetails(new PersonDetails());
So there is a instance of Person
with empty details
and this is exactly what Jackson is returning:
"person": {
"name": "Apple",
"details": {}
}
I want to have json without empty brackets {}
:
"person": {
"name": "Apple"
}
This question's didn't helped me:
- How to tell Jackson to ignore empty object during deserialization?
- How to ignore "null" or empty properties in json, globally, using Spring configuration
Update 1:
I'm using Jackson 2.9.6
java json spring-boot
java json spring-boot
edited Nov 10 at 0:15
asked Nov 9 at 23:52
Marin
12
12
Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g.@JsonInclude(Include.NON_NULL)
not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
– h0nzan
Nov 10 at 11:38
add a comment |
Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g.@JsonInclude(Include.NON_NULL)
not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?
– h0nzan
Nov 10 at 11:38
Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g.
@JsonInclude(Include.NON_NULL)
not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?– h0nzan
Nov 10 at 11:38
Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g.
@JsonInclude(Include.NON_NULL)
not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?– h0nzan
Nov 10 at 11:38
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Without a custom serializer, jackson will include your object.
Solution 1 : Replace new object with null
person.setDetails(new PersonDetails());
with
person.setDetails(null);
and add
@JsonInclude(Include.NON_NULL)
public class Person {
Solution 2: Custom serializer
public class PersonDetailsSerializer extends StdSerializer<PersonDetails> {
public PersonDetailsSerializer() {
this(null);
}
public PersonDetailsSerializer(Class<PersonDetails> t) {
super(t);
}
@Override
public void serialize(
PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
// custom behavior if you implement equals and hashCode in your code
if(personDetails.equals(new PersonDetails()){
return;
}
super.serialize(personDetails,jgen,provider);
}
}
and in your PersonDetails
public class Person {
private String name;
@JsonSerialize(using = PersonDetailsSerializer.class)
private PersonDetails details;
}
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Without a custom serializer, jackson will include your object.
Solution 1 : Replace new object with null
person.setDetails(new PersonDetails());
with
person.setDetails(null);
and add
@JsonInclude(Include.NON_NULL)
public class Person {
Solution 2: Custom serializer
public class PersonDetailsSerializer extends StdSerializer<PersonDetails> {
public PersonDetailsSerializer() {
this(null);
}
public PersonDetailsSerializer(Class<PersonDetails> t) {
super(t);
}
@Override
public void serialize(
PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
// custom behavior if you implement equals and hashCode in your code
if(personDetails.equals(new PersonDetails()){
return;
}
super.serialize(personDetails,jgen,provider);
}
}
and in your PersonDetails
public class Person {
private String name;
@JsonSerialize(using = PersonDetailsSerializer.class)
private PersonDetails details;
}
add a comment |
up vote
1
down vote
Without a custom serializer, jackson will include your object.
Solution 1 : Replace new object with null
person.setDetails(new PersonDetails());
with
person.setDetails(null);
and add
@JsonInclude(Include.NON_NULL)
public class Person {
Solution 2: Custom serializer
public class PersonDetailsSerializer extends StdSerializer<PersonDetails> {
public PersonDetailsSerializer() {
this(null);
}
public PersonDetailsSerializer(Class<PersonDetails> t) {
super(t);
}
@Override
public void serialize(
PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
// custom behavior if you implement equals and hashCode in your code
if(personDetails.equals(new PersonDetails()){
return;
}
super.serialize(personDetails,jgen,provider);
}
}
and in your PersonDetails
public class Person {
private String name;
@JsonSerialize(using = PersonDetailsSerializer.class)
private PersonDetails details;
}
add a comment |
up vote
1
down vote
up vote
1
down vote
Without a custom serializer, jackson will include your object.
Solution 1 : Replace new object with null
person.setDetails(new PersonDetails());
with
person.setDetails(null);
and add
@JsonInclude(Include.NON_NULL)
public class Person {
Solution 2: Custom serializer
public class PersonDetailsSerializer extends StdSerializer<PersonDetails> {
public PersonDetailsSerializer() {
this(null);
}
public PersonDetailsSerializer(Class<PersonDetails> t) {
super(t);
}
@Override
public void serialize(
PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
// custom behavior if you implement equals and hashCode in your code
if(personDetails.equals(new PersonDetails()){
return;
}
super.serialize(personDetails,jgen,provider);
}
}
and in your PersonDetails
public class Person {
private String name;
@JsonSerialize(using = PersonDetailsSerializer.class)
private PersonDetails details;
}
Without a custom serializer, jackson will include your object.
Solution 1 : Replace new object with null
person.setDetails(new PersonDetails());
with
person.setDetails(null);
and add
@JsonInclude(Include.NON_NULL)
public class Person {
Solution 2: Custom serializer
public class PersonDetailsSerializer extends StdSerializer<PersonDetails> {
public PersonDetailsSerializer() {
this(null);
}
public PersonDetailsSerializer(Class<PersonDetails> t) {
super(t);
}
@Override
public void serialize(
PersonDetails personDetails, JsonGenerator jgen, SerializerProvider provider)
throws IOException, JsonProcessingException {
// custom behavior if you implement equals and hashCode in your code
if(personDetails.equals(new PersonDetails()){
return;
}
super.serialize(personDetails,jgen,provider);
}
}
and in your PersonDetails
public class Person {
private String name;
@JsonSerialize(using = PersonDetailsSerializer.class)
private PersonDetails details;
}
answered Nov 11 at 8:15
Adina
1,1921210
1,1921210
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234727%2fdo-not-include-empty-object-to-jackson%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Custom serializer/deserializer for ignoring your empty specific object sounds as good solution. So e.g.
@JsonInclude(Include.NON_NULL)
not working in this case because you haven't null object, you have object wich has no attribute filled. Why do you want to ignore empty object in JSON?– h0nzan
Nov 10 at 11:38