List of usage examples for org.json.simple.parser JSONParser JSONParser
JSONParser
From source file:com.demandware.vulnapp.challenge.impl.CookieChallenge.java
private boolean doesCookieValueGrantAccess(Cookie c) { boolean grant = false; String value = new String(Base64.decodeBase64(c.getValue())); try {/*from w ww .j av a 2s .c o m*/ JSONObject o = (JSONObject) new JSONParser().parse(value); grant = Helpers.isTruthy((String) o.get(ACCESS_KEY)); } catch (ParseException e) { grant = false; } return grant; }
From source file:de.elggconnect.elggconnectclient.webservice.StatusUser.java
/** * Run the StatusUser Web API Method/*from w w w. j a v a 2s .c om*/ */ @Override public Long execute() { //Build URL for API Method String urlParameters = APIMETHOD + "&auth.token=" + this.authToken; String url = userAuthentication.getBaseURL() + urlParameters; //Try to execute the API Method try { URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); //add user agent to the request header con.setRequestProperty("User-Agent", USER_AGENT); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); //Read the response JSON while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); JSONObject json = (JSONObject) new JSONParser().parse(response.toString()); this.status = (Long) json.get("status"); if (this.status != -1L) { //Handle the JSON Response handleStatusUserResponse((JSONArray) json.get("result")); } } catch (Exception e) { System.err.println(e.getMessage()); } return this.status; }
From source file:net.BiggerOnTheInside.Binder.engine.JSONManager.java
private static JSONObject parseString(String s) throws ParseException { return (JSONObject) (new JSONParser().parse(s)); }
From source file:net.phyloviz.upgmanjcore.json.JsonSchemaValidator.java
/** * Validates choosen file with shema// w w w . j a v a 2 s. c o m * * @param schema schema with validation * @param directory file path to be load * @param filename file name to be load * @return */ public boolean validate(InputStream schema, String directory, String filename) { //parse validator try { BufferedReader br = new BufferedReader(new InputStreamReader(schema)); JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(br); //get order JSONArray orderObj = (JSONArray) json.get("order"); createOrder(orderObj); //get element properties JSONObject propertiesObj = (JSONObject) json.get("properties"); Iterator<String> itKeys = dataIds.keySet().iterator(); while (itKeys.hasNext()) { String key = itKeys.next(); JsonProp jp = createProps(propertiesObj, key, dataIds.get(key)); validatorMap.put(key, jp); } //check if has all orders for (String orderList1 : orderList) { if (!validatorMap.containsKey(orderList1)) { JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), "Invalid Schema!!!!"); return false; } } } catch (IOException | ParseException e) { Exceptions.printStackTrace(e); } //validate file try { FileReader reader = new FileReader(new File(directory, filename)); JSONParser parser = new JSONParser(); JSONObject json = (JSONObject) parser.parse(reader); for (String key : orderList) { JsonProp jp = validatorMap.get(key); if (jp.type.equalsIgnoreCase("array")) { JSONArray ja = (JSONArray) json.get(key); if (ja != null) { if (!validateJsonArray(ja, validatorMap.get(key), filename)) { return false; } } } else if (jp.type.equalsIgnoreCase("object")) { JSONObject jo = (JSONObject) json.get(key); if (jo != null) { if (!validateJsonObject(jo, validatorMap.get(key), filename)) { return false; } } } else { JOptionPane.showMessageDialog(WindowManager.getDefault().getMainWindow(), "Invalid file " + filename); return false; } } } catch (IOException | ParseException e) { Exceptions.printStackTrace(e); } return true; }
From source file:com.telefonica.euro_iaas.paasmanager.util.impl.OpenStackRegionImplTest.java
@Before public void setUp() throws IOException, ParseException { JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader(this.getClass().getResource("/service_catalog.json").getPath())); serviceCatalogJSON = JSONObject.fromObject(obj.toString()); systemPropertiesProvider = mock(SystemPropertiesProvider.class); when(systemPropertiesProvider.getProperty(SystemPropertiesProvider.KEYSTONE_URL)) .thenReturn("http://domain.com/v3/"); RegionCache regionCache = new RegionCache(); regionCache.clear();/* w ww .ja v a 2s. c om*/ }
From source file:com.apigee.edge.config.utils.ConsolidatedConfigReader.java
/** * Example Hierarchy/*from www. jav a2 s . c om*/ * orgConfig.apiProducts * * Returns List of * [ {apiProduct1}, {apiProduct2}, {apiProduct3} ] */ public static List getOrgConfig(File configFile, String scope, String resource) throws ParseException, IOException { Logger logger = LoggerFactory.getLogger(ConfigReader.class); JSONParser parser = new JSONParser(); ArrayList out = null; try { BufferedReader bufferedReader = new BufferedReader(new java.io.FileReader(configFile)); JSONObject edgeConf = (JSONObject) parser.parse(bufferedReader); if (edgeConf == null) return null; JSONObject scopeConf = (JSONObject) edgeConf.get(scope); if (scopeConf == null) return null; JSONArray configs = (JSONArray) scopeConf.get(resource); if (configs == null) return null; out = new ArrayList(); for (Object config : configs) { out.add(((JSONObject) config).toJSONString()); } } catch (IOException ie) { logger.info(ie.getMessage()); throw ie; } catch (ParseException pe) { logger.info(pe.getMessage()); throw pe; } return out; }
From source file:com.conwet.xjsp.session.Negotiator.java
public Negotiator(Map<String, FeatureHandler> handlers) { this.handlers = handlers; this.prefixes = new DefaultFeatureMap(); this.parser = new JSONParser(); this.phase = NegotiationPhase.start; }
From source file:it.polimi.logging.LogMessageUtils.java
public static Type getEntityType(String message) { JSONParser parser = new JSONParser(); JSONObject jsonMsg;//from ww w. j a va2s.co m String type = ""; try { jsonMsg = (JSONObject) parser.parse(message); type = (String) jsonMsg.get(JsonStrings.ENTITY_TYPE); return Type.valueOf(type); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:me.timothy.ddd.quests.QuestManager.java
@SuppressWarnings("unchecked") public QuestManager(Player player, AchievementManager aManager, EntityManager entManager) { logger = LogManager.getLogger(); entityManager = entManager;/*from w w w . ja v a 2 s . c o m*/ achievementManager = aManager; acceptedQuests = new ArrayList<>(); completedQuests = new ArrayList<>(); this.player = player; File questsFile = new File("quests.json"); if (questsFile.exists()) { try (FileReader fr = new FileReader(new File("quests.json"))) { JSONObject jObj = (JSONObject) (new JSONParser().parse(fr)); JSONArray questsArr = (JSONArray) jObj.get("current"); for (int i = 0; i < questsArr.size(); i++) { JSONObject questObj = (JSONObject) questsArr.get(i); String classStr = (String) questObj.get("class"); Class<?> cl = Class.forName(classStr); Quest quest = null; try { quest = (Quest) cl.getMethod("fromObject", getClass(), JSONObject.class).invoke(null, this, questObj); } catch (NoSuchMethodException nsme) { quest = (Quest) cl.getConstructor(QuestManager.class).newInstance(this); } acceptedQuests.add(quest); } JSONArray complete = (JSONArray) jObj.get("complete"); for (int i = 0; i < complete.size(); i++) { // completedQuests.add((Class<? extends Quest>) Class.forName((String) complete.get(i))); } } catch (Exception e) { logger.catching(e); } } Runnable saveQuests = new Runnable() { @Override public void run() { if (!DrunkDuckDispatch.ddd.save) return; JSONObject jObj = new JSONObject(); JSONArray questsArr = new JSONArray(); for (Quest qu : acceptedQuests) { questsArr.add(qu.asObject()); } jObj.put("current", questsArr); JSONArray completed = new JSONArray(); for (Class<? extends Quest> cl : completedQuests) { completed.add(cl.getCanonicalName()); } jObj.put("complete", completed); try (FileWriter fw = new FileWriter(new File("quests.json"))) { jObj.writeJSONString(fw); } catch (IOException ie) { } } }; Runtime.getRuntime().addShutdownHook(new Thread(saveQuests)); }
From source file:com.mstiles92.plugins.stileslib.player.UUIDFetcher.java
public Map<String, UUID> execute() { Map<String, UUID> results = new HashMap<>(); try {//from ww w .j a v a 2 s. c o m BasicHttpClient client = new BasicHttpClient(new URL("https://api.mojang.com/profiles/minecraft")); client.addHeader("Content-Type", "application/json"); int requests = (int) Math.ceil((double) usernames.size() / PROFILES_PER_REQUEST); for (int i = 0; i < requests; i++) { int start = i * PROFILES_PER_REQUEST; int end = Math.min((i + 1) * PROFILES_PER_REQUEST, usernames.size()); client.setBody(JSONArray.toJSONString(usernames.subList(start, end))); String response = client.post(); JSONArray responseJson = (JSONArray) new JSONParser().parse(response); for (Object o : responseJson) { JSONObject profile = (JSONObject) o; String id = (String) profile.get("id"); String name = (String) profile.get("name"); results.put(name, getUUID(id)); } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return results; }