Java Object to Json toJsonString(Object obj)

Here you can find the source of toJsonString(Object obj)

Description

Serializes an object to Json string.

License

Open Source License

Parameter

Parameter Description
obj a parameter

Declaration

public static String toJsonString(Object obj) 

Method Source Code

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

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    private final static ObjectMapper mapper = new ObjectMapper();

    /**/*from ww w.  j a  v a  2s.co m*/
     * Serializes an object to Json string.
     * 
     * @param obj
     * @return
     */
    public static String toJsonString(Object obj) {
        try {
            return obj != null ? mapper.writeValueAsString(obj) : null;
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. toJson(Object object)
  2. toJson(Object value)
  3. toJSON(T obj)
  4. toJSON(T obj)
  5. toJSON(T valueType)
  6. toJsonString(Object obj)
  7. toJSONString(Object object, boolean camelCaseToLowerCaseWithUnderscores)
  8. toJsonString(T value)
  9. writeJSON(HashMap data)