HalSerializer implements JsonSerializer<HalResource> { protected void readResource(Object value, String resName, Map<String, List<HalResource>> embedded, Set<String> embeddedLists) { if (value == null) { return; } if (value instanceof HalResource) { List<HalResource> list = embedded.get(resName); if (list == null) { list = new ArrayList<>(); embedded.put(resName, list); } list.add((HalResource) value); } else if (value instanceof Collection) { embeddedLists.add(resName); for (Object v : ((Collection) value)) { readResource(v, resName, embedded, embeddedLists); } } }  @Override JsonElement serialize(HalResource t, Type type, JsonSerializationContext jsc);  }
@Test public void testReadResource() { Map<String, List<HalResource>> embedded = new HashMap<>(); Set<String> embeddedLists = new HashSet<>(); TestResource res = new TestResource(); adapter.readResource(res, "test", embedded, embeddedLists); Assert.assertEquals(1, embedded.size()); List<HalResource> list = embedded.get("test"); Assert.assertNotNull(list); Assert.assertEquals(1, list.size()); Assert.assertTrue(embeddedLists.isEmpty()); }
@Test public void testReadResourceList() { Map<String, List<HalResource>> embedded = new HashMap<>(); Set<String> embeddedLists = new HashSet<>(); List<TestResource> res = new ArrayList<>(); res.add(new TestResource()); res.add(new TestResource()); adapter.readResource(res, "test", embedded, embeddedLists); Assert.assertEquals(1, embedded.size()); List<HalResource> list = embedded.get("test"); Assert.assertNotNull(list); Assert.assertEquals(2, list.size()); Assert.assertEquals(1, embeddedLists.size()); Assert.assertTrue(embeddedLists.contains("test")); }