@Test public void should_only_include_primitives_strings_mappables_and_collections_of_mappable() throws MalformedURLException { TestMappable tm = new TestMappable(); Map<String, Object> map = tm.toMap(); String expected = "" + "{\n" + " \"an_int\":1,\n" + " \"a_long\":2,\n" + " \"a_string\":\"3\",\n" + " \"a_mappable\":{\n" + " \"another_int\":4,\n" + " \"a_byte\":5\n" + " },\n" + " \"a_short_list\":[\n" + " 4,\n" + " 5,\n" + " 6\n" + " ],\n" + " \"a_mappable_list\":[\n" + " {\n" + " \"another_int\":4,\n" + " \"a_byte\":5\n" + " }\n" + " ]\n" + "}\n"; Gson gson = new Gson(); String actual = gson.toJson(map); assertEquals(gson.fromJson(new StringReader(expected), Map.class), gson.fromJson(new StringReader(actual), Map.class)); }
public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<String, Object>(); List<Field> mappableFields = getMappableFields(); for (Field field : mappableFields) { Object value; value = getValue(field); if (value != null && Mappable.class.isAssignableFrom(value.getClass())) { value = ((Mappable) value).toMap(); } if (value != null && Collection.class.isAssignableFrom(value.getClass())) { List<Object> mappedValue = new ArrayList<Object>(); for (Object o : (Collection) value) { if (Mappable.class.isAssignableFrom(o.getClass())) { mappedValue.add(((Mappable) o).toMap()); } else { mappedValue.add(o); } } value = mappedValue; } if (value != null && !Collections.EMPTY_LIST.equals(value) && !NO_LINE.equals(value)) { map.put(field.getName(), value); } } return map; }
Mappable implements Serializable { public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<String, Object>(); List<Field> mappableFields = getMappableFields(); for (Field field : mappableFields) { Object value; value = getValue(field); if (value != null && Mappable.class.isAssignableFrom(value.getClass())) { value = ((Mappable) value).toMap(); } if (value != null && Collection.class.isAssignableFrom(value.getClass())) { List<Object> mappedValue = new ArrayList<Object>(); for (Object o : (Collection) value) { if (Mappable.class.isAssignableFrom(o.getClass())) { mappedValue.add(((Mappable) o).toMap()); } else { mappedValue.add(o); } } value = mappedValue; } if (value != null && !Collections.EMPTY_LIST.equals(value) && !NO_LINE.equals(value)) { map.put(field.getName(), value); } } return map; } }
Mappable implements Serializable { public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<String, Object>(); List<Field> mappableFields = getMappableFields(); for (Field field : mappableFields) { Object value; value = getValue(field); if (value != null && Mappable.class.isAssignableFrom(value.getClass())) { value = ((Mappable) value).toMap(); } if (value != null && Collection.class.isAssignableFrom(value.getClass())) { List<Object> mappedValue = new ArrayList<Object>(); for (Object o : (Collection) value) { if (Mappable.class.isAssignableFrom(o.getClass())) { mappedValue.add(((Mappable) o).toMap()); } else { mappedValue.add(o); } } value = mappedValue; } if (value != null && !Collections.EMPTY_LIST.equals(value) && !NO_LINE.equals(value)) { map.put(field.getName(), value); } } return map; }  }
Mappable implements Serializable { public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<String, Object>(); List<Field> mappableFields = getMappableFields(); for (Field field : mappableFields) { Object value; value = getValue(field); if (value != null && Mappable.class.isAssignableFrom(value.getClass())) { value = ((Mappable) value).toMap(); } if (value != null && Collection.class.isAssignableFrom(value.getClass())) { List<Object> mappedValue = new ArrayList<Object>(); for (Object o : (Collection) value) { if (Mappable.class.isAssignableFrom(o.getClass())) { mappedValue.add(((Mappable) o).toMap()); } else { mappedValue.add(o); } } value = mappedValue; } if (value != null && !Collections.EMPTY_LIST.equals(value) && !NO_LINE.equals(value)) { map.put(field.getName(), value); } } return map; }  Map<String, Object> toMap(); }
Mappable implements Serializable { public Map<String, Object> toMap() { Map<String, Object> map = new HashMap<String, Object>(); List<Field> mappableFields = getMappableFields(); for (Field field : mappableFields) { Object value; value = getValue(field); if (value != null && Mappable.class.isAssignableFrom(value.getClass())) { value = ((Mappable) value).toMap(); } if (value != null && Collection.class.isAssignableFrom(value.getClass())) { List<Object> mappedValue = new ArrayList<Object>(); for (Object o : (Collection) value) { if (Mappable.class.isAssignableFrom(o.getClass())) { mappedValue.add(((Mappable) o).toMap()); } else { mappedValue.add(o); } } value = mappedValue; } if (value != null && !Collections.EMPTY_LIST.equals(value) && !NO_LINE.equals(value)) { map.put(field.getName(), value); } } return map; }  Map<String, Object> toMap();  }