I am a beginner of Jackson. How can I create a JSON message like this using Java?
{
"name": "John",
"age": "40",
"family": {
"parents_name": [
"David",
"Susan"
],
"children": "yes",
"children_names": [
"Peter",
"Mary"
]
}
}
The easiest way to do this for a beginner is to eliminate unnecessary nesting and rely on Jackson's default object binding.
You would create a class like this:
public class Person {
private String name;
private int age;
private List<String> parentNames;
private List<String> childrenNames;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<String> getParentNames() {
return parentNames;
}
public void setParentNames(List<String> parentNames) {
this.parentNames = parentNames;
}
public List<String> getChildrenNames() {
return childrenNames;
}
public void setChildrenNames(List<String> childrenNames) {
this.childrenNames = childrenNames;
}
}
Then you can instantiate a Person
from JSON like this:
Person p = ObjectMapper.readValue(jsonString, Person.class);
Note that the JSON you have in your example won't work with this object for three reasons:
Person
class has no Family
object. I felt that adds unnecessary complexity. If you want that, create a separate Family
class, and Person
would contain a Family
member (no pun intended). children
because that can be deduced from the length of the childrenNames
list. childrenNames
and parentNames
rather than children_names
and parents_name
. If you want those, add @JsonProperty
with the desired property names on the getters and setters for those values. Create a Person class in Java, with properties such as getName(), getAge() and so on. Then Jackson can create that JSON for you automatically, from your Person object.
I gather from your comments to Vidya's solution that your looking for more flexibility than you get can get with the default binding.
Jackson allows you to create your own custom serializer. For example:
public class Person {
private String name;
private int age;
private List<String> parentsName;
private List<String> childrenNames;
public Person(String name, List<String> parentsName) {
this(name, parentsName, -1, Collections.<String>emptyList());
}
public Person(String name, List<String> parentsName, int age) {
this(name, parentsName, age, Collections.<String>emptyList());
}
public Person(String name, List<String> parentsName, int age, List<String> childrenNames) {
this.name = name;
this.age = age;
this.parentsName = parentsName;
this.childrenNames = childrenNames;
}
private void serialize(JsonGenerator generator, SerializerProvider arg2) throws IOException {
generator.writeStartObject();
generator.writeObjectField("name", name);
if (age >= 0)
generator.writeNumberField("age", age);
// start family subset
generator.writeObjectFieldStart("family");
generator.writeArrayFieldStart("parents_name");
for (String parent : parentsName) {
generator.writeObject(parent);
}
generator.writeEndArray();
generator.writeObjectField("children", (childrenNames.isEmpty() ? "no" : "yes"));
generator.writeArrayFieldStart("children_names");
for (String child : childrenNames) {
generator.writeObject(child);
}
generator.writeEndArray();
generator.writeEndObject();
// end family subset
generator.writeEndObject();
}
public static JsonSerializer<Person> createJsonSerializer() {
return new JsonSerializer<Person>() {
@Override
public void serialize(Person me, JsonGenerator generator, SerializerProvider arg2) throws IOException, JsonProcessingException {
me.serialize(generator, arg2);
}
};
}
public static void main(String[] args) throws IOException {
List<String> parentsName = Arrays.<String>asList("David", "Susan");
List<String> childrenNames = Arrays.<String>asList("Peter", "Mary");
Person person = new Person("John", parentsName, 40, childrenNames);
ObjectMapper mapper = new ObjectMapper();
SimpleModule simpleModule = new SimpleModule("PersonModule", new Version(1, 0, 0, null));
simpleModule.addSerializer(Person.class, Person.createJsonSerializer());
// pretty output for debugging
mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
mapper.registerModule(simpleModule);
System.out.println("Person json: ");
System.out.println(mapper.writeValueAsString(person));
}
}
This gives you increased flexibility in two ways:
You can apply conditional logic in serialization
You can have multiple custom serializers
The downsides are fairly obvious
More complicated
More time to implement. The default bindings were almost free. This solution is not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.