Search in sources :

Example 6 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class ForestConfiguration method createConfiguration.

public static ForestConfiguration createConfiguration() {
    ForestConfiguration configuration = new ForestConfiguration();
    configuration.setId("forestConfiguration" + configuration.hashCode());
    configuration.setJsonConverterSelector(new JSONConverterSelector());
    ForestProtobufConverterManager protobufConverterFactory = ForestProtobufConverterManager.getInstance();
    configuration.setProtobufConverter(protobufConverterFactory.getForestProtobufConverter());
    configuration.setXmlConverter(new ForestJaxbConverter());
    configuration.setTextConverter(new DefaultTextConverter());
    DefaultAutoConverter autoConverter = new DefaultAutoConverter(configuration);
    configuration.getConverterMap().put(ForestDataType.AUTO, autoConverter);
    configuration.getConverterMap().put(ForestDataType.BINARY, new DefaultBinaryConverter(autoConverter));
    configuration.getConverterMap().put(ForestDataType.FORM, new DefaultFormConvertor(configuration));
    setupJSONConverter(configuration);
    configuration.setTimeout(3000);
    configuration.setMaxConnections(500);
    configuration.setMaxRouteConnections(500);
    configuration.setRetryer(BackOffRetryer.class);
    configuration.setMaxRetryCount(0);
    configuration.setMaxRetryInterval(0);
    configuration.registerFilter("json", JSONFilter.class);
    configuration.registerFilter("xml", XmlFilter.class);
    configuration.setLogHandler(new DefaultLogHandler());
    return configuration;
}
Also used : ForestJaxbConverter(com.dtflys.forest.converter.xml.ForestJaxbConverter) DefaultTextConverter(com.dtflys.forest.converter.text.DefaultTextConverter) DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) JSONConverterSelector(com.dtflys.forest.converter.json.JSONConverterSelector) DefaultBinaryConverter(com.dtflys.forest.converter.binary.DefaultBinaryConverter) DefaultLogHandler(com.dtflys.forest.logging.DefaultLogHandler) ForestProtobufConverterManager(com.dtflys.forest.converter.protobuf.ForestProtobufConverterManager) DefaultFormConvertor(com.dtflys.forest.converter.form.DefaultFormConvertor)

Example 7 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class TestAutoConverter method testAutoNumber.

@Test
public void testAutoNumber() {
    DefaultAutoConverter autoConverter = getConverter();
    String expect = "12";
    Integer num1 = autoConverter.convertToJavaObject(expect, Integer.class);
    assertEquals(Integer.valueOf(12), num1);
    expect = "1.2";
    Float num2 = autoConverter.convertToJavaObject(expect, Float.class);
    assertEquals(Float.valueOf(1.2f), num2);
    expect = "2.22";
    Double num3 = autoConverter.convertToJavaObject(expect, Double.class);
    assertEquals(Double.valueOf(2.22D), num3);
    expect = "3.345";
    BigDecimal num4 = autoConverter.convertToJavaObject(expect, BigDecimal.class);
    assertEquals(new BigDecimal(expect), num4);
}
Also used : DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 8 with DefaultAutoConverter

use of com.dtflys.forest.converter.auto.DefaultAutoConverter in project forest by dromara.

the class TestAutoConverter method testAutoJsonObject.

@Test
public void testAutoJsonObject() {
    DefaultAutoConverter autoConverter = getConverter();
    String text = "{\"username\": \"foo\", \"password\": \"bar\"}";
    Map<String, Object> map = autoConverter.convertToJavaObject(text, Map.class);
    assertNotNull(map);
    assertEquals("foo", map.get("username"));
    assertEquals("bar", map.get("password"));
}
Also used : DefaultAutoConverter(com.dtflys.forest.converter.auto.DefaultAutoConverter) Test(org.junit.Test)

Aggregations

DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)8 Test (org.junit.Test)6 TypeReference (com.alibaba.fastjson.TypeReference)1 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 DefaultBinaryConverter (com.dtflys.forest.converter.binary.DefaultBinaryConverter)1 DefaultFormConvertor (com.dtflys.forest.converter.form.DefaultFormConvertor)1 ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 JSONConverterSelector (com.dtflys.forest.converter.json.JSONConverterSelector)1 ForestProtobufConverterManager (com.dtflys.forest.converter.protobuf.ForestProtobufConverterManager)1 DefaultTextConverter (com.dtflys.forest.converter.text.DefaultTextConverter)1 ForestJaxbConverter (com.dtflys.forest.converter.xml.ForestJaxbConverter)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 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1