semblance.json.JSONParser.java Source code

Java tutorial

Introduction

Here is the source code for semblance.json.JSONParser.java

Source

/*
 * Copyright (C) 2014 balnave
 *
 * 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 semblance.json;

import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import semblance.io.IReader;
import semblance.io.ReaderFactory;

/**
 * Loads a JSON JSONParser { urls: [String, String], dir : String }
 *
 * @author balnave
 */
public class JSONParser {

    private Object json;

    public JSONParser(String pathToJson) {
        this.json = null;
        IReader reader = ReaderFactory.getReader(pathToJson);
        String source = reader.load();
        if (!source.isEmpty()) {
            try {
                json = (Map<String, Object>) JSONValue.parseWithException(source);
            } catch (ParseException ex) {
                Logger.getLogger(getClass().getName()).log(Level.SEVERE, "Exception loading JSON config", ex);
            }
        } else {
            Logger.getLogger(getClass().getName())
                    .severe(String.format("Unable to load JSON config %s", pathToJson));
        }
    }

    public Object getJson() {
        return json;
    }

}