-2

im trying to write to json using Jackson. It works, and as far as I know its correctly formated. But, it still only comes on one line in the json file. Its makes it hard to read. Is there any way to make it multiple lines so its readable?

Code:

// "Tag" is the object.
String json = new Gson().toJson(tag);

// Writing the object to the json file
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(new File("src/main/java/src/backend/json/toJson.json"), json);

The result in the toJson file:

"{"name":"div","attributes":[{"type":"text","info":"Two"},{"type":"class","info":"test"},{"type":"id","info":"test2"}],"tagsContaining":[{"name":"divv","attributes":[],"tagsContaining":[{"name":"a","attributes":[],"tagsContaining":[]}]},{"name":"div","attributes":[{"type":"text","info":"Two"}],"tagsContaining":[{"name":"a","attributes":[{"type":"text","info":"Two"}],"tagsContaining":[]}]},{"name":"img","attributes":[],"tagsContaining":[]}]}"

As you can see, it is crammed to one line. So, i need to have it formated so it looks like a json file like this :

{
  "name": "div",
  "attributes": [
    {
      "type": "text",
      "info": "Two"
    },
    {
      "type": "class",
      "info": "test"
    },
    {
      "type": "id",
      "info": "test2"
    } ...

My Tag class:

public class Tag {
    String name;
    ArrayList<Attribute> attributes;
    ArrayList<Tag> tagsContaining;

    public Tag() {
        this.attributes = new ArrayList<>();
        this.tagsContaining = new ArrayList<>();
    }

    public Tag(String name) {
        this.name = name;
        this.attributes = new ArrayList<>();
        this.tagsContaining = new ArrayList<>();
    }

    public void addAttribute(Attribute attribute) {
        attributes.add(attribute);
    }

    public void addTag(Tag tag) {
        tagsContaining.add(tag);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Tag{" +
                "name='" + name + '\'' +
                ", attributes=" + attributes +
                ", tagsContaining=" + tagsContaining +
                '}';
    }
}
MyhreS
  • 23
  • 6

1 Answers1

0

To get the desired result, you need to use this method

ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(new FileWriter("src/main/java/src/backend/json/toJson.json"), tag);
ArmDuke
  • 100
  • 4
  • Having a hard time with this mini-comment box... Using @ArmDuke method i get correct formating. But it only does the { "name" : "div" }. The tag-object contains String name, and two arraylist: attributes and tagscontaining. It seems like it doesnt write the arraylists – MyhreS Apr 10 '21 at 12:57
  • Updated/edited the question so it includes the Tag class – MyhreS Apr 10 '21 at 13:04
  • make sure that your Tag class have appropriate getter and setter methods for these fields – ArmDuke Apr 10 '21 at 13:07