Reputation: 175
I am using Jackson 2.10.1 library to convert my Java POJOs to JSON and I am getting the below output, I require the output without the POJO name(MyTestPojo here), I have tried various jackson annotations like @JsonIgnoreProperties but those are mostly for the members present in the POJO and not the POJO class name.
{
"MyTestPojo": [
{
"CreatedBy": "user1",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": true,
"Value": "testABC1",
"CreatedDate": "2017-03-13 15:41:54.0",
"Description": "testABC"
},
{
"CreatedBy": "user2",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": false,
"Value": "testABC2",
"CreatedDate": "2017-03-13 15:41:54.0",
"Description": "testABC"
}
]
}
whereas what I require is -
[
{
"CreatedBy": "user1",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": true,
"Value": "testABC1",
"CreatedDate": "2019-03-13 15:41:54.0",
"Description": "testABC"
},
{
"CreatedBy": "user2",
"Name": "testABC",
"UpdatedBy": null,
"UpdatedDate": null,
"IsActive": false,
"Value": "testABC2",
"CreatedDate": "2020-03-10 15:41:54.0",
"Description": "testABC"
}
]
}
Is there a way to handle this with Jackson annotations?
The POJOs that I have used are-
@JacksonXmlRootElement(localName = "ArrayOfTestPojos")
public class GetResponseVO {
@JsonProperty("MyTestPojo")
@JacksonXmlProperty(localName = "MyTestPojo")
@JacksonXmlElementWrapper(useWrapping = false)
private ArrayList<MyTestPojo> MyTestPojoList;
public ArrayList<MyTestPojo> getMyTestPojoList() {
return MyTestPojoList;
}
public void setMyTestPojoList(ArrayList<MyTestPojo> MyTestPojoList) {
this.MyTestPojoList = MyTestPojoList;
}
// standard getters and setters
}
and
@JacksonXmlRootElement(localName = "MyTestPojo")
public class MyTestPojo {
@JsonProperty("Name")
private String name;
@JsonProperty("Description")
private String description;
@JsonProperty("IsActive")
private int isActive;
@JsonProperty("Value")
private String value = null;
@JsonProperty("CreatedBy")
private String createdBy;
@JsonProperty("CreatedDate")
private String createdDate;
@JsonProperty("UpdatedBy")
private String updatedBy;
@JsonProperty("UpdatedDate")
private String updatedDate;
// standard getters and setters.
}
```````````
I am also generating the XML out of this so you can ignore the annotations relevant to XML.
Upvotes: 3
Views: 1507
Reputation: 2900
you can use JsonValue
annotation for that purpose which basically "use-value of this property instead of serializing the container object". it can be used on getters
also
@JsonValue indicates that results of the annotated "getter" method (which means signature must be that of getters; non-void return type, no args) is to be used as the single value to serialize for the instance. Usually value will be of a simple scalar type (String or Number), but it can be any serializable type (Collection, Map or Bean).
@JsonValue
@JacksonXmlProperty(localName = "MyTestPojo")
@JacksonXmlElementWrapper(useWrapping = false)
private ArrayList<MyTestPojo> MyTestPojoList;
But that would wrong practice as it will generate JSON like this, which would not be legal JSON
.
{[{"x":"value"}, ...]}
If you want to alter only JSON structure (without affecting xml), you can use MixIn
for that purpose.
public interface JsonMixin {
@JsonValue
List<MyTestPojo> getMyTestPojoList();
}
And register it with your object mapper and remove @JsonValue
from the main Class.
objectMapper.addMixIn(GetResponseVO.class, JsonMixin.class);
Upvotes: 3