Java Code Examples for org.json.simple.parser.JSONParser

The following examples show how to use org.json.simple.parser.JSONParser. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Builds URL params from input JSON string
 *
 * @param builder
 * @param jsonStr
 * @return
 * @throws ParseException
 */
public URIBuilder setParams(URIBuilder builder, String jsonStr) throws ParseException {

    if (jsonStr != null && !"".equals(jsonStr)) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject json = (JSONObject) parser.parse(jsonStr);
            json.keySet().forEach((Key) -> {
                builder.setParameter(Key.toString(), (String) json.get(Key));
            });
        } catch (Exception ex) {
            DLogger.LogE(ex.getMessage());
            LOG.log(Level.SEVERE, ex.getMessage(), ex);
        }

    }

    return builder;
}
 
Example 2
Source Project: BedrockConnect   Source File: UIComponents.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static ArrayList<String> getFormData(String data) {
    JSONParser parser = new JSONParser();

    // If no server data
    if(data == null)
        return new ArrayList<>();

    try {
        JSONArray obj = (JSONArray) parser.parse(data);
        ArrayList<String> strings = new ArrayList<>();
        for(int i=0;i<obj.size();i++) {
            strings.add(obj.get(i).toString());
        }
        return strings;
    } catch(ParseException e) {
        System.out.println(e.toString());
    }

    return null;
}
 
Example 3
Source Project: cellery-security   Source File: CellStsUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static void buildCellStsConfiguration() throws CelleryCellSTSException {

        try {
            String configFilePath = CellStsUtils.getConfigFilePath();
            String content = new String(Files.readAllBytes(Paths.get(configFilePath)), StandardCharsets.UTF_8);
            JSONObject config = (JSONObject) new JSONParser().parse(content);

            CellStsConfiguration.getInstance()
                    .setCellName(getMyCellName())
                    .setStsEndpoint((String) config.get(Constants.Configs.CONFIG_STS_ENDPOINT))
                    .setUsername((String) config.get(Constants.Configs.CONFIG_AUTH_USERNAME))
                    .setPassword((String) config.get(Constants.Configs.CONFIG_AUTH_PASSWORD))
                    .setGlobalJWKEndpoint((String) config.get(Constants.Configs.CONFIG_GLOBAL_JWKS))
                    .setSignatureValidationEnabled(Boolean.parseBoolean(String.valueOf(config.get
                            (Constants.Configs.CONFIG_SIGNATURE_VALIDATION_ENABLED))))
                    .setAudienceValidationEnabled(Boolean.parseBoolean(String.valueOf(config.get
                            (Constants.Configs.CONFIG_AUDIENCE_VALIDATION_ENABLED))))
                    .setIssuerValidationEnabled(Boolean.parseBoolean(String.valueOf(config.get
                            (Constants.Configs.CONFIG_ISSUER_VALIDATION_ENABLED))))
                    .setSTSOPAQueryPrefix((String) config.get(Constants.Configs.CONFIG_OPA_PREFIX))
                    .setAuthorizationEnabled(Boolean.parseBoolean(String.valueOf(config.get
                            (Constants.Configs.CONFIG_AUTHORIZATION_ENABLED))));
        } catch (ParseException | IOException e) {
            throw new CelleryCellSTSException("Error while setting up STS configurations", e);
        }
    }
 
Example 4
Source Project: Explvs-AIO   Source File: RSBuddyPriceGuide.java    License: MIT License 6 votes vote down vote up
private static Optional<Integer> getPrice(final int itemID, final String type) {
    try {
        URL url = new URL("http://api.rsbuddy.com/grandExchange?a=guidePrice&i=" + itemID);
        URLConnection con = url.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

        try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            JSONParser jsonParser = new JSONParser();
            JSONObject itemJSON = (JSONObject) jsonParser.parse(bufferedReader);

            int price = new Long((long) itemJSON.get(type)).intValue();

            System.out.println("Got RSBuddy price");
            return Optional.of(price);
        }
    } catch (Exception e) {
        System.out.println("Failed to get RSBuddy price");
    }
    return Optional.empty();
}
 
Example 5
Source Project: Explvs-AIO   Source File: LoadoutPanel.java    License: MIT License 6 votes vote down vote up
private Optional<URL> getIcon(final int itemID) {
    try {
        URL url = new URL("http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + itemID);
        URLConnection con = url.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

        try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            JSONParser jsonParser = new JSONParser();
            JSONObject json = (JSONObject) jsonParser.parse(bufferedReader);

            JSONObject itemJSON = (JSONObject) json.get("item");
            String iconURL = (String) itemJSON.get("icon");

            return Optional.of(new URL(iconURL));
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Failed to get icon from RuneScape api");
    }
    return Optional.empty();
}
 
Example 6
Source Project: soltix   Source File: ParserASTJSON.java    License: Apache License 2.0 6 votes vote down vote up
public AST parse(InputStream input) throws Exception {
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = (JSONObject)jsonParser.parse(
            new InputStreamReader(input, "UTF-8"));

    AST ast = new AST();

    if (!Configuration.skipASTProcessing) {
        // Process JSON object to build AST
        JSONObjectToAST(ast, jsonObject, 0);
    } else {
        // For debugging: only print, don't interpret
        printJSONObject(jsonObject, 0);
    }
    return ast;
}
 
Example 7
Source Project: hop   Source File: JsonRowMeta.java    License: Apache License 2.0 6 votes vote down vote up
public static IRowMeta fromJson(String rowMetaJson) throws ParseException, HopPluginException {
  JSONParser parser = new JSONParser();
  JSONObject jRowMeta = (JSONObject) parser.parse( rowMetaJson );

  IRowMeta rowMeta = new RowMeta(  );

  JSONArray jValues = (JSONArray) jRowMeta.get("values");
  for (int v=0;v<jValues.size();v++) {
    JSONObject jValue = (JSONObject) jValues.get( v );
    String name = (String) jValue.get("name");
    long type = (long)jValue.get("type");
    long length = (long)jValue.get("length");
    long precision = (long)jValue.get("precision");
    String conversionMask = (String) jValue.get("conversionMask");
    IValueMeta valueMeta = ValueMetaFactory.createValueMeta( name, (int)type, (int)length, (int)precision );
    valueMeta.setConversionMask( conversionMask );
    rowMeta.addValueMeta( valueMeta );
  }

  return rowMeta;

}
 
Example 8
Source Project: singleton   Source File: MultComponentService.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@SuppressWarnings({"unchecked" })
private TranslationDTO getTranslation(TranslationDTO translationDTO)
		throws ParseException, DataException {
	List<String> locales = translationDTO.getLocales();
	List<String> components = translationDTO.getComponents();
	List<String> bundles = multipleComponentsDao.get2JsonStrs(
			translationDTO.getProductName(), translationDTO.getVersion(),
			components, locales);
	JSONArray ja = new JSONArray();
	for (int i = 0; i < bundles.size(); i++) {
		String s = (String) bundles.get(i);
		if (s.equalsIgnoreCase("")) {
			continue;
		}
		JSONObject jo = (JSONObject) new JSONParser().parse(s);
		ja.add(jo);
	}
	translationDTO.setBundles(ja);
	return translationDTO;
}
 
Example 9
Source Project: VoxelGamesLibv2   Source File: MojangUtil.java    License: MIT License 6 votes vote down vote up
/**
 * Tries to fetch the current display name for the user
 *
 * @param id the id of the user to check
 * @return the current display name of that user
 * @throws IOException           if something goes wrong
 * @throws VoxelGameLibException if the user has no display name
 */
@Nonnull
public static String getDisplayName(@Nonnull UUID id) throws IOException, VoxelGameLibException {
    URL url = new URL(NAME_HISTORY_URL.replace("%1", id.toString().replace("-", "")));
    System.out.println(url.toString());
    Scanner scanner = new Scanner(new BufferedReader(new InputStreamReader(url.openStream())));
    if (scanner.hasNext()) {
        String json = scanner.nextLine();
        try {
            JSONArray jsonArray = (JSONArray) new JSONParser().parse(json);
            if (json.length() > 0) {
                return (String) ((JSONObject) jsonArray.get(0)).get("name");
            }
        } catch (ParseException ignore) {
        }
    }

    throw new VoxelGameLibException("User has no name! " + id);
}
 
Example 10
Source Project: singleton   Source File: SingleComponentDTO.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Get the ordered SingleComponentDTO from one JSON string
 *
 * @param jsonStr One JSON string can convert to a SingleComponentDTO object
 * @return SingleComponentDTO
 */
@SuppressWarnings("unchecked")
public static SingleComponentDTO getSingleComponentDTOWithLinkedMessages(String jsonStr)
        throws ParseException {
    JSONParser parser = new JSONParser();
    ContainerFactory containerFactory = getContainerFactory();
    Map<String, Object> messages = new LinkedHashMap<String, Object>();
    Map<String, Object> bundle = null;
    bundle = (Map<String, Object>) parser.parse(jsonStr, containerFactory);
    if (bundle == null) {
        return null;
    }
    SingleComponentDTO baseComponentMessagesDTO = new SingleComponentDTO();
    baseComponentMessagesDTO.setId(Long.parseLong(bundle.get(ConstantsKeys.ID) == null ? "0" : bundle.get(ConstantsKeys.ID).toString()));
    baseComponentMessagesDTO.setComponent((String) bundle.get(ConstantsKeys.COMPONENT));
    baseComponentMessagesDTO.setLocale((String) bundle.get(ConstantsKeys.lOCALE));
    messages = (Map<String, Object>) bundle.get(ConstantsKeys.MESSAGES);
    baseComponentMessagesDTO.setMessages(messages);
    return baseComponentMessagesDTO;
}
 
Example 11
Source Project: kettle-beam   Source File: JsonRowMeta.java    License: Apache License 2.0 6 votes vote down vote up
public static RowMetaInterface fromJson(String rowMetaJson) throws ParseException, KettlePluginException {
  JSONParser parser = new JSONParser();
  JSONObject jRowMeta = (JSONObject) parser.parse( rowMetaJson );

  RowMetaInterface rowMeta = new RowMeta(  );

  JSONArray jValues = (JSONArray) jRowMeta.get("values");
  for (int v=0;v<jValues.size();v++) {
    JSONObject jValue = (JSONObject) jValues.get( v );
    String name = (String) jValue.get("name");
    long type = (long)jValue.get("type");
    long length = (long)jValue.get("length");
    long precision = (long)jValue.get("precision");
    String conversionMask = (String) jValue.get("conversionMask");
    ValueMetaInterface valueMeta = ValueMetaFactory.createValueMeta( name, (int)type, (int)length, (int)precision );
    valueMeta.setConversionMask( conversionMask );
    rowMeta.addValueMeta( valueMeta );
  }

  return rowMeta;

}
 
Example 12
Source Project: Explvs-AIO   Source File: RSBuddyPriceGuide.java    License: MIT License 6 votes vote down vote up
private static Optional<Integer> getPrice(final int itemID, final String type) {
    try {
        URL url = new URL("http://api.rsbuddy.com/grandExchange?a=guidePrice&i=" + itemID);
        URLConnection con = url.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

        try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            JSONParser jsonParser = new JSONParser();
            JSONObject itemJSON = (JSONObject) jsonParser.parse(bufferedReader);

            int price = new Long((long) itemJSON.get(type)).intValue();

            System.out.println("Got RSBuddy price");
            return Optional.of(price);
        }
    } catch (Exception e) {
        System.out.println("Failed to get RSBuddy price");
    }
    return Optional.empty();
}
 
Example 13
Source Project: Explvs-AIO   Source File: LoadoutPanel.java    License: MIT License 6 votes vote down vote up
private Optional<URL> getIcon(final int itemID) {
    try {
        URL url = new URL("http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + itemID);
        URLConnection con = url.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

        try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            JSONParser jsonParser = new JSONParser();
            JSONObject json = (JSONObject) jsonParser.parse(bufferedReader);

            JSONObject itemJSON = (JSONObject) json.get("item");
            String iconURL = (String) itemJSON.get("icon");

            return Optional.of(new URL(iconURL));
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Failed to get icon from RuneScape api");
    }
    return Optional.empty();
}
 
Example 14
Source Project: JFoolNLTK   Source File: Vocab.java    License: Apache License 2.0 6 votes vote down vote up
private void parseJson(String content) throws ParseException {
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(content);
    JSONObject jsonObject = (JSONObject) obj;

    Map<String, Integer> wordMap = parseStrMap((JSONObject) jsonObject.get("word_map"));
    Map<String, Integer> charMap = parseStrMap((JSONObject) jsonObject.get("char_map"));

    Map<Integer, String> segMap = parseIdMap((JSONObject) jsonObject.get("seg_map"));
    Map<Integer, String> posMap = parseIdMap((JSONObject) jsonObject.get("pos_map"));
    Map<Integer, String> nerMap = parseIdMap((JSONObject) jsonObject.get("ner_map"));

    charToId.setLabelToid(charMap);
    wordToId.setLabelToid(wordMap);
    idToSeg.setIdTolabel(segMap);
    idToPos.setIdTolabel(posMap);
    idToNer.setIdTolabel(nerMap);
}
 
Example 15
Source Project: proxylive   Source File: ChannelURLService.java    License: MIT License 6 votes vote down vote up
private JSONObject getJSONResponse(URL url) throws IOException, ParseException {
    JSONParser jsonParser = new JSONParser();
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setReadTimeout(10000);
    if (url.getUserInfo() != null) {
        String basicAuth = "Basic " + new String(Base64.getEncoder().encode(url.getUserInfo().getBytes()));
        connection.setRequestProperty("Authorization", basicAuth);
    }
    connection.setRequestMethod("GET");
    connection.connect();
    if (connection.getResponseCode() != 200) {
        throw new IOException("Error on open stream:" + url);
    }
    JSONObject returnObject = (JSONObject) jsonParser.parse(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    connection.disconnect();
    return returnObject;
}
 
Example 16
Source Project: netbeans   Source File: LibraryProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Update a library returned by {@link #findLibraries(java.lang.String)}.
 * The full library data is fetched and the {@code versions} property is
 * filled.
 *
 * @param library to be updated
 */
public void updateLibraryVersions(Library library) {
    Objects.nonNull(library);
    if(library.getVersions() != null && library.getVersions().length > 0) {
        return;
    }
    Library cachedLibrary = getCachedLibrary(library.getName());
    if(cachedLibrary != null) {
        library.setVersions(cachedLibrary.getVersions());
        return;
    }
    String data = readUrl(getLibraryDataUrl(library.getName()));
    if(data != null) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject libraryData = (JSONObject)parser.parse(data);
            updateLibrary(library, libraryData);
            entryCache.put(library.getName(), new WeakReference<>(library));
        } catch (ParseException ex) {
            Logger.getLogger(LibraryProvider.class.getName()).log(Level.INFO, null, ex);
        }
    }
}
 
Example 17
Source Project: netbeans   Source File: LibraryProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Load the full data for the supplied library. All fields are populated,
 * including the {@code versions} property.
 *
 * @param libraryName
 * @return
 */
public Library loadLibrary(String libraryName) {
    Library cachedLibrary = getCachedLibrary(libraryName);
    if(cachedLibrary != null) {
        return cachedLibrary;
    }
    String data = readUrl(getLibraryDataUrl(libraryName));
    if (data != null) {
        try {
            JSONParser parser = new JSONParser();
            JSONObject libraryData = (JSONObject) parser.parse(data);
            Library library = createLibrary(libraryData);
            entryCache.put(library.getName(), new WeakReference<>(library));
            return library;
        } catch (ParseException ex) {
            Logger.getLogger(LibraryProvider.class.getName()).log(Level.INFO, null, ex);
        }
    }
    return null;
}
 
Example 18
Source Project: netbeans   Source File: LibraryProvider.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Parses the given JSON result of the search.
 *
 * @param data search result.
 *
 * @return libraries returned in the search result.
 */
Library[] parse(String data) {
    Library[] libraries = null;
    try {
        JSONParser parser = new JSONParser();
        JSONObject searchResult = (JSONObject) parser.parse(data);
        JSONArray libraryArray = (JSONArray) searchResult.get(PROPERTY_RESULT);
        libraries = new Library[libraryArray.size()];
        for (int i = 0; i < libraries.length; i++) {
            JSONObject libraryData = (JSONObject) libraryArray.get(i);
            libraries[i] = createLibrary(libraryData);
        }
    } catch (ParseException pex) {
        Logger.getLogger(LibraryProvider.class.getName()).log(Level.INFO, null, pex);
    }
    return libraries;
}
 
Example 19
Source Project: netbeans   Source File: ServerConnection.java    License: Apache License 2.0 6 votes vote down vote up
private void received(Listener listener, String tools, String message) throws ParseException, IOException {
    //System.out.println("RECEIVED: tools: '"+tools+"', message: '"+message+"'");
    fireReceived(message);
    LOG.log(Level.FINE, "RECEIVED: {0}, {1}", new Object[]{tools, message});
    if (message.isEmpty()) {
        return ;
    }
    JSONParser parser = new JSONParser();
    JSONObject obj = (JSONObject) parser.parse(message, containerFactory);
    V8Request request = JSONReader.getRequest(obj);
    ResponseProvider rp = listener.request(request);
    if (V8Command.Disconnect.equals(request.getCommand())) {
        try {
            closeCurrentConnection();
        } catch (IOException ioex) {}
    }
    if (rp != null) {
        rp.sendTo(this);
    }
}
 
Example 20
public static JSONObject doExecuteJSONRequest(RemoteConnectorRequest request, RemoteConnectorService service) throws ParseException, IOException, AuthenticationException
{
    // Set as JSON
    request.setContentType(MimetypeMap.MIMETYPE_JSON);
    
    // Perform the request
    RemoteConnectorResponse response = service.executeRequest(request);
    
    // Parse this as JSON
    JSONParser parser = new JSONParser();
    String jsonText = response.getResponseBodyAsString();
    Object json = parser.parse(jsonText);
    
    // Check it's the right type and return
    if (json instanceof JSONObject)
    {
        return (JSONObject)json;
    }
    else
    {
        throw new ParseException(0, json);
    }
}
 
Example 21
Source Project: aws-lambda-proxy-java   Source File: LambdaProxyHandlerTest.java    License: MIT License 6 votes vote down vote up
@Test
public void shouldReturnServerErrorAndReasonWhenMisconfigured() throws ParseException {
    ApiGatewayProxyRequest request = new ApiGatewayProxyRequestBuilder()
            .withHttpMethod(METHOD)
            .build();
    LambdaProxyHandler<Configuration> handlerWithFailingConguration = new TestLambdaProxyHandlerWithFailingConguration();
    handlerWithFailingConguration.registerMethodHandler(METHOD, c -> methodHandler);

    ApiGatewayProxyResponse actual = handlerWithFailingConguration.handleRequest(request, context);

    assertThat(actual).isNotNull();
    assertThat(actual.getStatusCode()).isEqualTo(INTERNAL_SERVER_ERROR.getStatusCode());
    JSONParser jsonParser = new JSONParser();
    JSONObject jsonObject = (JSONObject) jsonParser.parse(actual.getBody());
    assertThat(jsonObject.keySet()).contains("message", "cause");
    assertThat((String) jsonObject.get("message")).contains("This service is mis-configured. Please contact your system administrator.");
    assertThat((String) jsonObject.get("cause")).contains("NullPointerException");
}
 
Example 22
Source Project: cellery-security   Source File: CellStsUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static void readUnsecuredContexts() throws CelleryCellSTSException {

        String configFilePath = CellStsUtils.getUnsecuredPathsConfigPath();
        String content = null;
        try {
            content = new String(Files.readAllBytes(Paths.get(configFilePath)), StandardCharsets.UTF_8);
            JSONArray config = (JSONArray) new JSONParser().parse(content);
            List unsecuredContexts = config.subList(0, config.size());
            CellStsConfiguration.getInstance().setUnsecuredAPIS(unsecuredContexts);
        } catch (IOException | ParseException e) {
            throw new CelleryCellSTSException("Error while reading unsecured contexts from config file", e);
        }
    }
 
Example 23
Source Project: Explvs-AIO   Source File: VersionChecker.java    License: MIT License 5 votes vote down vote up
private static Optional<JSONObject> getLatestGitHubReleaseJSON() {
    try {
        URL url = new URL(GITHUB_API_LATEST_RELEASE_URL);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");

        try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            return Optional.of((JSONObject) (new JSONParser().parse(bufferedReader)));
        }
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
    return Optional.empty();
}
 
Example 24
Source Project: Explvs-AIO   Source File: ItemGuide.java    License: MIT License 5 votes vote down vote up
public static Map<String, Integer> getAllGEItems() {
    if (allGEItems.isEmpty()) {

        File summaryFile = Paths.get(System.getProperty("user.home"), "OSBot", "Data", "explv_aio_rsbuddy_summary.json").toFile();

        try {
            if (!summaryFile.exists()) {
                System.out.println("Downloading summary JSON from RSBuddy");
                URL website = new URL("https://rsbuddy.com/exchange/summary.json");
                ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                FileOutputStream fos = new FileOutputStream(summaryFile);
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
            }

            try (FileReader fileReader = new FileReader(summaryFile)) {
                JSONObject jsonObject = (JSONObject) (new JSONParser().parse(fileReader));
                for (Object value : jsonObject.values()) {
                    JSONObject item = (JSONObject) value;
                    allGEItems.put((String) item.get("name"), new Long((long) item.get("id")).intValue());
                }
            }
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }
    return allGEItems;
}
 
Example 25
Source Project: Explvs-AIO   Source File: OSRSPriceGuide.java    License: MIT License 5 votes vote down vote up
public static Optional<Integer> getPrice(final int itemID) {
    try {
        URL url = new URL("http://services.runescape.com/m=itemdb_oldschool/api/catalogue/detail.json?item=" + itemID);
        URLConnection con = url.openConnection();
        con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");

        try (InputStreamReader inputStreamReader = new InputStreamReader(con.getInputStream());
             BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
            JSONParser jsonParser = new JSONParser();
            JSONObject json = (JSONObject) jsonParser.parse(bufferedReader);

            JSONObject itemJSON = (JSONObject) json.get("item");
            JSONObject currentPriceData = (JSONObject) itemJSON.get("current");

            Object currentPriceObj = currentPriceData.get("price");

            int currentPrice;

            if (currentPriceObj instanceof String) {
                currentPrice = parsePriceStr((String) currentPriceData.get("price"));
            } else if (currentPriceObj instanceof Long) {
                currentPrice = ((Long) currentPriceObj).intValue();
            } else {
                currentPrice = (int) currentPriceObj;
            }

            return Optional.of(currentPrice);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Failed to get price from RuneScape api");
    }
    return Optional.empty();
}
 
Example 26
Source Project: Explvs-AIO   Source File: ConfigManager.java    License: MIT License 5 votes vote down vote up
public Optional<JSONObject> readConfig(final File file) {
    if (!file.exists()) {
        return Optional.empty();
    }
    try (FileReader fileReader = new FileReader(file)) {
        JSONObject jsonObject = (JSONObject) (new JSONParser().parse(fileReader));
        return Optional.of(jsonObject);
    } catch (IOException | ParseException e) {
        e.printStackTrace();
    }
    return Optional.empty();
}
 
Example 27
Source Project: soltix   Source File: ProfilingLogRecording.java    License: Apache License 2.0 5 votes vote down vote up
public void loadEventLog(String path) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(path));
    String line;

    recordedResultList = new ArrayList<ProfilingEvent>();
    while ((line = br.readLine()) != null) {
        JSONParser jsonParser = new JSONParser();
        JSONObject jsonObject = (JSONObject)jsonParser.parse(line);

        ProfilingEvent event = ProfilingEvent.fromJSON(ast, jsonObject);
        if (event == null) {
            continue;
        }

        recordedResultList.add(event);

        if (!event.getIsOrdinaryUserEvent()) {
            /*
            // A log for multiple contracts must be allowed at least for inter-contract
            // calls
            if (contract != null) {
                if (contract != event.getContract()) {
                    throw new Exception("Event log contains events from more than one contract");
                }
            } else*/ {
                contract = event.getContract();
                // Infer the containing function of this first event as well
                firstEventFunction = ast.getFunctionByStatementId(event.getStatementID());
            }
        }
    }
    currentReplayIndex = 0;

    // Finalize environment variables
    for (ProfilingEvent eventData : recordedResultList) {
        if (eventData.getStatement() != null) {
            eventData.getStatement().getVariableEnvironment().finishAddingValues();
        }
    }
}
 
Example 28
Source Project: singleton   Source File: JSONUtils.java    License: Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Convert Json string to Map<String, Object>
 * 
 * @param json
 * @return Map<String, Object> obj
 */
@SuppressWarnings("unchecked")
public static Map<String, Object> getMapFromJson(String json) {
	JSONParser parser = new JSONParser();
	ContainerFactory containerFactory = getContainerFactory();
	Map<String, Object> result = null;
	if (!StringUtils.isEmpty(json)) {
		try {
			result = (Map<String, Object>) parser.parse(json, containerFactory);
		} catch (ParseException e) {
			logger.error(e.getMessage(), e);
		}
	}
	return result;
}
 
Example 29
Source Project: SZZUnleashed   Source File: GitParser.java    License: MIT License 5 votes vote down vote up
/**
 * Use this method to use already found big fixing changes.
 *
 * @param path the path to the json file where the changes are stored.
 */
public Set<RevCommit> readBugFixCommits(String path) throws IOException, GitAPIException {
  if (repo == null) return Collections.emptySet();

  this.issues = new Issues();

  JSONParser commitParser = new JSONParser();
  try {
    JSONObject object = (JSONObject) commitParser.parse(new FileReader(path));

    this.issues.revisions = new HashSet<>();
    this.issues.dates = new HashMap<>();

    for (Object issue : object.keySet()) {
      Map<String, String> issueInfo = (Map<String, String>) object.get(issue);

      String rev = issueInfo.get("hash");
      RevCommit revCommit = this.repo.parseCommit(this.repo.resolve(rev));

      Map<String, String> dates = new HashMap<>();

      dates.put("resolutiondate", issueInfo.get("resolutiondate"));
      dates.put("commitdate", issueInfo.get("commitdate"));
      dates.put("creationdate", issueInfo.get("creationdate"));

      this.issues.dates.put(rev, dates);
      this.issues.revisions.add(revCommit);
    }

  } catch (FileNotFoundException | ParseException e) {
    return Collections.emptySet();
  }

  this.logger.info(String.format("Found %d number of commits.", this.issues.revisions.size()));

  if (this.issues.revisions.size() == 0) return Collections.emptySet();
  return this.issues.revisions;
}
 
Example 30
Source Project: BPR   Source File: DatasetUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert the original Yelp Challenge datasets into .raw file for lexicon construction.
 * The .raw data is used by the tool thuir-sentires.jar.
 * The format is <DOC>review_text</DOC>
 * @param inputfileDir
 * @param dataset
 * @throws IOException
 * @throws ParseException
 * @throws java.text.ParseException
 */
public void ConvertJsonToRawFile(String inputfileDir, String dataset) throws IOException, ParseException, java.text.ParseException {
	String inputfileName = inputfileDir + dataset + ".json";
	String outputfileName = inputfileDir + dataset + ".raw";
	System.out.println("\nConverting to .raw file: " + inputfileName);
	
	reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputfileName)));
	PrintWriter writer = new PrintWriter (new FileOutputStream(outputfileName));
	
	String line;
	JSONParser parser=new JSONParser();
	int count = 0;
	while ((line = reader.readLine()) != null) {
		JSONObject obj = (JSONObject) parser.parse(line);
		// Parse review words.
		String review = (String) obj.get("text");
		// Output to the .raw file.
		writer.println("<DOC>");
		writer.println(review);
		writer.println("</DOC>");
		if (count++ % 10000 == 0)
			System.out.print(".");
	}
	
	System.out.println("\nGenerated .raw file" + outputfileName);
	reader.close();
	writer.close();
}