HttpsURLConnection conn; try { URL url = new URL(UUID_URL); conn = (HttpsURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); os.writeBytes(JSONValue.toJSONString(playerList)); json = (JSONArray) JSONValue.parse(new InputStreamReader(conn.getInputStream())); } catch (IOException e) { GlowServer.logger.warning("Couldn't get UUID due to IO error: " + e); if (!json.isEmpty()) { String uuid = (String) ((JSONObject) json.get(0)).get("id"); return UuidUtils.fromFlatString(uuid);
public static Map<String, Object> fromCompressedJsonConf(byte[] serialized) { try { ByteArrayInputStream bis = new ByteArrayInputStream(serialized); InputStreamReader in = new InputStreamReader(new GZIPInputStream(bis)); Object ret = JSONValue.parseWithException(in); in.close(); return (Map<String, Object>) ret; } catch (IOException | ParseException ioe) { throw new RuntimeException(ioe); } }
public static byte[] toCompressedJsonConf(Map<String, Object> stormConf) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); OutputStreamWriter out = new OutputStreamWriter(new GZIPOutputStream(bos)); JSONValue.writeJSONString(stormConf, out); out.close(); return bos.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
public Void call() throws Exception { URL url = createURL(RestConstants.ADMIN_METRICS_RESOURCE, Collections.EMPTY_MAP); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode()); assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE)); JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream())); assertTrue(json.containsKey(JsonTags.INSTR_COUNTERS)); return null; } });
protected String call(HttpURLConnection conn) throws IOException, OozieClientException { if ((conn.getResponseCode() == HttpURLConnection.HTTP_OK)) { Reader reader = new InputStreamReader(conn.getInputStream(), Charsets.UTF_8); JSONObject json = (JSONObject) JSONValue.parse(reader); return (String) json.get(JsonTags.JMS_TOPIC_NAME); } else { handleError(conn); } return null; } }
private JSONArray getArray(String urlString){ try{ URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.addRequestProperty("User-Agent", USER_AGENT); // Set User-Agent InputStream inputStream = connection.getInputStream(); InputStreamReader reader = new InputStreamReader(inputStream); return (JSONArray) JSONValue.parseWithException(reader); } catch(Exception e){ e.printStackTrace(); return null; } } }
public Void call() throws Exception { URL url = createURL(RestConstants.ADMIN_TIME_ZONES_RESOURCE, Collections.EMPTY_MAP); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); assertEquals(HttpServletResponse.SC_OK, conn.getResponseCode()); assertTrue(conn.getHeaderField("content-type").startsWith(RestConstants.JSON_CONTENT_TYPE)); JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader(conn.getInputStream())); assertTrue(json.containsKey(JsonTags.AVAILABLE_TIME_ZONES)); JSONArray array = (JSONArray) json.get(JsonTags.AVAILABLE_TIME_ZONES); assertFalse(array.isEmpty()); return null; } });
/** * Deserialise a JSON value from the stream. * * @throws IllegalArgumentException if unable to parse a JSON value from * the stream */ @Override public Object deserialize(DataInputStream stream) throws IOException { final Reader reader = new InputStreamReader(stream, ENCODING); try { return JSONValue.parseWithException(reader); } catch (ParseException e) { throw new IllegalArgumentException(e); // TODO this is a bit impolite } } }
public static void main(String[] args) { // convert Java to json JSONObject root = new JSONObject(); root.put("message", "Hi"); JSONObject place = new JSONObject(); place.put("name", "World!"); root.put("place", place); String json = root.toJSONString(); System.out.println(json); System.out.println(); // convert json to Java JSONObject obj = (JSONObject) JSONValue.parse(json); String message = (String) obj.get("message"); place = (JSONObject) obj.get("place"); String name = (String) place.get("name"); System.out.println(message + " " + name); } }
Object parse = JSONValue.parseWithException(req.getReader()); if(parse instanceof JSONObject) { // 200, 400, 404, 409, 503 JSONObject jo = (JSONObject) parse; JSONArray statuses = new JSONArray(); JSONArray ja = (JSONArray) parse; if(!validateDocuments(ja)) { for(Object o: ja) { JSONObject jo = (JSONObject) o; JSONObject status = new JSONObject(); try { putDocument(api, jo); status.put("added", true); } catch(Exception e) { status.put("added", false); status.put("error", "Invalid or missing argument"); // TODO: descriptive error msg hasError = true; statuses.add(status); print(statuses.toJSONString()); return;
_inputs.put(genId, input); try { JSONObject obj = new JSONObject(); obj.put("id", genId); obj.put("comp", input.getSourceComponent()); obj.put("stream", input.getSourceStreamId()); obj.put("task", input.getSourceTask()); break; } else { Map action = (Map) JSONValue.parse(line); String command = (String) action.get("command"); if(command.equals("ack")) { sendToSubprocess(JSONValue.toJSONString(outtasks)); } else { _collector.emitDirect((int)task.longValue(), stream, anchors, tuple);
public static Object from_json(String json) { if (json == null) { return null; } else { // return JSON.parse(json); return JSONValue.parse(json); } }
private static String[] jsonObjValToStringArr(String inputString, String subObjPropertyName) throws Exception { JSONObject jsonObj = (JSONObject) JSONValue.parse(inputString); JSONArray jsonArr = (JSONArray) jsonObj.get(subObjPropertyName); return jsonArrToStringArr(jsonArr.toJSONString(), null); }
@Test public void testEnrich() throws Exception { Map<String, JSONObject> mapKnownHosts = new HashMap<>(); JSONArray jsonArray = (JSONArray) JSONValue.parse(expectedKnownHostsString); Iterator jsonArrayIterator = jsonArray.iterator(); while(jsonArrayIterator.hasNext()) { JSONObject jsonObject = (JSONObject) jsonArrayIterator.next(); String host = (String) jsonObject.remove("ip"); mapKnownHosts.put(host, jsonObject); } HostFromPropertiesFileAdapter hfa = new HostFromPropertiesFileAdapter(mapKnownHosts); JSONObject actualMessage = hfa.enrich(new CacheKey("dummy", ip, null)); Assert.assertNotNull(actualMessage); Assert.assertEquals(expectedMessage, actualMessage); actualMessage = hfa.enrich(new CacheKey("dummy", ip1, null)); JSONObject emptyJson = new JSONObject(); Assert.assertEquals(emptyJson, actualMessage); }
@Override public List<String> getSupportedCountries(MerchantStore store) throws ServiceException { List<String> supportedCountries = new ArrayList<String>(); MerchantConfiguration configuration = merchantConfigurationService.getMerchantConfiguration(SUPPORTED_COUNTRIES, store); if(configuration!=null) { String countries = configuration.getValue(); if(!StringUtils.isBlank(countries)) { Object objRegions=JSONValue.parse(countries); JSONArray arrayRegions=(JSONArray)objRegions; @SuppressWarnings("rawtypes") Iterator i = arrayRegions.iterator(); while(i.hasNext()) { supportedCountries.add((String)i.next()); } } } return supportedCountries; }
public HostFromJSONListAdapter(String jsonList) { JSONArray jsonArray = (JSONArray) JSONValue.parse(jsonList); Iterator jsonArrayIterator = jsonArray.iterator(); while(jsonArrayIterator.hasNext()) { JSONObject jsonObject = (JSONObject) jsonArrayIterator.next(); String host = (String) jsonObject.remove("ip"); _known_hosts.put(host, jsonObject); } }
public static String to_json(Object m) { // return JSON.toJSONString(m); return JSONValue.toJSONString(m); }
@SuppressWarnings("rawtypes") @Test public void testMetadataIsRoundTripSerializableWithJsonSimple() throws Exception { /** * Tests that the metadata object can be converted to and from a Map. This is needed because Trident metadata is written to * Zookeeper as JSON with the json-simple library, so the spout converts the metadata to Map before returning it to Trident. * It is important that all map entries are types json-simple knows about, * since otherwise the library just calls toString on them which will likely produce invalid JSON. */ long startOffset = 10; long endOffset = 20; String topologyId = "topologyId"; KafkaTridentSpoutBatchMetadata metadata = new KafkaTridentSpoutBatchMetadata(startOffset, endOffset, topologyId); Map<String, Object> map = metadata.toMap(); Map<String, Object> deserializedMap = (Map)JSONValue.parseWithException(JSONValue.toJSONString(map)); KafkaTridentSpoutBatchMetadata deserializedMetadata = KafkaTridentSpoutBatchMetadata.fromMap(deserializedMap); assertThat(deserializedMetadata.getFirstOffset(), is(metadata.getFirstOffset())); assertThat(deserializedMetadata.getLastOffset(), is(metadata.getLastOffset())); assertThat(deserializedMetadata.getTopologyId(), is(metadata.getTopologyId())); }