Search in sources :

Example 11 with ForestJacksonConverter

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

the class TestForestJacksonConverter 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);
    ForestJacksonConverter forestJacksonConverter = new ForestJacksonConverter();
    forestJacksonConverter.setDateFormat("yyyy/MM/dd hh:mm:ss");
    String jsonStr = forestJacksonConverter.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) ForestJacksonConverter(com.dtflys.forest.converter.json.ForestJacksonConverter) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 12 with ForestJacksonConverter

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

the class TestForestJacksonConverter method testJavaObjectToMap2.

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

Aggregations

ForestJacksonConverter (com.dtflys.forest.converter.json.ForestJacksonConverter)12 Test (org.junit.Test)11 LinkedHashMap (java.util.LinkedHashMap)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 ForestJsonConverter (com.dtflys.forest.converter.json.ForestJsonConverter)2 SubCoordinate (com.dtflys.test.model.SubCoordinate)2 DefaultAutoConverter (com.dtflys.forest.converter.auto.DefaultAutoConverter)1 ForestFastjsonConverter (com.dtflys.forest.converter.json.ForestFastjsonConverter)1 ForestGsonConverter (com.dtflys.forest.converter.json.ForestGsonConverter)1 JSONConverterSelector (com.dtflys.forest.converter.json.JSONConverterSelector)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 ForestNoFileNameException (com.dtflys.forest.exceptions.ForestNoFileNameException)1 ForestRetryException (com.dtflys.forest.exceptions.ForestRetryException)1 ForestUnsupportException (com.dtflys.forest.exceptions.ForestUnsupportException)1