Json { public static String[] jsonArrayToStringArray(String jsonString) { jsonString = "{key:" + jsonString + "}"; try { JSONObject jsonObject = (JSONObject) new JSONTokener(jsonString).nextValue(); JSONArray jsonArray = jsonObject.getJSONArray("key"); String[] result = new String[jsonArray.length()]; for (int i = 0; i < result.length; i++) { result[i] = jsonArray.getString(i); } return result; } catch (JSONException exception) { return new String[0]; } }  static Map<String, String> jsonStringToMap(String jsonParams); static String mapToJsonString(Map<String, String> map); static String[] jsonArrayToStringArray(String jsonString); @SuppressWarnings("unused") static T getJsonValue(final JSONObject jsonObject, final String key, final Class<T> valueClass);  }
@Test public void jsonArrayToStringArray_withMultipleValidParameters_shouldReturnCorrespondingStringArray() throws Exception { String jsonString = "[\"hi\",\"dog\",\"goat\"]"; String[] expected = {"hi", "dog", "goat"}; assertThat(Json.jsonArrayToStringArray(jsonString)).isEqualTo(expected); }
@Test public void jsonArrayToStringArray_withMultipleValidParameters_withSingleQuotes_shouldReturnCorrespondingStringArray() throws Exception { String jsonString = "['hi','dog','goat']"; String[] expected = {"hi", "dog", "goat"}; assertThat(Json.jsonArrayToStringArray(jsonString)).isEqualTo(expected); }
@Test public void jsonArrayToStringArray_withMultipleValidParameters_withNoQuotes_shouldReturnCorrespondingStringArray() throws Exception { String jsonString = "[hi,dog,goat]"; String[] expected = {"hi", "dog", "goat"}; assertThat(Json.jsonArrayToStringArray(jsonString)).isEqualTo(expected); }
@Test public void jsonArrayToStringArray_withNullInput_shouldReturnEmptyStringArray() throws Exception { String[] result = Json.jsonArrayToStringArray(null); assertThat(result).isNotNull(); assertThat(result).isEmpty(); }
@Test public void jsonArrayToStringArray_withEmptyJsonArray_shouldReturnEmptyStringArray() throws Exception { String[] result = Json.jsonArrayToStringArray("[]"); assertThat(result).isNotNull(); assertThat(result).isEmpty(); }
@Test public void jsonArrayToStringArray_withEmptyString_shouldReturnEmptyStringArray() throws Exception { String[] result = Json.jsonArrayToStringArray(""); assertThat(result).isNotNull(); assertThat(result).isEmpty(); }
@Test public void jsonArrayToStringArray_withMalformedMalicousString_shouldReturnEmptyStringArray() throws Exception { String[] result = Json.jsonArrayToStringArray("} die"); assertThat(result).isNotNull(); assertThat(result).isEmpty(); }
@Test public void jsonArrayToStringArray_whenMalformed_shouldReturnEmptyStringArray() throws Exception { String jsonString = "[cool,guy,crew"; String[] result = Json.jsonArrayToStringArray(jsonString); assertThat(result).isNotNull(); assertThat(result).isEmpty(); }
@Test public void jsonArrayToStringArray_withLotsOfEmptySpace_shouldReturnStringArrayWithoutSpaces() throws Exception { String jsonString = " [ \" hi\",\"do g\",\"goat \"]"; String[] expected = {" hi", "do g", "goat "}; String[] result = Json.jsonArrayToStringArray(jsonString); assertThat(result).isEqualTo(expected); }