Java Object to Json toJSON(T valueType)

Here you can find the source of toJSON(T valueType)

Description

to JSON

License

Open Source License

Declaration

public static <T> String toJSON(T valueType) 

Method Source Code

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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

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

    public static <T> String toJSON(T valueType) {

        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
        String json = "Problem serializing " + valueType.getClass();
        try {/*from   w  w  w  .  j av  a  2s .  c  o  m*/
            json = objectMapper.writeValueAsString(valueType);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }
}

Related

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