net.chunkyhosting.Roe.CHGManagerLauncher.utils.JSON.java Source code

Java tutorial

Introduction

Here is the source code for net.chunkyhosting.Roe.CHGManagerLauncher.utils.JSON.java

Source

/*
 * CHGManager Launcher
 * Copyright (C) 2012-2013 Chunky Hosting LLC
 * Made by: Roe: http://icake.pw
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.chunkyhosting.Roe.CHGManagerLauncher.utils;

import java.io.*;

import org.json.JSONObject;

/**
 * @author Roe: http://icake.pw
 * Created 13.41.07 - 10/07/2013
 *
 */
public class JSON {

    public static void writeJsonToFile(JSONObject json, File file) throws IOException {

        FileWriter writer = new FileWriter(file.toString());

        try {

            writer.write(json.toString(4));

        } finally {

            writer.flush();
            writer.close();

        }

    }

    public static JSONObject readJsonFromFile(File file)
            throws FileNotFoundException, IOException, NullPointerException {

        String json = "NULL";
        BufferedReader reader = new BufferedReader(new FileReader(file.toString()));
        String read = "";
        while ((read = reader.readLine()) != null) {

            json = json + read;

        }

        reader.close();

        if (!json.equalsIgnoreCase("NULL")) {

            return stringToJson(json);

        }

        throw new NullPointerException();

    }

    public static JSONObject stringToJson(String json) {

        return new JSONObject(json);

    }

}