Search in sources :

Example 6 with ForestJacksonConverter

use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.

the class TestJacksonConverter method testConfig.

@Test
public void testConfig() {
    ForestJsonConverter jsonConverter = forestConfiguration.getJsonConverter();
    assertThat(jsonConverter).isNotNull().isInstanceOf(ForestJacksonConverter.class);
    ForestJacksonConverter forestJacksonConverter = (ForestJacksonConverter) jsonConverter;
    assertThat(forestJacksonConverter.getDateFormat()).isEqualTo("yyyy/MM/dd hh:mm:ss");
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) Test(org.junit.Test)

Example 7 with ForestJacksonConverter

use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.

the class TestForestConfiguration method testJSONConverterSelect.

@Test
public void testJSONConverterSelect() throws Throwable {
    JSONConverterSelector jsonConverterSelector = new JSONConverterSelector();
    JSONConverterSelector spy = Mockito.spy(jsonConverterSelector);
    Mockito.when(spy.checkFastJSONClass()).thenThrow(new ClassNotFoundException("com.alibaba.fastjson.JSON"));
    ForestJsonConverter jsonConverter = spy.select();
    assertNotNull(jsonConverter);
    assertTrue(jsonConverter instanceof ForestJacksonConverter);
    jsonConverterSelector = new JSONConverterSelector();
    spy = Mockito.spy(jsonConverterSelector);
    Mockito.when(spy.checkFastJSONClass()).thenThrow(new ClassNotFoundException("com.alibaba.fastjson.JSON"));
    Mockito.when(spy.checkJacsonClass()).thenThrow(new ClassNotFoundException("com.fasterxml.jackson.databind.ObjectMapper"));
    jsonConverter = spy.select();
    assertNotNull(jsonConverter);
    assertTrue(jsonConverter instanceof ForestGsonConverter);
    jsonConverterSelector = new JSONConverterSelector();
    spy = Mockito.spy(jsonConverterSelector);
    Mockito.when(spy.checkFastJSONClass()).thenThrow(new ClassNotFoundException("com.alibaba.fastjson.JSON"));
    Mockito.when(spy.checkJacsonClass()).thenThrow(new ClassNotFoundException("com.fasterxml.jackson.databind.ObjectMapper"));
    Mockito.when(spy.checkGsonClass()).thenThrow(new ClassNotFoundException("com.google.gson.JsonParser"));
    jsonConverter = spy.select();
    assertNull(jsonConverter);
}
Also used : ForestJsonConverter(com.dtflys.forest.converter.json.ForestJsonConverter) ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) JSONConverterSelector(com.dtflys.forest.converter.json.JSONConverterSelector) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) Test(org.junit.Test)

Example 8 with ForestJacksonConverter

use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.

the class TestAmapClient method prepareClient.

// @BeforeClass
public static void prepareClient() {
    configuration = Forest.config();
    configuration.setJsonConverter(new ForestJacksonConverter());
    configuration.setCacheEnabled(false);
    amapClient = configuration.createInstance(AmapClient.class);
}
Also used : ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) AmapClient(com.dtflys.test.http.client.AmapClient)

Example 9 with ForestJacksonConverter

use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.

the class TestForestJacksonConverter method testConvertToJsonError.

@Test
public void testConvertToJsonError() {
    ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
    Map map = new HashMap();
    map.put("ref", map);
    boolean error = false;
    try {
        forestJacksonConverter.encodeToString(map);
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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 10 with ForestJacksonConverter

use of com.dtflys.forest.converter.json.ForestJacksonConverter in project forest by dromara.

the class TestForestJacksonConverter method testDate.

@Test
public void testDate() throws ParseException {
    ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
    String json = "{\"name\":\"foo\",\"date\":\"2020-10-10 10:12:00\"}";
    forestJacksonConverter.setDateFormat("yyyy-MM-dd hh:mm:ss");
    TestJsonObj testJsonObj = forestJacksonConverter.convertToJavaObject(json, TestJsonObj.class);
    assertNotNull(testJsonObj);
    assertEquals("foo", testJsonObj.getName());
    assertDateEquals("2020-10-10 10:12:00", testJsonObj.getDate(), "yyyy-MM-dd hh:mm:ss");
    json = "{\"name\":\"foo\",\"date\":\"2020/10/10 10:12:00\"}";
    forestJacksonConverter.setDateFormat("yyyy/MM/dd hh:mm:ss");
    testJsonObj = forestJacksonConverter.convertToJavaObject(json, TestJsonObj.class);
    assertNotNull(testJsonObj);
    assertEquals("foo", testJsonObj.getName());
    assertDateEquals("2020-10-10 10:12:00", testJsonObj.getDate(), "yyyy-MM-dd hh:mm:ss");
}
Also used : ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) 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