I want to optimize the json data to be sent on wire. I have three models in my code. These are Customer, Invoice and Particular.
The Customer class
@Data
public class Customer implements Serializable {
private long customerId;
private String name;
private String taxId;
private String phone;
private String address;
private String emailId;
private Date created;
private List<Invoice> invoices;
}
The Invoice class
@Data
public class Invoice implements Serializable {
private String invoiceId;
private List<Particular> particulars;
private Date invoiceDate;
}
The Particular class
@Data
public class Particular {
private String item;
private int quantity;
private float tax;
private int unitPrice;
}
My test code:
@Test
public void makeCustomerJsonWithInvoices() throws JsonProcessingException {
Customer customer = new Customer();
customer.setCustomerId(1234);
customer.setName("Pawan");
customer.setPhone("+918989898989");
customer.setEmailId("something@something.com");
customer.setAddress("Mumbai, India");
customer.setTaxId("MQZ11DPS");
customer.setCreated(new Date());
Invoice invoice1 = new Invoice();
invoice1.setInvoiceId("A-1");
Particular particular1 = new Particular("abc", 1, 0, 12);
Particular particular2 = new Particular("xyz", 2, 0, 20);
invoice1.setInvoiceDate(new Date());
invoice1.setParticulars(Arrays.asList(particular1, particular2));
Particular particular3 = new Particular("mno", 2, 0, 15);
Invoice invoice2 = new Invoice();
invoice2.setInvoiceId("A-2");
invoice2.setParticulars(Arrays.asList(particular3));
invoice2.setInvoiceDate(new Date());
customer.setInvoices(Arrays.asList(invoice1, invoice2));
String value = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(customer);
System.out.println(value);
}
What I want here is to avoid the redundancy by serializing the Invoice so that the resulting json would be compact. This should be achieved by only sending the invoiceId attribute value instead of whole Invoice object json.
What the test code prints:
{
"customerId" : 1234,
"name" : "Pawan",
"taxId" : "MQZ11DPS",
"phone" : "+918989898989",
"address" : "Mumbai, India",
"emailId" : "something@something.com",
"created" : 1553243962038,
"invoices" : [ {
"invoiceId" : "A-1",
"particulars" : [ {
"item" : "abc",
"quantity" : 1,
"tax" : 0.0,
"unitPrice" : 12
}, {
"item" : "xyz",
"quantity" : 2,
"tax" : 0.0,
"unitPrice" : 20
} ],
"invoiceDate" : 1553243962038
}, {
"invoiceId" : "A-2",
"particulars" : [ {
"item" : "mno",
"quantity" : 2,
"tax" : 0.0,
"unitPrice" : 15
} ],
"invoiceDate" : 1553243962039
} ]
}
What I want it to print:
{
"customerId" : 1234,
"name" : "Pawan",
"taxId" : "MQZ11DPS",
"phone" : "+918989898989",
"address" : "Mumbai, India",
"emailId" : "something@something.com",
"created" : 1553243962038,
"invoices" : [ {
"invoiceId" : "A-1"
}, {
"invoiceId" : "A-2"
} ]
}
The @Data
is lombok annotation used to generate getters and setters.
I tried to add @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "invoiceId")
annotation to Invoice class but this doesn't change the output.
Note that I want this serialization with Invoice happen only when it is passed as a child to a container Model. If I want to send Invoice independently, it shall serialize all fields in Invoice model. I believe this is a common scenario while implementing RESTful WS.
Do I need to write customer serializer for this?