jsonb-api
jsonb-api copied to clipboard
Simple way to 'flatten' ?
I have an Employee class that produces the following JSON:
{
"audit": {
"createdDate": "2019-03-17T21:07:57.019",
"updatedDate": "2019-03-17T21:07:57.019"
},
"id": 1,
"version": 1,
"firstName": "Mike",
"lastName": "Norman",
"salary": 5000
}
The audit element is from the JPA Embeddable class Audit which contains timestamp info for when the object is created and updated in the database
@Embeddable
public class Audit {
protected LocalDateTime createdDate;
protected LocalDateTime updatedDate;
public Audit() {
}
@Column(name = "CREATED_DATE")
public LocalDateTime getCreatedDate() {
return createdDate;
}
public void setCreatedDate(LocalDateTime createdDate) {
this.createdDate = createdDate;
}
@Column(name = "UPDATED_DATE")
public LocalDateTime getUpdatedDate() {
return updatedDate;
}
public void setUpdatedDate(LocalDateTime updatedDate) {
this.updatedDate = updatedDate;
}
}
I would like to 'flatten' the audit portion of the JSON:
{
"createdDate": "2019-03-17T21:07:57.019",
"updatedDate": "2019-03-17T21:07:57.019"
"id": 1,
"version": 1,
"firstName": "Mike",
"lastName": "Norman",
"salary": 5000
}
I have started down the road of JsonbSerializer but things are getting pretty complicated pretty quick. Is there a simpler way to accomplish this?
I know that Jackson had the @JsonUnwrapped annotation which seems wonderful right now ;-)
A JsonbAdapter<Employee, Map<String,Object>> should be easier to implement than a JsonbSerializer, but there is currently no automated way to unwrap nested values based on annotation/configuration.