Search in sources :

Example 6 with ForestGsonConverter

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

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

the class TestGsonConverter method testConvertToJavaError.

@Test
public void testConvertToJavaError() {
    String jsonText = "{\"a\":1, ";
    boolean error = false;
    ForestGsonConverter gsonConverter = new ForestGsonConverter();
    try {
        gsonConverter.convertToJavaObject(jsonText, Map.class);
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
    error = false;
    try {
        gsonConverter.convertToJavaObject(jsonText, new TypeToken<Map>() {
        }.getType());
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
    jsonText = "[1, 2,";
    try {
        gsonConverter.convertToJavaObject(jsonText, Map.class);
    } catch (ForestRuntimeException e) {
        error = true;
        assertNotNull(e.getCause());
    }
    assertTrue(error);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Test(org.junit.Test)

Example 8 with ForestGsonConverter

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

the class TestGsonConverter method testConvertToJava.

@Test
public void testConvertToJava() {
    String jsonText = "{\"a\":1, \"b\":2}";
    ForestGsonConverter gsonConverter = new ForestGsonConverter();
    Map result = gsonConverter.convertToJavaObject(jsonText, Map.class);
    assertNotNull(result);
    assertEquals(1, result.get("a"));
    assertEquals(2, result.get("b"));
    result = gsonConverter.convertToJavaObject(jsonText, new TypeToken<Map>() {
    }.getType());
    assertNotNull(result);
    assertEquals(1, result.get("a"));
    assertEquals(2, result.get("b"));
    Data data = gsonConverter.convertToJavaObject(jsonText, Data.class);
    assertNotNull(data);
    assertEquals(Integer.valueOf(1), data.getA());
    assertEquals(Integer.valueOf(2), data.getB());
    jsonText = "[1, 2, 3]";
    List list = gsonConverter.convertToJavaObject(jsonText, List.class);
    assertNotNull(list);
    assertEquals(3, list.size());
    assertEquals(1, list.get(0));
    assertEquals(2, list.get(1));
    assertEquals(3, list.get(2));
    jsonText = "[1, 2, 3]";
    list = gsonConverter.convertToJavaObject(jsonText, new TypeToken<List>() {
    }.getType());
    assertNotNull(list);
    assertEquals(3, list.size());
    assertEquals(1, list.get(0));
    assertEquals(2, list.get(1));
    assertEquals(3, list.get(2));
    jsonText = "[1, 2, 3, {\"a\":1, \"b\":2}]";
    list = gsonConverter.convertToJavaObject(jsonText, List.class);
    assertNotNull(list);
    assertEquals(4, list.size());
    assertEquals(1, list.get(0));
    assertEquals(2, list.get(1));
    assertEquals(3, list.get(2));
    Object obj = list.get(3);
    assertNotNull(obj);
    assertTrue(obj instanceof Map);
    Map map = (Map) obj;
    assertEquals(1, map.get("a"));
    assertEquals(2, map.get("b"));
}
Also used : ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 9 with ForestGsonConverter

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

the class TestGsonConverter method testMapToJSONString.

@Test
public void testMapToJSONString() throws ParseException {
    Map<String, Object> map = new LinkedHashMap<>();
    map.put("name", "foo");
    map.put("password", "bar");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    Date date = dateFormat.parse("2020-10-10 10:10:10");
    map.put("createDate", date);
    ForestGsonConverter forestGsonConverter = new ForestGsonConverter();
    forestGsonConverter.setDateFormat("yyyy/MM/dd hh:mm:ss");
    String jsonStr = forestGsonConverter.encodeToString(map);
    assertEquals("{\"name\":\"foo\",\"password\":\"bar\",\"createDate\":\"2020/10/10 10:10:10\"}", jsonStr);
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) ForestGsonConverter(com.dtflys.forest.converter.json.ForestGsonConverter) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 10 with ForestGsonConverter

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

the class TestGsonConverter method testJavaObjectToMap2.

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

Aggregations

ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)10 Test (org.junit.Test)9 LinkedHashMap (java.util.LinkedHashMap)4 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)3 Map (java.util.Map)3 SubCoordinate (com.dtflys.test.model.SubCoordinate)2 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 JSONConverterSelector (com.dtflys.forest.converter.json.JSONConverterSelector)1 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)1 TestLogHandler (com.dtflys.spring.test.logging.TestLogHandler)1 Coordinate (com.dtflys.test.model.Coordinate)1 TypeToken (com.google.gson.reflect.TypeToken)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 List (java.util.List)1 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1