use of com.dtflys.forest.converter.json.ForestFastjsonConverter in project forest by dromara.
the class TestForestFastjsonConverter method testConvertToJson.
@Test
public void testConvertToJson() {
ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
String text = forestFastjsonConverter.encodeToString(new Integer[] { 100, 10 });
assertEquals("[100,10]", text);
Map map = new LinkedHashMap();
map.put("a", 1);
text = forestFastjsonConverter.encodeToString(map);
assertEquals("{\"a\":1}", text);
Map sub = new LinkedHashMap();
sub.put("x", 0);
map.put("s1", sub);
map.put("s2", sub);
text = forestFastjsonConverter.encodeToString(map);
assertEquals("{\"a\":1,\"s1\":{\"x\":0},\"s2\":{\"x\":0}}", text);
forestFastjsonConverter.setSerializerFeature(null);
text = forestFastjsonConverter.encodeToString(new Integer[] { 100, 10 });
assertEquals("[100,10]", text);
}
use of com.dtflys.forest.converter.json.ForestFastjsonConverter in project forest by dromara.
the class TestForestFastjsonConverter method testConvertToJava.
@Test
public void testConvertToJava() throws ParseException {
String jsonText = "{\"a\":1, \"b\":2}";
ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
Map result = forestFastjsonConverter.convertToJavaObject(jsonText, Map.class);
assertNotNull(result);
assertEquals(1, result.get("a"));
assertEquals(2, result.get("b"));
result = forestFastjsonConverter.convertToJavaObject(jsonText, new TypeReference<Map>() {
}.getType());
assertNotNull(result);
assertEquals(1, result.get("a"));
assertEquals(2, result.get("b"));
result = forestFastjsonConverter.convertToJavaObject(jsonText, new TypeReference<Map>() {
});
assertNotNull(result);
assertEquals(1, result.get("a"));
assertEquals(2, result.get("b"));
}
use of com.dtflys.forest.converter.json.ForestFastjsonConverter in project forest by dromara.
the class TestForestFastjsonConverter 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);
ForestFastjsonConverter fastjsonConverter = new ForestFastjsonConverter();
fastjsonConverter.setDateFormat("yyyy/MM/dd hh:mm:ss");
String jsonStr = fastjsonConverter.encodeToString(map);
assertEquals("{\"name\":\"foo\",\"password\":\"bar\",\"createDate\":\"2020/10/10 10:10:10\"}", jsonStr);
}
use of com.dtflys.forest.converter.json.ForestFastjsonConverter in project forest by dromara.
the class TestForestFastjsonConverter method testJavaObjectToMap3.
@Test
public void testJavaObjectToMap3() {
FormListParam param = new FormListParam();
List<Integer> idList = Lists.newArrayList(1, 2, 3);
param.setUsername("foo");
param.setPassword("123456");
param.setIdList(idList);
Cause cause1 = new Cause();
cause1.setId(1);
cause1.setScore(87);
Cause cause2 = new Cause();
cause2.setId(2);
cause2.setScore(73);
List<Cause> causes = Lists.newArrayList(cause1, cause2);
param.setCause(causes);
ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
Map map = forestFastjsonConverter.convertObjectToMap(param);
assertEquals("foo", map.get("username"));
assertEquals("123456", map.get("password"));
assertEquals(idList, map.get("idList"));
assertEquals(causes, map.get("cause"));
}
use of com.dtflys.forest.converter.json.ForestFastjsonConverter in project forest by dromara.
the class TestForestFastjsonConverter method testDeepMap.
@Test
public void testDeepMap() {
String json = "{\"code\":\"000000\",\"msg\":\"success\",\"timestamp\":\"2020-08-26T12:06:55.498Z\",\"data\":[{\"id\":8,\"channelCode\":\"UMS_MINI_PAY_WEIXIN\",\"channelName\":\"银联商务小程序微信支付\",\"channelParty\":\"1\",\"terminalType\":3,\"payType\":2,\"logoUrl\":null}]}";
ForestFastjsonConverter forestFastjsonConverter = new ForestFastjsonConverter();
Map map = forestFastjsonConverter.convertToJavaObject(json, HashMap.class);
System.out.println(map);
}
Aggregations