BeginnerVB_Net
asked on
jackson set serialization using object mapper
Lets say, I have below classes.
@JsonRootName(value = "car")
public class car {
private String make;
private String model;
//getters and setters
}
public class Automobile {
private String color;
private String type;
private Set<Car> cars = new HashSet<>();
//getters and setters
}
When I serialize Automobile using below code
ObjectMapper mapper = new ObjectMapper();
mapper.enable(Serializatio nFeature.W RAP_ROOT_V ALUE);
mapper.writeValueAsString( Automobile );
I am getting the output
{
“Automobile”:
{
“color”: “red”,
“type”: “four wheel”,
“cars”:[{“make”:”Honda”,”m odel”:”cry ”}]
}
}
But I want something like below
{
“color”: “red”,
“type”: “four wheel”,
“cars”: {
“car”: [{“make”:”Honda”,”model”:” cry”}]
}
}
How can i use jackson annotations so that i can get the output i want
@JsonRootName(value = "car")
public class car {
private String make;
private String model;
//getters and setters
}
public class Automobile {
private String color;
private String type;
private Set<Car> cars = new HashSet<>();
//getters and setters
}
When I serialize Automobile using below code
ObjectMapper mapper = new ObjectMapper();
mapper.enable(Serializatio
mapper.writeValueAsString(
I am getting the output
{
“Automobile”:
{
“color”: “red”,
“type”: “four wheel”,
“cars”:[{“make”:”Honda”,”m
}
}
But I want something like below
{
“color”: “red”,
“type”: “four wheel”,
“cars”: {
“car”: [{“make”:”Honda”,”model”:”
}
}
How can i use jackson annotations so that i can get the output i want
What's the difference between Automobile and car (that should be Car in Java btw) - i thought they were synonyms?
ASKER
i was just giving a sample. i couldn't come up with a better name :)
You can certainly make the output as you have stated, but I am not sure it is exactly how you would want it. So before we go too far down a wrong path, can you give us the required example output if there were more than one "Car" object in the cars Set?
ASKER
For multiple cars, output should look like below
{
“color”: “red”,
“type”: “four wheel”,
“cars”: {
“car”: [{“make”:”Honda”,”model”:” crv”}, {“make”:”Toyota”,”model”:” carolla”} ]
}
}
{
“color”: “red”,
“type”: “four wheel”,
“cars”: {
“car”: [{“make”:”Honda”,”model”:”
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I was looking for some other solution through annotations but what i finally ended up doing seems to be the right thing to do