How to use indentation in gson to generate a json file

Viewed 2808

I have to generate a json file containing indentation. I was using Jackson for this but it adds a space before the colon and I don't need it, so I decided to use Gson.

After changing the code, I figured out that by default Gson don't use indentation but Jackson does. Does anyone know if it is possible in Gson to get indentation and how to do it?

For generating the json file with Gson I made this:

GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder = gsonBuilder.setPrettyPrinting();
Gson gson = gsonBuilder.create();
Writer writer = new FileWriter(propsFile); 
gson.toJson(properties, writer);
2 Answers

Possible duplicate of https://stackoverflow.com/a/41509714/4379906


Use Jackson and and configure it not to add spaces before colon:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.2</version>
</dependency>

CustomPrettyPrinter.java:

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;

import java.io.IOException;

class CustomPrettyPrinter extends DefaultPrettyPrinter {
    public CustomPrettyPrinter() {
        super();
    }

    public CustomPrettyPrinter(DefaultPrettyPrinter base) {
        super(base);
    }

    @Override
    public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
        g.writeRaw(": ");
    }

    @Override
    public DefaultPrettyPrinter createInstance() {
        return new CustomPrettyPrinter(this);
    }
}

UseJackson.java:

import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class UseJackson {
    public static void main(String[] args) throws IOException {
        DefaultPrettyPrinter.Indenter indenter = new DefaultIndenter("    ", DefaultIndenter.SYS_LF);
        DefaultPrettyPrinter printer = new CustomPrettyPrinter();
        printer.indentArraysWith(indenter);
        printer.indentObjectsWith(indenter);

        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDefaultPrettyPrinter(printer);

        Properties properties = new Properties();
        properties.put("foo", "3");
        properties.put("bar", "4");
        objectMapper.writerWithDefaultPrettyPrinter().writeValue(new File("a.json"), properties);
    }
}

Use Gson and configure indentation:

UseGson.java:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.stream.JsonWriter;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class UseGson {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        properties.put("foo", "3");
        properties.put("bar", "4");

        JsonWriter jsonWriter = new JsonWriter(new FileWriter("a.json"));
        jsonWriter.setIndent("    ");

        Gson gson = new GsonBuilder().serializeNulls().create();
        gson.toJson(properties, Properties.class, jsonWriter);

        jsonWriter.close();
    }
}
Related