Java Json Create toJson(E e)

Here you can find the source of toJson(E e)

Description

To json string.

License

Apache License

Parameter

Parameter Description
E the type parameter
e the e

Exception

Parameter Description
IOException the io exception

Return

the string

Declaration

public static <E> String toJson(E e) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.io.StringWriter;
import java.text.SimpleDateFormat;

public class Main {
    /**/*from www .ja  v a2s. c  om*/
     * The constant DATE_FORMAT_DEF.
     */
    public static String DATE_FORMAT_DEF = "yyyy-MM-dd HH:mm:ss";

    /**
     * To json string.
     *
     * @param <E> the type parameter
     * @param e   the e
     * @return the string
     * @throws IOException the io exception
     */
    public static <E> String toJson(E e) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT_DEF));
        StringWriter stringWriter = new StringWriter();
        objectMapper.writeValue(stringWriter, e);
        return stringWriter.toString();
    }
}

Related

  1. createPrettyWriterFactory()
  2. emptyArray()
  3. getJson(T t)
  4. object2JsonDateSerializer(Object obj, final String dateformat)
  5. toBean(Class clazz, String json)
  6. toJSON(Map map)
  7. toJson(Object object)
  8. toJson(Object object, String dateFormat)
  9. toJsonString(Map map)