Example usage for org.json.simple.parser ParseException getPosition

List of usage examples for org.json.simple.parser ParseException getPosition

Introduction

In this page you can find the example usage for org.json.simple.parser ParseException getPosition.

Prototype

public int getPosition() 

Source Link

Usage

From source file:processingtest.CitySense.java

public ArrayList<CityData> getDayDataByLocation() throws IOException {
    String query = queryBuilder.getDayDataByLocation(2); //Over 2 days
    CloseableHttpResponse response = this.executeRequest(query);
    HttpEntity entity = response.getEntity();
    ArrayList<CityData> dataPoints = null;

    if (entity != null) {
        try ( // A Simple JSON Response Read
                InputStream instream = entity.getContent()) {
            String result = convertStreamToString(instream);
            // now you have the string representation of the HTML request
            //System.out.println("RESPONSE: " + result);

            JSONParser parser = new JSONParser();
            try {
                Object obj = parser.parse(result);
                JSONObject jobj = (JSONObject) obj;
                //System.out.println(jobj.entrySet());
                Object dataObject = jobj.get("data");
                JSONArray array = (JSONArray) dataObject;
                //System.out.println(array.get(0));
                dataPoints = decodeJsonData(array);
                System.out.println((dataPoints.size()));
            } catch (ParseException pe) {
                System.out.println("position: " + pe.getPosition());
                System.out.println(pe);
            }//ww  w. j a v  a 2s .c om
        }
    }
    return dataPoints;
}

From source file:processingtest.CitySense.java

public ArrayList<CityData> getDayDataBySource() throws URISyntaxException, IOException {
    String query = queryBuilder.getDayDataBySource(2); //Over 2 days
    CloseableHttpResponse response = this.executeRequest(query);
    HttpEntity entity = response.getEntity();
    ArrayList<CityData> dataPoints = null;

    if (entity != null) {
        try ( // A Simple JSON Response Read
                InputStream instream = entity.getContent()) {
            String result = convertStreamToString(instream);
            // now you have the string representation of the HTML request
            //System.out.println("RESPONSE: " + result);

            JSONParser parser = new JSONParser();
            try {
                Object obj = parser.parse(result);
                JSONObject jobj = (JSONObject) obj;
                //System.out.println(jobj.entrySet());
                Object dataObject = jobj.get("data");
                JSONArray array = (JSONArray) dataObject;
                //System.out.println(array.get(0));
                dataPoints = decodeJsonData(array);
                System.out.println((dataPoints.size()));
            } catch (ParseException pe) {
                System.out.println("position: " + pe.getPosition());
                System.out.println(pe);
            }//from   ww  w.  j a  va 2  s . com
        }
    }
    return dataPoints;
}

From source file:de.hstsoft.sdeep.SaveGameParser.java

public SaveGame parse(File file) throws IOException {

    SaveGame saveGame = new SaveGame();
    Reader reader = new BufferedReader(new FileReader(file));
    try {//from   ww  w  .j  ava  2  s  .  co m
        JSONParser jsonParser = new JSONParser();
        JSONObject json = (JSONObject) jsonParser.parse(reader);

        JSONObject savegameJson = (JSONObject) json.get("Persistent");

        updateStateMsg("Parsing TerrainGeneration");
        JSONObject tgJson = (JSONObject) savegameJson.get("TerrainGeneration");
        TerrainGeneration terrainGeneration = TerrainGeneration.parse(tgJson);
        saveGame.setTerrainGeneration(terrainGeneration);

        updateStateMsg("Parsing PlayerStatistics");
        JSONObject psJson = (JSONObject) savegameJson.get("PlayerStatistics");
        PlayerStatistics playerStatistic = PlayerStatistics.parse(psJson);
        saveGame.setPlayerStatistic(playerStatistic);

        updateStateMsg("Parsing PlayerCrafting");
        JSONObject pcJson = (JSONObject) savegameJson.get("PlayerCrafting");
        PlayerCrafting playerCrafting = PlayerCrafting.parse(pcJson);
        saveGame.setPlayerCrafting(playerCrafting);

        updateStateMsg("Parsing FollowSpawn");
        JSONObject fsJson = (JSONObject) savegameJson.get("FollowSpawn");
        FollowSpawn followSpawn = FollowSpawn.parse(fsJson);
        saveGame.setFollowSpawn(followSpawn);

        updateStateMsg("Parsing PlayerMovement");
        JSONObject pmJson = (JSONObject) savegameJson.get("PlayerMovement");
        PlayerMovement playerMovement = PlayerMovement.parse(pmJson);
        saveGame.setPlayerMovement(playerMovement);

        updateStateMsg("Parsing Atmosphere");
        JSONObject asJson = (JSONObject) savegameJson.get("Atmosphere");
        Atmosphere atmosphere = Atmosphere.parse(asJson);
        saveGame.setAtmosphere(atmosphere);

        updateStateMsg("Parsing StatsManager");
        JSONObject smJson = (JSONObject) savegameJson.get("StatsManager");
        StatsManager statsManager = StatsManager.parse(smJson);
        saveGame.setStatsManager(statsManager);

        updateStateMsg("Parsing PlayerInventory");
        JSONObject piJson = (JSONObject) savegameJson.get("PlayerInventory");
        PlayerInventory playerInventory = PlayerInventory.parse(piJson);
        saveGame.setPlayerInventory(playerInventory);

        saveGame.setFile(file.getPath());

    } catch (ParseException e) {
        System.out.println("Error parsing SavegameFile at line: " + e.getPosition());
        e.printStackTrace();
    } finally {
        reader.close();
    }

    saveGame.updateItemTypeSet();

    System.out.println("Parsed file:" + file.toString());

    return saveGame;
}

From source file:jenkins.plugins.testrail.util.TestRailJsonParser.java

/**
 *
 * @param json/*from w w  w.  j  a  v  a2  s .  c  o  m*/
 * @return
 */
public String getProjectId(String json) {
    List<String> returnList = new ArrayList<String>();
    System.out.println("Parsing json");
    try {
        JSONObject rootJsonObject = (JSONObject) new JSONParser().parse(json);
        return rootJsonObject.get("project_id").toString();
    } catch (ParseException pe) {
        System.out.println("Exception caught: " + pe.getPosition());
        System.out.println(pe);
    }

    return null;
}

From source file:jenkins.plugins.testrail.util.TestRailJsonParser.java

/**
 *
 * @param json/* w  ww.  j av a2 s .  co m*/
 * @return
 */
public String getNewPlanId(String json) {
    List<String> returnList = new ArrayList<String>();
    System.out.println("Parsing json");
    try {
        JSONObject rootJsonObject = (JSONObject) new JSONParser().parse(json);
        return rootJsonObject.get("id").toString();
    } catch (ParseException pe) {
        System.out.println("Exception caught: " + pe.getPosition());
        System.out.println(pe);
    }

    return null;
}

From source file:jenkins.plugins.testrail.util.TestRailJsonParser.java

/**
 *
 * @param json//from w w w  .j a v  a2 s.c om
 * @return
 */
public List<String> decodeGetPlanJSON(String json) {
    List<String> returnList = new ArrayList<String>();
    System.out.println("Parsing json");
    try {
        JSONObject rootJsonObject = (JSONObject) new JSONParser().parse(json);
        JSONArray entriesJsonArray = (JSONArray) rootJsonObject.get("entries");
        for (Object entryObject : entriesJsonArray) {
            JSONObject entryJsonObject = (JSONObject) entryObject;
            JSONArray runsJsonArray = (JSONArray) entryJsonObject.get("runs");
            for (Object runObject : runsJsonArray) {
                JSONObject runJsonObject = (JSONObject) runObject;
                returnList.add(runJsonObject.get("id").toString());
            }
        }
    } catch (ParseException pe) {
        System.out.println("Exception caught: " + pe.getPosition());
        System.out.println(pe);
    }

    return returnList;
}

From source file:nosqltools.ActionPerformedOnDB.java

public boolean parseText(String x) {
    boolean flag = true;
    JSONParser parser = new JSONParser();
    try {/*from ww w . j  av a 2 s .  c om*/
        parser.parse(x);
    } catch (org.json.simple.parser.ParseException pe) {
        flag = false;
        if (x.isEmpty())
            JOptionPane.showMessageDialog(null, "There are no JSON Objects to " + action + "!", "Error",
                    JOptionPane.ERROR_MESSAGE);
        else
            JOptionPane.showMessageDialog(null,
                    "Incorrect JSON format - " + Initializations.ERRORLINE
                            + json_util.getLineNumber(pe.getPosition(), x) + " " + pe + "!",
                    "Error", JOptionPane.ERROR_MESSAGE);
    }

    return flag;
}

From source file:nosqltools.MainForm.java

/**
 * This event handler is used to save the JSON objects to the file chosen
 * @param evt /*from   w  w  w . j a  va2 s  .  c  o m*/
 */
private void Save_FileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Save_FileActionPerformed
    try {
        file = null;
        sb.setLength(0);

        //choosable file extensions 
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Text Files (" + ext + ")", ext_array);
        fc.setFileFilter(filter);

        //save is OK
        int returnVal = fc.showSaveDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            Save_File.setEnabled(true);
            file = fc.getSelectedFile();

            parser.parse(textArea.getText());
            //parse data
            if (json_util.isDataParsed(textArea.getText())) {
                try {
                    BufferedWriter writer = new BufferedWriter(new FileWriter(fc.getSelectedFile()));
                    writer.write(textArea.getText());
                    writer.close();
                    Text_MessageBar.setText(Initializations.JSONSAVESUCCESS);
                    Text_MessageBar.setForeground(Color.GREEN);
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else //case for saving error
            {
                Text_MessageBar.setForeground(Color.RED);
                Text_MessageBar.setText(Initializations.VALIDATIONERROR);
                JOptionPane.showMessageDialog(null,
                        Initializations.JSONSAVEERROR + Initializations.VALIDATIONERROR, "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        }
    } catch (org.json.simple.parser.ParseException pe) //case for incorrect JSON object
    {
        Text_MessageBar.setForeground(Color.RED);
        Text_MessageBar.setText(Initializations.JSONSAVEERROR + Initializations.ERRORLINE
                + json_util.getLineNumber(pe.getPosition(), textArea.getText()) + " - " + pe);
    }
}

From source file:nosqltools.MainForm.java

private void validateDataPanel_text(StringBuilder sb) {
    if (json_util.isValid(sb.toString())) {
        //  json_util.isDataParsed(textArea.getText());
        if (json_util.isDataParsed(sb.toString())) {
            Text_MessageBar.setText(Initializations.JSONFILESUCCESS);
            Text_MessageBar.setForeground(Color.GREEN);
        } else {/* www .  j  a  v  a  2s  . c  om*/
            Text_MessageBar.setText(Initializations.JSONINCORRECTFORMAT);
            Text_MessageBar.setForeground(Color.ORANGE);
            JOptionPane.showMessageDialog(null, Initializations.JSONINCORRECTFORMAT, "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
    } else {
        sb.setLength(0);
        JOptionPane.showMessageDialog(this, Initializations.JSONINCORRECTFORMAT,
                Initializations.VALIDATIONERROR, JOptionPane.ERROR_MESSAGE);

        try {
            Object obj = parser.parse(sb.toString());
        } catch (org.json.simple.parser.ParseException pe) {
            Text_MessageBar.setText(Initializations.ERRORLINE
                    + json_util.getLineNumber(pe.getPosition(), textArea.getText()) + " - " + pe);
            Text_MessageBar.setForeground(Color.ORANGE);
        }
    }
}

From source file:nosqltools.MainForm.java

private void Save_MongoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_Save_MongoActionPerformed
    // Text_MessageBar.setText(Initializations.WAITINGFORCONNECTION);
    //  Text_MessageBar.setForeground(Color.ORANGE);
    if (dbcon.isConnectionSuccess()) {
        if (dbcon.checkDatabaseConnection()) {

            if (indexOfCurrentCollection != 0) {
                try {
                    file = null;//  www.ja  v  a 2 s .  c  o m
                    sb.setLength(0);

                    if (dbcon.isConnectionSuccess() && textArea.getText() != null) {
                        Save_Mongo.setEnabled(true);

                        parser.parse(textArea.getText());

                        String json = textArea.getText();

                        if (dbcon.saveColl(json)) {
                            Text_MessageBar.setText(Initializations.JSONSAVESUCCESS);
                            Text_MessageBar.setForeground(Color.GREEN);
                        } else {
                            Text_MessageBar.setText(Initializations.VALIDATIONERROR);
                            Text_MessageBar.setForeground(Color.RED);
                        }

                    }
                } catch (org.json.simple.parser.ParseException pe) {
                    Text_MessageBar.setForeground(Color.RED);
                    Text_MessageBar.setText(Initializations.ERRORLINE
                            + json_util.getLineNumber(pe.getPosition(), textArea.getText()) + " - " + pe);
                }
            } else {
                Text_MessageBar.setText(Initializations.DBACTIONNOCOLLECTION);
                Text_MessageBar.setForeground(Color.RED);
                JOptionPane.showMessageDialog(null, Initializations.DBACTIONNOCOLLECTION, "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
        } else {
            JOptionPane.showMessageDialog(null, Initializations.MONGOSERVERERROR, "Error",
                    JOptionPane.ERROR_MESSAGE);
            Text_MessageBar.setText(Initializations.MONGOSERVERERROR);
            Text_MessageBar.setForeground(Color.RED);
        }
    } else {
        JOptionPane.showMessageDialog(null, Initializations.NODBCONNECTION, "Error", JOptionPane.ERROR_MESSAGE);
        Text_MessageBar.setText(Initializations.NODBCONNECTION);
        Text_MessageBar.setForeground(Color.RED);
    }
}