RegimenItemAdapter implements JsonSerializer<RegimenItem>, JsonDeserializer<RegimenItem> { @Override public JsonElement serialize(RegimenItem src, Type typeOfSrc, JsonSerializationContext context) { JsonParser jsonParser = new JsonParser(); JsonObject result = jsonParser.parse(gson.toJson(src.getRegimen())).getAsJsonObject(); result.addProperty("patientsOnTreatment", src.getAmount()); result.addProperty("hf", src.getHf()); result.addProperty("chw", src.getChw()); result.addProperty("comunitaryPharmacy", src.getPharmacy()); return result; } RegimenItemAdapter(); @Override RegimenItem deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context); @Override JsonElement serialize(RegimenItem src, Type typeOfSrc, JsonSerializationContext context); @Inject
    public RegimenRepository regimenRepository; }
@Test public void shouldSerializeRegimenItem() throws Exception { RegimenItem regimenItem = new RegimenItem(); Regimen regimen = new Regimen(); regimen.setCode("regimenCode"); regimen.setName("regimenName"); regimen.setType(Regimen.RegimeType.Adults); regimenItem.setAmount(100L); regimenItem.setRegimen(regimen); RegimenItemAdapter regimenItemAdapter = new RegimenItemAdapter(); JsonElement json = regimenItemAdapter.serialize(regimenItem, null, null); assertNotNull(json); assertThat(json.getAsJsonObject().get("categoryName").getAsString(), is("Adults")); }