Java Object to Json toJSON(Object obj)

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

Description

to JSON

License

Apache License

Declaration

public static String toJSON(Object obj) 

Method Source Code


//package com.java2s;
//License from project: Apache 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 {
    public static String toJSON(Object obj) {
        try {//from w  ww. j a va 2  s.  co m
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
            objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
            return objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return "{error: " + e.getMessage() + "}";
        }
    }
}

Related

  1. toJson(List list)
  2. toJson(Object dto)
  3. toJson(Object message)
  4. toJson(Object o)
  5. toJson(Object o)
  6. toJSON(Object obj)
  7. toJson(Object obj)
  8. toJson(Object object)
  9. toJson(Object object)