List of usage examples for org.json.simple JSONValue parseWithException
public static Object parseWithException(String s) throws ParseException
From source file:org.apache.storm.utils.Utils.java
public static Map<String, Object> parseJson(String json) { if (json == null) { return new HashMap<>(); } else {// w w w.jav a 2 s . c o m try { return (Map<String, Object>) JSONValue.parseWithException(json); } catch (ParseException e) { throw new RuntimeException(e); } } }
From source file:org.arkhamnetwork.playersync.utils.SerializationUtils.java
public static ItemStack[] deserializeItemStacks(byte[] b) throws SerializationException, ParseException, IOException, ClassNotFoundException { if (b == null) { return null; }/* w w w .jav a 2 s . c o m*/ NBTTagCompound baseTag = toTag(JSONValue.parseWithException(new String(b)).toString()); NBTTagList inventoryTag = baseTag.getList("Inventory", 10); Map<Integer, ItemStack> itemMap = new ConcurrentHashMap<>(); final int invSize = inventoryTag.get(0).getByte("InvSize"); for (int i = 0; i < inventoryTag.size(); i++) { NBTTagCompound item = inventoryTag.get(i); int slot = item.getByte("CurrentSlot"); if (!itemMap.containsKey(slot)) { itemMap.put(slot, CraftItemStack.asCraftMirror(net.minecraft.server.v1_7_R4.ItemStack.createStack(item))); } } List<ItemStack> items = new ArrayList<>(); for (int i = 0; i < invSize; i++) { if (i > invSize) { break; } if (itemMap.get(i) == null) { items.add(AIR.clone()); continue; } items.add(itemMap.get(i)); //IMPORTANT itemMap.remove(i); } return items.toArray(new ItemStack[items.size()]); }
From source file:org.arkhamnetwork.playersync.utils.SerializationUtils.java
public static PotionEffect[] deserializePotionEffects(byte[] b) throws SerializationException, ParseException { if (b == null) { return null; }/*www . j a v a2 s . c o m*/ Object o = JSONValue.parseWithException(new String(b)); try { if (o instanceof List) { final List<?> data = (List) o; ArrayList<PotionEffect> items = new ArrayList<>(data.size()); for (Object t : data) { if (t instanceof Map) { final Map<?, ?> mdata = (Map) t; final Map<String, Object> conv = new HashMap<>(mdata.size()); for (Map.Entry<?, ?> e : mdata.entrySet()) { conv.put(String.valueOf(e.getKey()), convert(e.getValue())); } items.add(new PotionEffect(conv)); } else { throw new IllegalArgumentException("Not a Map"); } } return items.toArray(new PotionEffect[items.size()]); } throw new IllegalArgumentException("Not a List"); } catch (IllegalArgumentException ex) { ex.printStackTrace(); } return null; }
From source file:org.exoplatform.addon.pulse.service.ws.RestActivitiesStatistic.java
private long getTotalPlfDownloadDataFromJson(String json, Date startDate) { Date today = new Date(); Calendar cal = Calendar.getInstance(); cal.clear();//from ww w . j a va2s. c om cal.setTime(today); cal.add(Calendar.DATE, 1); Date nexDate = cal.getTime(); cal.clear(); if (startDate.after(nexDate)) return 0L; // Do not get data of future try { org.json.simple.JSONObject jsonObj = (org.json.simple.JSONObject) JSONValue.parseWithException(json); //in case request to get data in future, return 0 String startDateStr = partString(startDate, "yyyy-MM-dd"); String returnStartDateStr = jsonObj.get("start_date").toString(); if (returnStartDateStr.substring(0, 10).equalsIgnoreCase(startDateStr) == false) { return 0L; } Long total = Long.parseLong(jsonObj.get("total") + ""); return total; } catch (ParseException e) { // TODO Auto-generated catch block LOG.error(e.getMessage()); } catch (Exception e) { LOG.error(e.getMessage()); } return 0l; }
From source file:org.exoplatform.addons.es.client.ElasticContentRequestBuilderTest.java
@Test public void getCreateIndexRequestContent_ifSimpleConnector_shouldReturnIndexJSONSettings() throws org.json.simple.parser.ParseException { // Given/*from ww w .j a v a 2 s. c om*/ // When String request = elasticContentRequestBuilder.getCreateIndexRequestContent(elasticIndexingServiceConnector); // Then JSONObject parsedRequest = (JSONObject) JSONValue.parseWithException(request); JSONObject settings = (JSONObject) parsedRequest.get("settings"); assertThat((String) settings.get("number_of_replicas"), is("2")); assertThat((String) settings.get("number_of_shards"), is("3")); assertNotNull(settings.get("analysis")); assertNotNull(((JSONObject) settings.get("analysis")).get("analyzer")); assertNotNull(((JSONObject) ((JSONObject) settings.get("analysis")).get("analyzer")).get("default")); }
From source file:org.exoplatform.addons.es.client.ElasticContentRequestBuilderTest.java
@Test public void getDeleteDocumentRequestContent_ifSimpleConnectorAndEntityId_shouldReturnDeleteQuery() throws org.json.simple.parser.ParseException { // Given/*from w ww .ja v a 2 s . c o m*/ // When String request = elasticContentRequestBuilder .getDeleteDocumentRequestContent(elasticIndexingServiceConnector, "1"); // Then JSONObject parsedRequest = (JSONObject) JSONValue.parseWithException(request); JSONObject delete = (JSONObject) parsedRequest.get("delete"); assertThat((String) delete.get("_type"), is("type1")); assertThat((String) delete.get("_id"), is("1")); assertThat((String) delete.get("_index"), is("test")); }
From source file:org.exoplatform.addons.es.client.ElasticContentRequestBuilderTest.java
@Test public void getCreateDocumentRequestContent_ifSimpleConnectorAndEntityId_shouldReturnCreateQuery() throws ParseException, org.json.simple.parser.ParseException { // Given// ww w . jav a 2 s. co m initDocumentMock(); // When String request = elasticContentRequestBuilder .getCreateDocumentRequestContent(elasticIndexingServiceConnector, "1"); // Then String[] lines = request.split("\n"); JSONObject parsedRequestLine1 = (JSONObject) JSONValue.parseWithException(lines[0]); JSONObject create = (JSONObject) parsedRequestLine1.get("create"); assertThat((String) create.get("_type"), is("type1")); assertThat((String) create.get("_id"), is("1")); assertThat((String) create.get("_index"), is("test")); JSONObject parsedRequestLine2 = (JSONObject) JSONValue.parseWithException(lines[1]); assertThat((String) parsedRequestLine2.get("author"), is("Michael Jordan")); assertThat((String) parsedRequestLine2.get("quote"), is("I've missed more than 9000 shots in my career. I've lost almost 300 games. " + "26 times, I've been trusted to take the game winning shot and missed. " + "I've failed over and over and over again in my life. And that is why I succeed.")); JSONArray permissions = (JSONArray) parsedRequestLine2.get("permissions"); assertTrue(permissions.contains("vizir")); assertTrue(permissions.contains("goleador")); assertThat((Long) parsedRequestLine2.get("lastUpdatedDate"), is(601171200000L)); assertThat((String) parsedRequestLine2.get("url"), is("MyUrlBaby")); }
From source file:org.exoplatform.addons.es.client.ElasticContentRequestBuilderTest.java
@Test public void getUpdateDocumentRequestContent_ifSimpleConnectorAndEntityId_shouldReturnUpdateQuery() throws ParseException, org.json.simple.parser.ParseException { // Given//www .jav a 2 s . co m initDocumentMock(); // When String request = elasticContentRequestBuilder .getUpdateDocumentRequestContent(elasticIndexingServiceConnector, "1"); // Then String[] lines = request.split("\n"); JSONObject parsedRequestLine1 = (JSONObject) JSONValue.parseWithException(lines[0]); JSONObject create = (JSONObject) parsedRequestLine1.get("index"); assertThat((String) create.get("_type"), is("type1")); assertThat((String) create.get("_id"), is("1")); assertThat((String) create.get("_index"), is("test")); JSONObject parsedRequestLine2 = (JSONObject) JSONValue.parseWithException(lines[1]); assertThat((String) parsedRequestLine2.get("author"), is("Michael Jordan")); assertThat((String) parsedRequestLine2.get("quote"), is("I've missed more than 9000 shots in my career. I've lost almost 300 games. " + "26 times, I've been trusted to take the game winning shot and missed. " + "I've failed over and over and over again in my life. And that is why I succeed.")); JSONArray permissions = (JSONArray) parsedRequestLine2.get("permissions"); assertTrue(permissions.contains("vizir")); assertTrue(permissions.contains("goleador")); assertThat((Long) parsedRequestLine2.get("lastUpdatedDate"), is(601171200000L)); assertThat((String) parsedRequestLine2.get("url"), is("MyUrlBaby")); }
From source file:org.exoplatform.addons.es.client.ElasticIndexingClient.java
private void logBulkResponse(String response, long executionTime) { try {//from w w w . j a va 2 s . co m Object parsedResponse = JSONValue.parseWithException(response); if (!(parsedResponse instanceof JSONObject)) { LOG.error("Unable to parse Bulk response: response is not a JSON. response={}", response); throw new ElasticClientException("Unable to parse Bulk response: response is not a JSON."); } // process items Object items = ((JSONObject) parsedResponse).get("items"); if (items != null) { if (!(items instanceof JSONArray)) { LOG.error("Unable to parse Bulk response: items is not a JSONArray. items={}", items); throw new ElasticClientException("Unable to parse Bulk response: items is not a JSONArray."); } // Looping over all the items is required because // in case of error, ES send a response with Status 200 and a flag // errors:true // in the JSON message for (Object item : ((JSONArray) items).toArray()) { if (!(item instanceof JSONObject)) { LOG.error("Unable to parse Bulk response: item is not a JSONObject. item={}", item); throw new ElasticClientException( "Unable to parse Bulk response: item is not a JSONObject."); } logBulkResponseItem((JSONObject) item, executionTime); } } } catch (ParseException e) { throw new ElasticClientException("Unable to parse Bulk response", e); } }
From source file:org.geotools.data.couchdb.CouchDBTestSupport.java
protected JSONArray loadJSON(String path, String featureClass) throws Exception { String content = resolveContent(path); JSONObject data = (JSONObject) JSONValue.parseWithException(content); JSONArray features = (JSONArray) data.get("features"); if (featureClass == null) { featureClass = path.split("\\.")[0]; }/*from www . j a va2 s. co m*/ for (int i = 0; i < features.size(); i++) { JSONObject f = (JSONObject) features.get(i); f.put("featureClass", featureClass); } return features; }