Here you can find the source of toJson(E e)
Parameter | Description |
---|---|
E | the type parameter |
e | the e |
Parameter | Description |
---|---|
IOException | the io exception |
public static <E> String toJson(E e) throws IOException
//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(); } }