org.ocsoft.olivia.models.contents.config.JsonObject.java Source code

Java tutorial

Introduction

Here is the source code for org.ocsoft.olivia.models.contents.config.JsonObject.java

Source

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.ocsoft.olivia.models.contents.config;

import java.io.IOException;
import java.util.Map;

import org.frows.genericfile.v0_3_2.GenericFile;
import org.ocsoft.olivia.models.contents.config.def.ConfigDef;
import org.ocsoft.olivia.utils.JsonUtils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

/**
 * 
 * @author tohhy
 */
public class JsonObject {

    /**
     * ??JsonObject?????.
     */
    private final GenericFile file;

    /**
     * 
     */
    private ObjectNode root;

    /**
     * 
     * @param file
     */
    public JsonObject(GenericFile file) {
        if (file == null)
            throw new IllegalArgumentException("file must not be null");
        this.file = file;
        load();
    }

    private ObjectNode loadRootNode() throws IOException {
        if (!file.exists())
            return JsonUtils.createObjectNode();
        String json = file.getReader().readAsString();
        JsonNode node = (json.length() == 0) ? JsonUtils.createObjectNode() : JsonUtils.readTree(json);
        if (node instanceof ObjectNode) {
            return (ObjectNode) node;
        } else {
            return JsonUtils.createObjectNode();
        }
    }

    public void load() {
        try {
            root = loadRootNode();
        } catch (IOException e) {
            e.printStackTrace();
            if (root == null)
                root = JsonUtils.createObjectNode();
        }
    }

    public void store() throws IOException {
        JsonUtils.writeObjectToFile(file.getFile(), root);
    }

    public String getAsJsonString() throws JsonProcessingException {
        return JsonUtils.toJsonString(root);
    }

    public JsonNode get(String key) {
        return root.get(key);
    }

    public Map<String, Object> getAsMap() throws IOException {
        return JsonUtils.readJsonParser(root.traverse(), new TypeReference<Map<String, Object>>() {
        });
    }

    public boolean containsKey(String key) {
        return root.has(key);
    }

    public boolean getAsBoolean(ConfigDef key) throws IOException {
        boolean defaultValue = JsonUtils.readJsonString(key.getDefaultValue(), boolean.class);
        if (!containsKey(key.getConfigKey()))
            put(key.getConfigKey(), defaultValue);

        return getAsBoolean(key.getConfigKey(), defaultValue);
    }

    public int getAsInt(ConfigDef key) throws IOException {
        int defaultValue = JsonUtils.readJsonString(key.getDefaultValue(), int.class);
        if (!containsKey(key.getConfigKey()))
            put(key.getConfigKey(), defaultValue);

        return getAsInt(key.getConfigKey(), defaultValue);
    }

    public double getAsDouble(ConfigDef key) throws IOException {
        double defaultValue = JsonUtils.readJsonString(key.getDefaultValue(), double.class);
        if (!containsKey(key.getConfigKey()))
            put(key.getConfigKey(), defaultValue);

        return getAsDouble(key.getConfigKey(), defaultValue);
    }

    public String getAsString(String key) {
        JsonNode value = root.get(key);
        return value.asText();
    }

    public boolean getAsBoolean(String key, boolean defaultValue) {
        JsonNode value = root.get(key);
        if (value == NullNode.getInstance())
            return defaultValue;
        return value.asBoolean(defaultValue);
    }

    public int getAsInt(String key, int defaultValue) {
        JsonNode value = root.get(key);
        if (value == NullNode.getInstance())
            return defaultValue;
        return value.asInt();
    }

    public double getAsDouble(String key, double defaultValue) {
        JsonNode value = root.get(key);
        if (value == NullNode.getInstance())
            return defaultValue;
        return value.asDouble();
    }

    public <T> T getAsType(String key, TypeReference<T> type, T defaultValue) throws IOException {
        JsonNode value = root.get(key);
        if (value == null)
            return defaultValue;
        return JsonUtils.readJsonParser(value.traverse(), type);
    }

    public void put(String key, Object value) {
        root.put(key, (JsonNode) JsonUtils.toJsonNode(value));
    }

    public void putJson(String key, String json) {
        try {
            root.put(key, JsonUtils.readTree(json));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void putJsonNode(String key, JsonNode json) {
        root.put(key, json);
    }
}