Closed aufZone closed 10 years ago
When serialized as JSON, no additional wrapping is added, regardless of existence of @JsonRootName
, unless explicitly defined to add wrapping. So you need to enable WRAP_ROOT_VALUE
either globally:
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
or for specific call using ObjectWriter
mapper.writer().with(SerializationFeature.WRAP_ROOT_VALUE).writeValue(...);
It worked !!
Latest output:
{
"Person" : {
"name" : "Ali Bin Baba",
"age" : 12,
"date" : "06-13-2014",
"Address" : {
"postcode" : "90121",
"city" : "Vila",
"state" : "Belgium",
"street" : "Jln Koli"
}
}
}
This my writer:
public class Json {
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private ObjectMapper mapper = new ObjectMapper();
public Json(Object obj) throws JsonGenerationException,
JsonMappingException, IOException {
mapper.writerWithDefaultPrettyPrinter().with(SerializationFeature.WRAP_ROOT_VALUE).writeValue(baos, obj);
}
public int size() {
return baos.size();
}
public String toString() {
byte[] bytes = baos.toByteArray();
return new String(bytes);
}
}
Thank you very much!!
Anyway, I have tried put that object into List like this:
Address a = new Address("Jln Koli", "90121", "Vila", "Belgium");
Person p = new Person("Ali Bin Baba", new Date(), 90.0, 12, a);
List<Person> persons = new LinkedList<>();
persons.add(p);
persons.add(p);
Json json = new Json(persons);
System.out.println(json.toString());
The output is like this:
{
"LinkedList" : [ {
"name" : "Ali Bin Baba",
"age" : 12,
"date" : "06-13-2014",
"Address" : {
"postcode" : "90121",
"city" : "Vila",
"state" : "Belgium",
"street" : "Jln Koli"
}
}, {
"name" : "Ali Bin Baba",
"age" : 12,
"date" : "06-13-2014",
"Address" : {
"postcode" : "90121",
"city" : "Vila",
"state" : "Belgium",
"street" : "Jln Koli"
}
} ]
}
My question is, how to change root name "LinkedList" into "Person" ??
Two ways to go; either use something like:
@JsonRootName("Person")
public class PersonList extends ArrayList<Person> { }
to specify root name for your List
subtype.
Or use method in ObjectWriter
to use whatever name you want for object:
ObjectWriter w = ...;
w.withRootName("Person").writeValueAsString(list);
Thank you. I'm use the first way, and it work!! Anyway, I'm also tried the second way, but it not work. This is my writer. What your suggestion?
public class Json {
private ByteArrayOutputStream baos = new ByteArrayOutputStream();
private ObjectMapper mapper = new ObjectMapper();
public Json(Object obj) throws JsonGenerationException,
JsonMappingException, IOException {
mapper.writerWithDefaultPrettyPrinter()
.with(SerializationFeature.WRAP_ROOT_VALUE)
.writeValue(baos, obj);
}
public int size() {
return baos.size();
}
public String toString() {
byte[] bytes = baos.toByteArray();
return new String(bytes);
}
}
You are not using "withRootName("Person")" in there.
And from now on, let's use mailing lists for questions, discussions. Issue trackers are for bugs.
This is Person.java
This is my main class:
Output:
But, the output I want is like this:
My dependencies:
Did you know, which part is wrong??