I’d just like to create the Jackson mapping equivalent of the below :
{\"isDone\": true}
I think I need to create a class like this :
public class Status {
private boolean isDone;
public boolean isDone{
return this.isDone;
}
public void setDone(boolean isDone){
this.isDone = isDone;
}
}
But how do I instatiate it and then write the JSON to a string ?
A problem with your example and Jackson is the default choices of JSON property names: Jackson will see
isDoneandsetDoneand choosedoneas the JSON property name. You can override this default choice using theJsonPropertyannotation:Then:
Now
jsonStringcontains{ "isDone" : true }. Note that you can also write the string to anOutputStreamusing ObjectMapper.writeValue(OutputStream, Object), or to aWriterusing ObjectMapper.writeValue(Writer, Object).In this case you really only need the
JsonPropertyannotation on either of your accessors, but not both. Just annotatingisDonewill get you the JSON property name that you want.An alternative to using the
JsonPropertyannotation is to rename your accessorssetIsDone/getIsDone. Then the annotations are unnecessary.See the quick and dirty Jackson tutorial: Jackson in 5 minutes. Understanding of the specific properties came from looking through the docs for Jackson annotations.