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);
}
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);
}
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"));
}
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);
}
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"));
}
Aggregations