Java Object to Json toJsonString(T value)

Here you can find the source of toJsonString(T value)

Description

to Json String

License

Apache License

Declaration

public static <T> String toJsonString(T value) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

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

    public static <T> String toJsonString(T value) {
        try {//  ww  w  .  j a v a2s. c o  m
            return mapper.writeValueAsString(value);
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException(String.format(
                    "Illegal argument. Could not convert object %s to JSON string: (%s)", value, e.getMessage()));
        }
    }
}

Related

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