@Test public void shouldReturnJsonForSpecifiedFieldsInVideo() throws Exception { String[] fields = new String[]{"id", "title", "length"}; Video video = new Video(2, "Godzilla Resurgence", 120, "Action & drama movie|", "Hideaki Anno", "Japanese"); String jsonFieldResponse = mapper.toJson(video, fields); String expectedDetails = "{\"id\": 2,\"title\": \"Godzilla Resurgence\",\"length\": 120}"; assertEquals(expectedDetails, jsonFieldResponse); }
public String toJson(Video video, String[] fields) throws Exception { StringBuilder json = new StringBuilder().append("{"); for (int i = 0, fieldsLength = fields.length; i < fieldsLength; i++) { json.append(getString(video, Video.class.getDeclaredField(fields[i]))); if (i != fieldsLength - 1) { json.append(","); } } json.append("}"); return json.toString(); }
FieldJsonMapper { public String toJson(Video video, String[] fields) throws Exception { StringBuilder json = new StringBuilder().append("{"); for (int i = 0, fieldsLength = fields.length; i < fieldsLength; i++) { json.append(getString(video, Video.class.getDeclaredField(fields[i]))); if (i != fieldsLength - 1) { json.append(","); } } json.append("}"); return json.toString(); } }
FieldJsonMapper { public String toJson(Video video, String[] fields) throws Exception { StringBuilder json = new StringBuilder().append("{"); for (int i = 0, fieldsLength = fields.length; i < fieldsLength; i++) { json.append(getString(video, Video.class.getDeclaredField(fields[i]))); if (i != fieldsLength - 1) { json.append(","); } } json.append("}"); return json.toString(); }  }
FieldJsonMapper { public String toJson(Video video, String[] fields) throws Exception { StringBuilder json = new StringBuilder().append("{"); for (int i = 0, fieldsLength = fields.length; i < fieldsLength; i++) { json.append(getString(video, Video.class.getDeclaredField(fields[i]))); if (i != fieldsLength - 1) { json.append(","); } } json.append("}"); return json.toString(); }  String toJson(Video video, String[] fields); }
FieldJsonMapper { public String toJson(Video video, String[] fields) throws Exception { StringBuilder json = new StringBuilder().append("{"); for (int i = 0, fieldsLength = fields.length; i < fieldsLength; i++) { json.append(getString(video, Video.class.getDeclaredField(fields[i]))); if (i != fieldsLength - 1) { json.append(","); } } json.append("}"); return json.toString(); }  String toJson(Video video, String[] fields);  }