Java Json Create toBean(Class clazz, String json)

Here you can find the source of toBean(Class clazz, String json)

Description

To bean e.

License

Apache License

Parameter

Parameter Description
E the type parameter
clazz the clazz
json the json

Exception

Parameter Description
IOException the io exception

Return

the e

Declaration

public static <E> E toBean(Class<E> clazz, String json) 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  w  w  w.jav a2 s. c  o m*/
     * The constant DATE_FORMAT_DEF.
     */
    public static String DATE_FORMAT_DEF = "yyyy-MM-dd HH:mm:ss";

    /**
     * To bean e.
     *
     * @param <E>   the type parameter
     * @param clazz the clazz
     * @param json  the json
     * @return the e
     * @throws IOException the io exception
     */
    public static <E> E toBean(Class<E> clazz, String json) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat(DATE_FORMAT_DEF));
        objectMapper.getDeserializationConfig().with(new SimpleDateFormat(DATE_FORMAT_DEF));
        StringWriter stringWriter = new StringWriter();
        return objectMapper.readValue(json, clazz);
    }
}

Related

  1. createJsonValue(String string)
  2. createPrettyWriterFactory()
  3. emptyArray()
  4. getJson(T t)
  5. object2JsonDateSerializer(Object obj, final String dateformat)
  6. toJson(E e)
  7. toJSON(Map map)
  8. toJson(Object object)
  9. toJson(Object object, String dateFormat)