Search in sources :

Example 1 with ForestJacksonConverter

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"));
}
Also used : SubCoordinate(com.dtflys.test.model.SubCoordinate) Coordinate(com.dtflys.test.model.Coordinate) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 2 with ForestJacksonConverter

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);
}
Also used : ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with ForestJacksonConverter

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);
}
Also used : ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) Test(org.junit.Test)

Example 4 with ForestJacksonConverter

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"));
}
Also used : ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with ForestJacksonConverter

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);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) ForestUnsupportException(com.dtflys.forest.exceptions.ForestUnsupportException) ForestInterceptorDefineException(com.dtflys.forest.exceptions.ForestInterceptorDefineException) ForestHandlerException(com.dtflys.forest.exceptions.ForestHandlerException) ForestVariableUndefinedException(com.dtflys.forest.exceptions.ForestVariableUndefinedException) ForestRetryException(com.dtflys.forest.exceptions.ForestRetryException) ForestNetworkException(com.dtflys.forest.exceptions.ForestNetworkException) ForestNoFileNameException(com.dtflys.forest.exceptions.ForestNoFileNameException) ForestFileNotFoundException(com.dtflys.forest.exceptions.ForestFileNotFoundException) ForestConvertException(com.dtflys.forest.exceptions.ForestConvertException) Test(org.junit.Test)

Aggregations

ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)12 Test (org.junit.Test)11 LinkedHashMap (java.util.LinkedHashMap)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)2 SubCoordinate (com.dtflys.test.model.SubCoordinate)2 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)1 ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)1 JSONConverterSelector (com.dtflys.forest.converter.json.JSONConverterSelector)1 ForestConvertException (com.dtflys.forest.exceptions.ForestConvertException)1 ForestFileNotFoundException (com.dtflys.forest.exceptions.ForestFileNotFoundException)1 ForestHandlerException (com.dtflys.forest.exceptions.ForestHandlerException)1 ForestInterceptorDefineException (com.dtflys.forest.exceptions.ForestInterceptorDefineException)1 ForestNetworkException (com.dtflys.forest.exceptions.ForestNetworkException)1 ForestNoFileNameException (com.dtflys.forest.exceptions.ForestNoFileNameException)1 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)1 ForestUnsupportException (com.dtflys.forest.exceptions.ForestUnsupportException)1