use of com.github.openjson.JSONObject in project triplea by triplea-game.
the class OpenJsonUtilsTest method toMap_ShouldConvertJsonArrayValueToList.
@Test
public void toMap_ShouldConvertJsonArrayValueToList() {
final JSONObject jsonObject = new JSONObject(ImmutableMap.of("name", new JSONArray(Arrays.asList(42))));
final Map<String, Object> properties = OpenJsonUtils.toMap(jsonObject);
assertThat(properties, is(ImmutableMap.of("name", Arrays.asList(42))));
}
use of com.github.openjson.JSONObject in project triplea by triplea-game.
the class OpenJsonUtilsTest method optEnum_ShouldThrowExceptionWhenPresentButNotEnumValue.
@Test
public void optEnum_ShouldThrowExceptionWhenPresentButNotEnumValue() {
final JSONObject jsonObject = new JSONObject(ImmutableMap.of("name", "unknown"));
assertThrows(IllegalArgumentException.class, () -> OpenJsonUtils.optEnum(jsonObject, TestEnum.class, "name", TestEnum.SECOND));
}
use of com.github.openjson.JSONObject in project triplea by triplea-game.
the class OpenJsonUtilsTest method toList_ShouldConvertNullSentinelValueToNull.
@Test
public void toList_ShouldConvertNullSentinelValueToNull() {
final JSONArray jsonArray = new JSONArray(Arrays.asList(JSONObject.NULL));
final List<Object> elements = OpenJsonUtils.toList(jsonArray);
// NB: need to test using reference equality because JSONObject#NULL#equals() is defined to be equal to null
assertThat(elements.get(0), is(nullValue()));
}
use of com.github.openjson.JSONObject in project triplea by triplea-game.
the class OpenJsonUtilsTest method optEnum_ShouldReturnDefaultValueWhenAbsent.
@Test
public void optEnum_ShouldReturnDefaultValueWhenAbsent() {
final JSONObject jsonObject = new JSONObject();
final TestEnum value = OpenJsonUtils.optEnum(jsonObject, TestEnum.class, "name", TestEnum.SECOND);
assertThat(value, is(TestEnum.SECOND));
}
use of com.github.openjson.JSONObject in project triplea by triplea-game.
the class OpenJsonUtilsTest method optEnum_ShouldReturnEnumValueWhenPresent.
@Test
public void optEnum_ShouldReturnEnumValueWhenPresent() {
final JSONObject jsonObject = new JSONObject(ImmutableMap.of("name", TestEnum.FIRST.toString()));
final TestEnum value = OpenJsonUtils.optEnum(jsonObject, TestEnum.class, "name", TestEnum.SECOND);
assertThat(value, is(TestEnum.FIRST));
}
Aggregations