Search in sources :

Example 6 with ForestFastjsonConverter

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

the class TestForestFastjsonConverter method testDate.

@Test
public void testDate() throws ParseException {
    String json = "{\"name\":\"foo\",\"date\":\"2020-10-10 10:12:00\"}";
    ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
    TestJsonObj testJsonObj = forestFastjsonConverter.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\"}";
    testJsonObj = forestFastjsonConverter.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 : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) Test(org.junit.Test)

Example 7 with ForestFastjsonConverter

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

the class TestForestFastjsonConverter method testJavaObjectToMap2.

@Test
public void testJavaObjectToMap2() {
    SubCoordinate coordinate = new SubCoordinate("11.11111", "22.22222");
    ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
    Map map = forestFastjsonConverter.convertObjectToMap(coordinate);
    assertNotNull(map);
    assertEquals("11.11111", map.get("longitude"));
    assertEquals("22.22222", map.get("latitude"));
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) SubCoordinate(com.dtflys.test.model.SubCoordinate) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with ForestFastjsonConverter

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

the class TestForestFastjsonConverter method testConvertToJavaError.

@Test
public void testConvertToJavaError() {
    String badJsonText = "{\"a\":1";
    ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
    boolean error = false;
    try {
        forestFastjsonConverter.convertToJavaObject(badJsonText, Map.class);
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
    error = true;
    try {
        forestFastjsonConverter.convertToJavaObject(badJsonText, new TypeReference<Map>() {
        }.getType());
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
    error = true;
    try {
        forestFastjsonConverter.convertToJavaObject(badJsonText, new TypeReference<Map>() {
        });
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) TypeReference(com.alibaba.fastjson.TypeReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 9 with ForestFastjsonConverter

use of com.dtflys.forest.converter.json.ForestFastjsonConverter 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)

Example 10 with ForestFastjsonConverter

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

the class TestConverterBeanListener method test1.

@Test
public void test1() {
    ForestConverter forestConverter = forestConfiguration.getConverterMap().get(ForestDataType.JSON);
    assertTrue(forestConverter instanceof ForestFastjsonConverter);
    ForestRequest<String> request = giteeClient.index2();
    System.out.println(request.execute());
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) ForestConverter(com.dtflys.forest.converter.ForestConverter) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)16 Test (org.junit.Test)16 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Map (java.util.Map)8 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 TypeReference (com.alibaba.fastjson.TypeReference)2 ForestConverter (com.dtflys.forest.converter.ForestConverter)2 SubCoordinate (com.dtflys.test.model.SubCoordinate)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)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