Here you can find the source of toJSON(Object obj)
public static String toJSON(Object obj)
//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() + "}"; } } }