Search in sources :

Example 1 with ForestFastjsonConverter

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);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with ForestFastjsonConverter

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"));
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) TypeReference(com.alibaba.fastjson.TypeReference) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with ForestFastjsonConverter

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);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) SimpleDate(freemarker.template.SimpleDate) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 4 with ForestFastjsonConverter

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"));
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) Cause(com.dtflys.test.http.model.Cause) FormListParam(com.dtflys.test.http.model.FormListParam) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with ForestFastjsonConverter

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);
}
Also used : ForestFastjsonConverter(com.dtflys.forest.converter.json.ForestFastjsonConverter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)16 Test (org.junit.Test)16 HashMap (java.util.HashMap)9 LinkedHashMap (java.util.LinkedHashMap)9 Map (java.util.Map)8 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 TypeReference (com.alibaba.fastjson.TypeReference)2 ForestConverter (com.dtflys.forest.converter.ForestConverter)2 SubCoordinate (com.dtflys.test.model.SubCoordinate)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)1 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)1 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)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