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 +
'}';
}
}