Java Json Create toJson(Object object, String dateFormat)

Here you can find the source of toJson(Object object, String dateFormat)

Description

to Json

License

Open Source License

Declaration

public static String toJson(Object object, String dateFormat) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.SimpleDateFormat;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Main {
    public static String toJson(Object object) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        try {/*from   ww w . j a v  a  2s .  c om*/
            return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String toJson(Object object, String dateFormat) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat(dateFormat));
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        try {
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}

Related

  1. object2JsonDateSerializer(Object obj, final String dateformat)
  2. toBean(Class clazz, String json)
  3. toJson(E e)
  4. toJSON(Map map)
  5. toJson(Object object)
  6. toJsonString(Map map)
  7. toList(String json, Class clz)
  8. toLocalJson(Object value, int depth)