Convert List to JSON (#14226)
Hello there, The code provides a test class for a Java list to JSON conversion. Regards, Mohamed.
This commit is contained in:
parent
643f99281f
commit
e91d7301bd
|
@ -0,0 +1,35 @@
|
||||||
|
package com.baeldung.listtojson;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import org.json.JSONArray;
|
||||||
|
import org.junit.Assert;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import org.junit.Test;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ListToJsonArrayUnitTest {
|
||||||
|
public List<String> list = Arrays.asList("Article 1", "Article 2", "Article 3");
|
||||||
|
public String expectedJsonArray = "[\"Article 1\",\"Article 2\",\"Article 3\"]";
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void given_JavaList_whenUsingJacksonLibrary_thenOutJsonArray() throws JsonProcessingException {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
String jsonArray = objectMapper.writeValueAsString(list);
|
||||||
|
Assert.assertEquals(expectedJsonArray, jsonArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void given_JavaList_whenUsingGsonLibrary_thenOutJsonArray() {
|
||||||
|
Gson gson = new Gson();
|
||||||
|
String jsonArray = gson.toJson(list);
|
||||||
|
Assert.assertEquals(expectedJsonArray, jsonArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void given_JavaList_whenOrgJsonLibrary_thenOutJsonAray() {
|
||||||
|
JSONArray jsonArray = new JSONArray(list);
|
||||||
|
Assert.assertEquals(expectedJsonArray, jsonArray.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue