Reputation: 93
The class I need to Serialize:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class MyClass {
@JsonProperty("CustomerId")
private String customerId;
@JsonProperty("Products")
private List<ProductDetails> products;
//Getters and setters
}
My ProductDetails.java class:
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class ProductDetails {
@JsonProperty("ProductId")
private String productId;
@JsonProperty("ProductName")
private String productName;
//Getters and setters
}
The default serialized output:
{
"CustomerId" : "ewm0po",
"Products" : [ {
"ProductId" : "AAA",
"ProductName" : "AAA Product"
}, {
"ProductId" : "AAA",
"ProductName" : "AAA Product"
}]
}
The output I'm trying to get:
{
"CustomerId" : "ewm0po",
"ProductId1" : "AAA",
"ProductName1" : "AAA Product"
"ProductId2" : "AAA",
"ProductName2" : "AAA Product"
}
In other words, I am trying to skip the JSON brackets for the Products-list and suffix each of the ProductId and ProductName fields with a increasing integer.
Any help is very much appreciated!
Upvotes: 2
Views: 1330
Reputation: 93
As @Boris the spider pointed out. Writing a custom serializer was the solution. Not as painful as I expected :-)
Upvotes: 2