Solved How to save a JSON file with Syntax (I mean Line breaks) for easy reading.

Discussion in 'Spigot Plugin Development' started by Abcik, Jan 20, 2021.

  1. I haven't worked much with Json, so sorry if this question is stupid, but I didn't find anything on the forums.

    So now i have this:


    But i need some like this:

    So, the code I am using:

    Code (Java):

        public void Test() {
            try {

                Gson gson = new Gson();
                FileWriter wtitter = new FileWriter(getDataFolder() + "/test.json");
                gson.toJson(new User(1, "TEST",, wtitter);

            } catch (IOException e) {

    public class User {
        private int id;
        private String name;
        private LocalDateTime time;
        public User(int ids, String names, LocalDateTime times) {
            id = ids;
            time = times;
            name = names;
        public int getID() {
            return id;
        public LocalDateTime getTime() {
            return time;
        public String getName() {
            return name;
    I'm looking for the simplest possible solution, and I hope Gson provides this capability without having to iterate over objects manually.

    Thank you!
  2. Can't you do something like
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    Basically what you want is called Pretty Print
    • Like Like x 1
  3. Gson contains a GsonBuilder which contains a setPrettyPrinting method which does exactly what you're after and also contains several other methods that might prove useful for you.

    Edit: Sniped by HexedHero
    • Like Like x 1
  4. Thanks a lot guys, exactly what I was looking for! As I said, I have just started working with json.
  5. Glad we helped and good luck!