Java Object to Json toJson(Object message)

Here you can find the source of toJson(Object message)

Description

to Json

License

Apache License

Declaration

public static String toJson(Object message) throws JsonProcessingException 

Method Source Code


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

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.ObjectMapper;

import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;
import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;
import static com.fasterxml.jackson.annotation.PropertyAccessor.FIELD;
import static com.fasterxml.jackson.annotation.PropertyAccessor.GETTER;
import static com.fasterxml.jackson.annotation.PropertyAccessor.IS_GETTER;
import static com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES;
import static com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES;
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATES_AS_TIMESTAMPS;
import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS;

public class Main {
    public static final ObjectMapper mapper = new ObjectMapper().setVisibility(FIELD, ANY)
            .setVisibility(GETTER, NONE).setVisibility(IS_GETTER, NONE).enable(ALLOW_UNQUOTED_FIELD_NAMES)
            .enable(ALLOW_SINGLE_QUOTES).disable(FAIL_ON_UNKNOWN_PROPERTIES).disable(WRITE_DATES_AS_TIMESTAMPS)
            .disable(WRITE_DATE_KEYS_AS_TIMESTAMPS).setSerializationInclusion(NON_NULL);

    public static String toJson(Object message) throws JsonProcessingException {
        return mapper.writeValueAsString(message);
    }//  ww  w .j  a v  a 2 s  .c o m
}

Related

  1. object2Jason(Object obj)
  2. objectToString(StringBuilder bld, ObjectNode node, int depth, boolean newLine)
  3. objToJson(Object obj)
  4. toJson(List list)
  5. toJson(Object dto)
  6. toJson(Object o)
  7. toJson(Object o)
  8. toJSON(Object obj)
  9. toJSON(Object obj)