Search in sources :

Example 1 with JSONConverterSelector

use of com.dtflys.forest.converter.json.JSONConverterSelector 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 2 with JSONConverterSelector

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

the class TestForestConfiguration method testJSONConvertSelectCheck.

@Test
public void testJSONConvertSelectCheck() throws Throwable {
    JSONConverterSelector jsonConverterSelector = new JSONConverterSelector();
    try {
        Class fastJsonClass = jsonConverterSelector.checkFastJSONClass();
        assertNotNull(fastJsonClass);
    } catch (Throwable th) {
    }
    try {
        Class gsonClass = jsonConverterSelector.checkGsonClass();
        assertNotNull(gsonClass);
    } catch (Throwable th) {
    }
    try {
        Class jacson = jsonConverterSelector.checkJacsonClass();
        assertNotNull(jacson);
    } catch (Throwable th) {
    }
}
Also used : JSONConverterSelector(com.dtflys.forest.converter.json.JSONConverterSelector) Test(org.junit.Test)

Example 3 with JSONConverterSelector

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

Aggregations

JSONConverterSelector (com.dtflys.forest.converter.json.JSONConverterSelector)3 Test (org.junit.Test)2 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 DefaultBinaryConverter (com.dtflys.forest.converter.binary.DefaultBinaryConverter)1 DefaultFormConvertor (com.dtflys.forest.converter.form.DefaultFormConvertor)1 ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)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 DefaultLogHandler (com.dtflys.forest.logging.DefaultLogHandler)1