JsonObject.java 1.43 KB
package com.ishchuk.antlr.json.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class JsonObject extends JsonElement {
    private Map<String, JsonElement> map;

    public Map<String, JsonElement> getMap() {
        return map;
    }

    public void setMap(Map<String, JsonElement> map) {
        this.map = map;
    }

    public JsonObject() {
        this.map = new HashMap<>();
    }

    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("{");
        this.map.keySet().forEach(key -> {
            builder.append(key)
                    .append(":")
                    .append(map.get(key).toString())
                    .append(",");
        });
        int lastInd = builder.lastIndexOf(",");
        if (lastInd > 0) {
            builder.deleteCharAt(lastInd);
        }
        builder.append("}");

        return builder.toString();
    }

    @Override
    public String toYml(String delay) {
        StringBuilder builder = new StringBuilder();
        this.map.keySet().forEach(key -> {
            builder.append(delay).append(key).append(": ");
            if (this.map.get(key) instanceof JsonPrimitive) {
                builder.append(this.map.get(key).toString()).append("\n");
            } else {
                builder.append("\n").append(this.map.get(key).toYml(delay + "  "));
            }
        });
        return builder.toString();
    }
}