Example usage for com.google.gson.stream JsonReader JsonReader

List of usage examples for com.google.gson.stream JsonReader JsonReader

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader JsonReader.

Prototype

public JsonReader(Reader in) 

Source Link

Document

Creates a new instance that reads a JSON-encoded stream from in .

Usage

From source file:fr.inria.atlanmod.discoverer.JsonSource.java

License:Open Source License

/**
 * Adds a new JSON definition from a string with the JSON and a provided input
 * //  w w w.j a v  a 2  s.c o  m
 * @param file
 * @param input
 * @throws FileNotFoundException 
 */
public void addJsonDef(String string, String input) {
    if (string == null || string.equals(""))
        throw new IllegalArgumentException("Argument cannot be null or empty");
    if (this.jsonData.size() > 0 && this.withInput == true)
        throw new IllegalStateException("This JSON source was created to hold JSON data *with* input");
    if (input == null || input.equals(""))
        throw new IllegalArgumentException("Argument cannot be null or empty");

    JsonElement inputElement = (new JsonParser()).parse(new JsonReader(new StringReader(input)));
    if (!inputElement.isJsonObject())
        throw new JsonParseException("The input value must be a valid JSON object. Received " + input);

    JsonElement rootElement = (new JsonParser()).parse(new JsonReader(new StringReader(string)));
    JsonData data = new JsonData(inputElement.getAsJsonObject(), rootElement);
    getJsonData().add(data);
    this.withInput = true;
}

From source file:fr.inria.atlanmod.discoverer.JsonSource.java

License:Open Source License

/**
 * Adds a new JSON definition from a file with the JSON
 * /*  w  w w.jav  a2s. c o m*/
 * Warning: If the JSON source already includes JSON Data, the provision of input must match
 * 
 * @param file
 * @throws FileNotFoundException 
 */
public void addJsonDef(File file) throws FileNotFoundException {
    if (file == null || !file.exists())
        throw new IllegalArgumentException("File cannot be null and must exist");
    if (this.jsonData.size() > 0 && this.withInput == true)
        throw new IllegalStateException("This JSON source was created to hold JSON data *with* input");
    JsonElement rootElement = (new JsonParser()).parse(new JsonReader(new FileReader(file)));
    JsonData data = new JsonData(null, rootElement);
    getJsonData().add(data);
    this.withInput = false;
}

From source file:fr.inria.atlanmod.discoverer.JsonSource.java

License:Open Source License

/**
 * Adds a new JSON definition from a string with the JSON
 * /*from w  ww  . j  a  v  a  2 s.co m*/
 * @param file
 * @throws FileNotFoundException 
 */
public void addJsonDef(String string) {
    if (string == null || string.equals(""))
        throw new IllegalArgumentException("Argument cannot be null or empty");
    if (this.jsonData.size() > 0 && this.withInput == true)
        throw new IllegalStateException("This JSON source was created to hold JSON data *with* input");
    JsonElement rootElement = (new JsonParser()).parse(new JsonReader(new StringReader(string)));
    JsonData data = new JsonData(null, rootElement);
    getJsonData().add(data);
    this.withInput = false;
}

From source file:framework.mod.settings.model.clss.profile_json.java

/**
 * Settings_Autoload load automatically the file settings.json from the path
 * /src/framework/clss/files/settings.json to singletonS.Alist_sett
 *
 */// w w w .ja  v a 2s.co  m
public static void Profiles_Autoload() {
    String PATH;
    Profiles sett;
    singletonProfile.Alist_Profile.clear();

    try {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("Profiles", Profiles.class);

        PATH = new java.io.File(".").getCanonicalPath()
                + "/src/framework/mod/settings/model/files/profiles.json";

        JsonReader read = new JsonReader(new FileReader(PATH));
        JsonParser parseator = new JsonParser();
        JsonElement root = parseator.parse(read);

        Gson json = new Gson();

        JsonArray list = root.getAsJsonArray();
        for (JsonElement elem : list) {
            sett = json.fromJson(elem, Profiles.class);
            singletonProfile.Alist_Profile.add(sett);
        }
    } catch (IOException | JsonIOException | JsonSyntaxException e) {
        System.out.println("ERR auto-loading Profiles");
    }
}

From source file:framework.mod.user.admin.model.tools.json.java

/**AdminJson_load() function load 
 * from selected file /*from  w  w  w  . j  a v  a2  s. com*/
 * to singletonU.Alist_adm 
 * @return singletonU.Alist_adm
 */
public static ArrayList<Admin> AdminJson_load() {
    String PATH = null;
    Admin adm = new Admin();

    try {

        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("Admin", Admin.class);

        JFileChooser fileChooser = new JFileChooser();
        int seleccion = fileChooser.showOpenDialog(null);
        if (seleccion == JFileChooser.APPROVE_OPTION) {
            File JFC = fileChooser.getSelectedFile();
            PATH = JFC.getAbsolutePath();

            singletonAdmin.AdminTableArray.clear();
            //singletonU.Alist_adm = (ArrayList<Admin>)xstream.fromXML(new FileReader(PATH));
            //singletonU.Alist_adm.addAll((ArrayList<Admin>)xstream.fromXML(new FileReader(PATH)));

            JsonReader jreader = new JsonReader(new FileReader(PATH));
            JsonParser jparser = new JsonParser();
            JsonElement raiz = jparser.parse(jreader);

            Gson json = new Gson();
            JsonArray list = raiz.getAsJsonArray();
            for (JsonElement elem : list) {
                adm = json.fromJson(elem, Admin.class);
                singletonAdmin.AdminTableArray.add(adm);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "ERROR reading JSON file", "JSON file read ERROR",
                JOptionPane.ERROR_MESSAGE);
    }
    return singletonAdmin.AdminTableArray;
}

From source file:framework.mod.user.admin.model.tools.json.java

/**AdminJson_Autoload load automatically the file admin.json 
 * from the path /src/framework/mod/user/model/files/admin.json
 * to singletonU.Alist_adm //from ww w.j  av a  2 s . c o  m
 * 
 */
public static void AdminJson_Autoload() {
    String PATH;
    Admin adm = new Admin();
    singletonAdmin.AdminTableArray.clear();

    try {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("Admin", Admin.class);

        PATH = new java.io.File(".").getCanonicalPath() + singletonAdmin.PATH_JSON;

        JsonReader read = new JsonReader(new FileReader(PATH));
        JsonParser parseator = new JsonParser();
        JsonElement root = parseator.parse(read);

        Gson json = new Gson();

        JsonArray list = root.getAsJsonArray();
        for (JsonElement elem : list) {
            adm = json.fromJson(elem, Admin.class);
            singletonAdmin.AdminTableArray.add(adm);
        }
    } catch (Exception e) {
        System.out.println("ERR auto-loading Json");
    }
}

From source file:framework.mod.user.client.model.tools.jsonClt.java

/**
 * ClientJson_Autoload load automatically the file client_dummies.json from
 * the path /src/framework/mod/user/model/files/client_dummies.json to
 * singletonU.Alist_clt//from   www . ja  v a 2s  .  c om
 *
 */
public static void ClientJson_Autoload() {
    String PATH;
    Client clt = new Client();

    try {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("Client", Client.class);

        PATH = new java.io.File(".").getCanonicalPath() + singletonClient.PATH_JSON;

        JsonReader read = new JsonReader(new FileReader(PATH));
        JsonParser parseator = new JsonParser();
        JsonElement root = parseator.parse(read);

        Gson json = new Gson();

        JsonArray list = root.getAsJsonArray();
        for (JsonElement elem : list) {
            clt = json.fromJson(elem, Client.class);
            singletonClient.ClienTableArray.add(clt);
        }
    } catch (Exception e) {
        System.out.println("ERR auto-loading Json");
    }
}

From source file:framework.mod.user.registered.model.tools.jsonReg.java

/**
 * RegJson_load() function load from selected file to singletonReg.RegTableArray
 *
 * @return singletonU.Alist_adm// w w w.ja  va2  s  . c  om
 */
public static ArrayList<RegisteredU> RegJson_load() {
    String PATH = null;
    RegisteredU regu = new RegisteredU();

    try {

        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("RegU", RegisteredU.class);

        JFileChooser fileChooser = new JFileChooser();
        int seleccion = fileChooser.showOpenDialog(null);
        if (seleccion == JFileChooser.APPROVE_OPTION) {
            File JFC = fileChooser.getSelectedFile();
            PATH = JFC.getAbsolutePath();

            singletonReg.RegTableArray.clear();

            JsonReader jreader = new JsonReader(new FileReader(PATH));
            JsonParser jparser = new JsonParser();
            JsonElement raiz = jparser.parse(jreader);

            Gson json = new Gson();
            JsonArray list = raiz.getAsJsonArray();
            for (JsonElement elem : list) {
                regu = json.fromJson(elem, RegisteredU.class);
                singletonReg.RegTableArray.add(regu);
            }
        }
    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "ERROR reading JSON file", "JSON file read ERROR",
                JOptionPane.ERROR_MESSAGE);
    }
    return singletonReg.RegTableArray;
}

From source file:framework.mod.user.registered.model.tools.jsonReg.java

/**
 * RegJson_Autoload load automatically the file jsonRegDB.json from the path
 * /src/framework/mod/user/model/files/jsonRegDB.json to singletonReg.RegTableArray
 *
 *///w  w w  . j a  va  2 s .  c  om
public static void RegJson_Autoload() {
    String PATH;
    RegisteredU regu = new RegisteredU();
    singletonReg.RegTableArray.clear();

    try {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("RegU", RegisteredU.class);

        PATH = new java.io.File(".").getCanonicalPath() + singletonReg.PATH_JSON;

        JsonReader read = new JsonReader(new FileReader(PATH));
        JsonParser parseator = new JsonParser();
        JsonElement root = parseator.parse(read);

        Gson json = new Gson();

        JsonArray list = root.getAsJsonArray();
        for (JsonElement elem : list) {
            regu = json.fromJson(elem, RegisteredU.class);
            singletonReg.RegTableArray.add(regu);
        }
    } catch (Exception e) {
        System.out.println("ERR auto-loading Json");
    }
}

From source file:framework.modules.users.admin.Model.utils.lib_Afiles.A_auto_json.java

public static void auto_openjson_admin() {

    String PATH = null;/* w  ww .  j a  va 2  s.c  om*/
    admin_class a = new admin_class("");

    try {
        XStream xstream = new XStream(new JettisonMappedXmlDriver());
        xstream.setMode(XStream.NO_REFERENCES);
        xstream.alias("admin_class", admin_class.class);

        PATH = new java.io.File(".").getCanonicalPath()
                + "/src/framework/modules/users/admin/Model/utils/admin_files/json/auto_json.json";

        File path = new File(PATH);

        if (path.exists()) {
            JsonReader reader = new JsonReader(new FileReader(PATH));
            JsonParser parser = new JsonParser();
            JsonElement root = parser.parse(reader);

            Gson json = new Gson();
            JsonArray list = root.getAsJsonArray();
            for (JsonElement element : list) {
                a = json.fromJson(element, admin_class.class);
                singleton_admin.admin.add(a);
                //singleton.admin = (ArrayList<Usuarios>) xstream.fromXML(new FileReader(PATH));
            }
        }
    } catch (Exception e) {

        JOptionPane.showMessageDialog(null, class_language.getinstance().getProperty("error_open_json"),
                "Error", JOptionPane.ERROR_MESSAGE);
    }

}