Spring boot, Jackson Json Issue while serializing and deserializing
up vote
1
down vote
favorite
For some use case, I need to convert one POJO to another POJO with the different fields name. I tried using Jackson object mapper. It worked in some extends. However end result is not what I expected.
public class JacksonTest {
public static void main(String args) throws IOException{
ObjectMapper mapper = new ObjectMapper();
User user = new User("Deepak", "111", "Singapore");
UserMap newUser = mapper.convertValue(user, UserMap.class);
System.out.println("SOUT: " + newUser);
System.out.println("Jackson: " + mapper.writeValueAsString(newUser));
}
}
class User {
User(String name, String id, String address){
this.name = name;
this.id = id;
this.address = address;
}
String name;
String id;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
class UserMap implements Serializable {
@JsonProperty("name")
String name;
private Map<String, Object> meta = new HashMap<>();
@JsonAnyGetter
public Map<String, Object> any() {
return meta;
}
@JsonAnySetter
public void set(String name, Object value) {
meta.put(name, value);
}
@Override
public String toString() {
return "UserMap{" +
"name_new='" + name + ''' +
", meta=" + meta.keySet().stream().map(x-> x+ ":: "+ meta.get(x)).collect(Collectors.joining(", ")) +
'}';
}
}
If you run, the output would be :
SOUT: UserMap{name_new='Deepak', meta=address:: Singapore, id:: 111}
Jackson: {"name":"Deepak","address":"Singapore","id":"111"}
I am using Springboot which internally uses jackson serializer. It converts the newUser object to normal user class again. I want to serialize string in the way class constructed. I want the output in SOUT format.
java json spring-boot jackson objectmapper
add a comment |
up vote
1
down vote
favorite
For some use case, I need to convert one POJO to another POJO with the different fields name. I tried using Jackson object mapper. It worked in some extends. However end result is not what I expected.
public class JacksonTest {
public static void main(String args) throws IOException{
ObjectMapper mapper = new ObjectMapper();
User user = new User("Deepak", "111", "Singapore");
UserMap newUser = mapper.convertValue(user, UserMap.class);
System.out.println("SOUT: " + newUser);
System.out.println("Jackson: " + mapper.writeValueAsString(newUser));
}
}
class User {
User(String name, String id, String address){
this.name = name;
this.id = id;
this.address = address;
}
String name;
String id;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
class UserMap implements Serializable {
@JsonProperty("name")
String name;
private Map<String, Object> meta = new HashMap<>();
@JsonAnyGetter
public Map<String, Object> any() {
return meta;
}
@JsonAnySetter
public void set(String name, Object value) {
meta.put(name, value);
}
@Override
public String toString() {
return "UserMap{" +
"name_new='" + name + ''' +
", meta=" + meta.keySet().stream().map(x-> x+ ":: "+ meta.get(x)).collect(Collectors.joining(", ")) +
'}';
}
}
If you run, the output would be :
SOUT: UserMap{name_new='Deepak', meta=address:: Singapore, id:: 111}
Jackson: {"name":"Deepak","address":"Singapore","id":"111"}
I am using Springboot which internally uses jackson serializer. It converts the newUser object to normal user class again. I want to serialize string in the way class constructed. I want the output in SOUT format.
java json spring-boot jackson objectmapper
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
For some use case, I need to convert one POJO to another POJO with the different fields name. I tried using Jackson object mapper. It worked in some extends. However end result is not what I expected.
public class JacksonTest {
public static void main(String args) throws IOException{
ObjectMapper mapper = new ObjectMapper();
User user = new User("Deepak", "111", "Singapore");
UserMap newUser = mapper.convertValue(user, UserMap.class);
System.out.println("SOUT: " + newUser);
System.out.println("Jackson: " + mapper.writeValueAsString(newUser));
}
}
class User {
User(String name, String id, String address){
this.name = name;
this.id = id;
this.address = address;
}
String name;
String id;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
class UserMap implements Serializable {
@JsonProperty("name")
String name;
private Map<String, Object> meta = new HashMap<>();
@JsonAnyGetter
public Map<String, Object> any() {
return meta;
}
@JsonAnySetter
public void set(String name, Object value) {
meta.put(name, value);
}
@Override
public String toString() {
return "UserMap{" +
"name_new='" + name + ''' +
", meta=" + meta.keySet().stream().map(x-> x+ ":: "+ meta.get(x)).collect(Collectors.joining(", ")) +
'}';
}
}
If you run, the output would be :
SOUT: UserMap{name_new='Deepak', meta=address:: Singapore, id:: 111}
Jackson: {"name":"Deepak","address":"Singapore","id":"111"}
I am using Springboot which internally uses jackson serializer. It converts the newUser object to normal user class again. I want to serialize string in the way class constructed. I want the output in SOUT format.
java json spring-boot jackson objectmapper
For some use case, I need to convert one POJO to another POJO with the different fields name. I tried using Jackson object mapper. It worked in some extends. However end result is not what I expected.
public class JacksonTest {
public static void main(String args) throws IOException{
ObjectMapper mapper = new ObjectMapper();
User user = new User("Deepak", "111", "Singapore");
UserMap newUser = mapper.convertValue(user, UserMap.class);
System.out.println("SOUT: " + newUser);
System.out.println("Jackson: " + mapper.writeValueAsString(newUser));
}
}
class User {
User(String name, String id, String address){
this.name = name;
this.id = id;
this.address = address;
}
String name;
String id;
String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
class UserMap implements Serializable {
@JsonProperty("name")
String name;
private Map<String, Object> meta = new HashMap<>();
@JsonAnyGetter
public Map<String, Object> any() {
return meta;
}
@JsonAnySetter
public void set(String name, Object value) {
meta.put(name, value);
}
@Override
public String toString() {
return "UserMap{" +
"name_new='" + name + ''' +
", meta=" + meta.keySet().stream().map(x-> x+ ":: "+ meta.get(x)).collect(Collectors.joining(", ")) +
'}';
}
}
If you run, the output would be :
SOUT: UserMap{name_new='Deepak', meta=address:: Singapore, id:: 111}
Jackson: {"name":"Deepak","address":"Singapore","id":"111"}
I am using Springboot which internally uses jackson serializer. It converts the newUser object to normal user class again. I want to serialize string in the way class constructed. I want the output in SOUT format.
java json spring-boot jackson objectmapper
java json spring-boot jackson objectmapper
edited May 30 '17 at 9:21
R. Zagórski
13.7k23463
13.7k23463
asked May 30 '17 at 7:21
xdeepakv
646179
646179
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I think you misunderstood what the @JsonAnyGetter/@JsonAnySetter
pair will, in effect, do.
It allows you to create a almost dynamic bean, with mandatory as well as voluntary fields. In your case, the name would be mandatory, and all other fields voluntary.
What goes on under the hood is not that your UserMap
gets converted to a User
. What you see is a serialized UserMap, but since it has the same fields and values as the corresponding User
instance, their serialized forms look identical.
I understand how its working. For some requirement, i need the way is to construct object class. Right now i am creating a POJO and feel all these data before serialization.
– xdeepakv
May 31 '17 at 9:04
@DeepakSharma I don't understand what you mean by " i need the way is to construct object class". Also, what is wrong with creating a POJO and filling in the data before serializing it?
– Dave
May 31 '17 at 12:16
add a comment |
up vote
0
down vote
I couldn't get the auto serialization and deserialization to work using the default Spring boot beans. In the end, this worked well for me after including Project Lombok and apache BeanUtils:
@ToString() @Getter() @Setter() @NoArgsConstructor()
public class User {
private String email;
private String bio;
private String image;
private String displayName;
private String userId;
private long lat;
private long lng;
public User(String json) throws JsonParseException, JsonMappingException, IOException, IllegalAccessException, InvocationTargetException {
ObjectMapper om = new ObjectMapper();
User u = om.readValue(json, User.class);
BeanUtils.copyProperties(this, u);
}
}
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
https://projectlombok.org/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f44255956%2fspring-boot-jackson-json-issue-while-serializing-and-deserializing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I think you misunderstood what the @JsonAnyGetter/@JsonAnySetter
pair will, in effect, do.
It allows you to create a almost dynamic bean, with mandatory as well as voluntary fields. In your case, the name would be mandatory, and all other fields voluntary.
What goes on under the hood is not that your UserMap
gets converted to a User
. What you see is a serialized UserMap, but since it has the same fields and values as the corresponding User
instance, their serialized forms look identical.
I understand how its working. For some requirement, i need the way is to construct object class. Right now i am creating a POJO and feel all these data before serialization.
– xdeepakv
May 31 '17 at 9:04
@DeepakSharma I don't understand what you mean by " i need the way is to construct object class". Also, what is wrong with creating a POJO and filling in the data before serializing it?
– Dave
May 31 '17 at 12:16
add a comment |
up vote
0
down vote
I think you misunderstood what the @JsonAnyGetter/@JsonAnySetter
pair will, in effect, do.
It allows you to create a almost dynamic bean, with mandatory as well as voluntary fields. In your case, the name would be mandatory, and all other fields voluntary.
What goes on under the hood is not that your UserMap
gets converted to a User
. What you see is a serialized UserMap, but since it has the same fields and values as the corresponding User
instance, their serialized forms look identical.
I understand how its working. For some requirement, i need the way is to construct object class. Right now i am creating a POJO and feel all these data before serialization.
– xdeepakv
May 31 '17 at 9:04
@DeepakSharma I don't understand what you mean by " i need the way is to construct object class". Also, what is wrong with creating a POJO and filling in the data before serializing it?
– Dave
May 31 '17 at 12:16
add a comment |
up vote
0
down vote
up vote
0
down vote
I think you misunderstood what the @JsonAnyGetter/@JsonAnySetter
pair will, in effect, do.
It allows you to create a almost dynamic bean, with mandatory as well as voluntary fields. In your case, the name would be mandatory, and all other fields voluntary.
What goes on under the hood is not that your UserMap
gets converted to a User
. What you see is a serialized UserMap, but since it has the same fields and values as the corresponding User
instance, their serialized forms look identical.
I think you misunderstood what the @JsonAnyGetter/@JsonAnySetter
pair will, in effect, do.
It allows you to create a almost dynamic bean, with mandatory as well as voluntary fields. In your case, the name would be mandatory, and all other fields voluntary.
What goes on under the hood is not that your UserMap
gets converted to a User
. What you see is a serialized UserMap, but since it has the same fields and values as the corresponding User
instance, their serialized forms look identical.
answered May 30 '17 at 8:43
Dave
1,23721427
1,23721427
I understand how its working. For some requirement, i need the way is to construct object class. Right now i am creating a POJO and feel all these data before serialization.
– xdeepakv
May 31 '17 at 9:04
@DeepakSharma I don't understand what you mean by " i need the way is to construct object class". Also, what is wrong with creating a POJO and filling in the data before serializing it?
– Dave
May 31 '17 at 12:16
add a comment |
I understand how its working. For some requirement, i need the way is to construct object class. Right now i am creating a POJO and feel all these data before serialization.
– xdeepakv
May 31 '17 at 9:04
@DeepakSharma I don't understand what you mean by " i need the way is to construct object class". Also, what is wrong with creating a POJO and filling in the data before serializing it?
– Dave
May 31 '17 at 12:16
I understand how its working. For some requirement, i need the way is to construct object class. Right now i am creating a POJO and feel all these data before serialization.
– xdeepakv
May 31 '17 at 9:04
I understand how its working. For some requirement, i need the way is to construct object class. Right now i am creating a POJO and feel all these data before serialization.
– xdeepakv
May 31 '17 at 9:04
@DeepakSharma I don't understand what you mean by " i need the way is to construct object class". Also, what is wrong with creating a POJO and filling in the data before serializing it?
– Dave
May 31 '17 at 12:16
@DeepakSharma I don't understand what you mean by " i need the way is to construct object class". Also, what is wrong with creating a POJO and filling in the data before serializing it?
– Dave
May 31 '17 at 12:16
add a comment |
up vote
0
down vote
I couldn't get the auto serialization and deserialization to work using the default Spring boot beans. In the end, this worked well for me after including Project Lombok and apache BeanUtils:
@ToString() @Getter() @Setter() @NoArgsConstructor()
public class User {
private String email;
private String bio;
private String image;
private String displayName;
private String userId;
private long lat;
private long lng;
public User(String json) throws JsonParseException, JsonMappingException, IOException, IllegalAccessException, InvocationTargetException {
ObjectMapper om = new ObjectMapper();
User u = om.readValue(json, User.class);
BeanUtils.copyProperties(this, u);
}
}
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
https://projectlombok.org/
add a comment |
up vote
0
down vote
I couldn't get the auto serialization and deserialization to work using the default Spring boot beans. In the end, this worked well for me after including Project Lombok and apache BeanUtils:
@ToString() @Getter() @Setter() @NoArgsConstructor()
public class User {
private String email;
private String bio;
private String image;
private String displayName;
private String userId;
private long lat;
private long lng;
public User(String json) throws JsonParseException, JsonMappingException, IOException, IllegalAccessException, InvocationTargetException {
ObjectMapper om = new ObjectMapper();
User u = om.readValue(json, User.class);
BeanUtils.copyProperties(this, u);
}
}
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
https://projectlombok.org/
add a comment |
up vote
0
down vote
up vote
0
down vote
I couldn't get the auto serialization and deserialization to work using the default Spring boot beans. In the end, this worked well for me after including Project Lombok and apache BeanUtils:
@ToString() @Getter() @Setter() @NoArgsConstructor()
public class User {
private String email;
private String bio;
private String image;
private String displayName;
private String userId;
private long lat;
private long lng;
public User(String json) throws JsonParseException, JsonMappingException, IOException, IllegalAccessException, InvocationTargetException {
ObjectMapper om = new ObjectMapper();
User u = om.readValue(json, User.class);
BeanUtils.copyProperties(this, u);
}
}
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
https://projectlombok.org/
I couldn't get the auto serialization and deserialization to work using the default Spring boot beans. In the end, this worked well for me after including Project Lombok and apache BeanUtils:
@ToString() @Getter() @Setter() @NoArgsConstructor()
public class User {
private String email;
private String bio;
private String image;
private String displayName;
private String userId;
private long lat;
private long lng;
public User(String json) throws JsonParseException, JsonMappingException, IOException, IllegalAccessException, InvocationTargetException {
ObjectMapper om = new ObjectMapper();
User u = om.readValue(json, User.class);
BeanUtils.copyProperties(this, u);
}
}
http://commons.apache.org/proper/commons-beanutils/download_beanutils.cgi
https://projectlombok.org/
answered Nov 10 at 10:25
Hypothetical inthe Clavicle
2,82011634
2,82011634
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%2f44255956%2fspring-boot-jackson-json-issue-while-serializing-and-deserializing%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