Java Properties Create toPrettyPrintJSON(Object object)

Here you can find the source of toPrettyPrintJSON(Object object)

Description

to Pretty Print JSON

License

Open Source License

Declaration

public static String toPrettyPrintJSON(Object object) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014, 2017 Open Door Logistics (www.opendoorlogistics.com)
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at http://www.gnu.org/licenses/lgpl.txt
 ******************************************************************************/

import java.io.StringWriter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Main {
    public static String toPrettyPrintJSON(Object object) {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // disable this to get data times etc out in  ISO 8601 format (which is close to a standard)
        mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        StringWriter writer = new StringWriter();
        try {//from  w w  w. ja va2  s .  com
            mapper.writeValue(writer, object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return writer.toString();
    }
}

Related

  1. splitArrayElementsIntoProperties(String[] array, String delimiter)
  2. splitArrayElementsIntoProperties(String[] array, String delimiter)
  3. toDictionary(Map properties)
  4. toFileContent(Properties props)
  5. toMap(byte[] source)
  6. toProperties(byte[] source)
  7. toProperties(byte[] source)
  8. toProperties(File properties, File xml, String comment)
  9. toProperties(FileInputStream fis)