use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.
the class TestForestJacksonConverter method testJavaObjectToMap.
@Test
public void testJavaObjectToMap() {
Coordinate coordinate = new Coordinate("11.11111", "22.22222");
ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
Map map = forestJacksonConverter.convertObjectToMap(coordinate);
assertNotNull(map);
assertEquals("11.11111", map.get("longitude"));
assertEquals("22.22222", map.get("latitude"));
}
use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.
the class TestForestJacksonConverter method testConvertToJavaError.
@Test
public void testConvertToJavaError() {
String jsonText = "{\"a\":1, ";
boolean error = false;
ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
try {
forestJacksonConverter.convertToJavaObject(jsonText, Map.class);
} catch (ForestRuntimeException e) {
error = true;
assertNotNull(e.getCause());
}
assertTrue(error);
error = false;
try {
forestJacksonConverter.convertToJavaObject(jsonText, mapper.constructType(Map.class));
} catch (ForestRuntimeException e) {
error = true;
assertNotNull(e.getCause());
}
assertTrue(error);
jsonText = "[1, 2,";
try {
forestJacksonConverter.convertToJavaObject(jsonText, List.class);
} catch (ForestRuntimeException e) {
error = true;
assertNotNull(e.getCause());
}
assertTrue(error);
jsonText = "[1, 2,";
try {
forestJacksonConverter.convertToJavaObject(jsonText, List.class, Integer.class);
} catch (ForestRuntimeException e) {
error = true;
assertNotNull(e.getCause());
}
assertTrue(error);
}
use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.
the class TestForestJacksonConverter method testConvertToJson.
@Test
public void testConvertToJson() {
ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
String text = forestJacksonConverter.encodeToString(new Integer[] { 100, 10 });
Assert.assertEquals("[100,10]", text);
}
use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.
the class TestForestJacksonConverter method testConvertToJava.
@Test
public void testConvertToJava() {
String jsonText = "{\"a\":1, \"b\":2}";
ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
Map result = forestJacksonConverter.convertToJavaObject(jsonText, Map.class);
assertNotNull(result);
assertEquals(1, result.get("a"));
assertEquals(2, result.get("b"));
result = forestJacksonConverter.convertToJavaObject(jsonText, mapper.constructType(Map.class));
assertNotNull(result);
assertEquals(1, result.get("a"));
assertEquals(2, result.get("b"));
}
use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.
the class TestExceptions method testConvertException.
@Test
public void testConvertException() {
Throwable th = new Exception("xxx");
ForestConverter<?> converter = new ForestFastjsonConverter();
ForestConvertException exception = new ForestConvertException(converter, th);
assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestFastjsonConverter' error: xxx");
assertThat(exception.getConverterClass()).isEqualTo(ForestFastjsonConverter.class);
converter = new ForestJacksonConverter();
exception = new ForestConvertException(converter, th);
assertThat(exception.getMessage()).isEqualTo("[Forest] json converter: 'ForestJacksonConverter' error: xxx");
assertThat(exception.getConverterClass()).isEqualTo(ForestJacksonConverter.class);
converter = new DefaultAutoConverter(ForestConfiguration.configuration());
exception = new ForestConvertException(converter, th);
assertThat(exception.getMessage()).isEqualTo("[Forest] auto converter: 'DefaultAutoConverter' error: xxx");
assertThat(exception.getConverterClass()).isEqualTo(DefaultAutoConverter.class);
}
Aggregations