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); } }
@Override public void submitTopologyWithOpts(String name, String uploadedJarLocation, String jsonConf, StormTopology topology, SubmitOptions options) throws AlreadyAliveException, InvalidTopologyException, AuthorizationException, TException { try { @SuppressWarnings("unchecked") Map<String, Object> conf = (Map<String, Object>) JSONValue.parseWithException(jsonConf); submitTopologyWithOpts(name, conf, topology, options); } catch (ParseException e) { throw new RuntimeException(e); } }
@Override public void submitTopology(String name, String uploadedJarLocation, String jsonConf, StormTopology topology) throws AlreadyAliveException, InvalidTopologyException, AuthorizationException, TException { try { @SuppressWarnings("unchecked") Map<String, Object> conf = (Map<String, Object>) JSONValue.parseWithException(jsonConf); submitTopology(name, conf, topology); } catch (ParseException e) { throw new RuntimeException(e); } }
Map<Object, Object> readState (String stream, String shardId) { try { String path = getZkPath(stream, shardId); Map<Object, Object> state = null; byte[] b = null; if (curatorFramework.checkExists().forPath(path) != null) { b = curatorFramework.getData().forPath(path); } if (b != null) { state = (Map<Object, Object>) JSONValue.parseWithException(new String(b, "UTF-8")); } return state; } catch (Exception e) { throw new RuntimeException(e); } }
private static void addLagResultForKafkaSpout(Map<String, Map<String, Object>> finalResult, String spoutId, SpoutSpec spoutSpec) throws IOException { ComponentCommon componentCommon = spoutSpec.get_common(); String json = componentCommon.get_json_conf(); if (json != null && !json.isEmpty()) { Map<String, Object> jsonMap = null; try { jsonMap = (Map<String, Object>) JSONValue.parseWithException(json); } catch (ParseException e) { throw new IOException(e); } if (jsonMap.containsKey(TOPICS_CONFIG) && jsonMap.containsKey(GROUPID_CONFIG) && jsonMap.containsKey(BOOTSTRAP_CONFIG)) { finalResult.put(spoutId, getLagResultForNewKafkaSpout(spoutId, spoutSpec)); } } }
Map<String, Object> jsonMap = null; try { jsonMap = (Map<String, Object>) JSONValue.parseWithException(json); } catch (ParseException e) { throw new IOException(e); result = (Map<String, Object>) JSONValue.parseWithException(resultFromMonitor); } catch (ParseException e) { logger.debug("JSON parsing failed, assuming message as error message: {}", resultFromMonitor);
String json = message.toString(); try { obj = JSONValue.parseWithException(json); } catch (ParseException e) { sender.sendMessage(ChatColor.RED + "Failed to parse JSON: " + e.getMessage());
@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())); }
Object parse = JSONValue.parseWithException(req.getReader()); if(parse instanceof JSONObject) { // 200, 400, 404, 409, 503 JSONObject jo = (JSONObject) parse;
IndexEngineApi api = (IndexEngineApi) ctx().getAttribute("api"); try { Object parse = JSONValue.parseWithException(req().getReader()); if(parse instanceof JSONObject) { // 200, 400, 404, 409, 503 JSONObject jo = (JSONObject) parse;
/** * Parses the given {@code text} and returns the corresponding JSON object. * * @param text text to parse. * @return JSON object that corresponds to the given text. * @throws IllegalArgumentException when the given text is not a valid * representation of a JSON object. */ private static JSONObject toJSONObject(String text) throws IllegalArgumentException { try { JSONObject json = (JSONObject)JSONValue.parseWithException(text); return json; } catch (ParseException ex) { throw new IllegalArgumentException(text); } }
/** * 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 } } }
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 Map<Object, Object> readJSON(String path) { try { byte[] b = readBytes(path); if (b == null) { return null; } return (Map<Object, Object>) JSONValue.parseWithException(new String(b, "UTF-8")); } catch (Exception e) { throw new RuntimeException(e); } }
public Map<String, Object> decode(final String message) { Map<String, Object> event = null; try { event = (HashMap) JSONValue.parseWithException(message); } catch (Exception e) { log.debug("failed to json parse message to event", e); } finally { if (event == null) { event = createDefaultEvent(message); } return event; } } }
/** * [zk: localhost:2181(CONNECTED) 56] get /brokers/ids/0 * { "host":"localhost", "jmx_port":9999, "port":9092, "version":1 } * * @param contents * @return */ private Broker getBrokerHost(byte[] contents) { try { Map<Object, Object> value = (Map<Object, Object>) JSONValue.parseWithException(new String(contents, "UTF-8")); String host = (String) value.get("host"); Integer port = ((Long) value.get("port")).intValue(); return new Broker(host, port); } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
/** * get /brokers/topics/distributedTopic/partitions/1/state * { "controller_epoch":4, "isr":[ 1, 0 ], "leader":1, "leader_epoch":1, "version":1 } * @param topic * @param partition * @return */ private int getLeaderFor(String topic, long partition) { try { String topicBrokersPath = partitionPath(topic); byte[] hostPortData = _curator.getData().forPath(topicBrokersPath + "/" + partition + "/state"); Map<Object, Object> value = (Map<Object, Object>) JSONValue.parseWithException(new String(hostPortData, "UTF-8")); Integer leader = ((Number) value.get("leader")).intValue(); if (leader == -1) { throw new RuntimeException("No leader found for partition " + partition); } return leader; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void processPacket(Packet packet) throws SmackException.NotConnectedException { Message incomingMessage = (Message) packet; GcmPacketExtension gcmPacket = (GcmPacketExtension) incomingMessage.getExtension(GCM_NAMESPACE); String json = gcmPacket.getJson(); try { Map<String, Object> jsonObject = (Map<String, Object>) JSONValue.parseWithException(json); Object messageType = jsonObject.get("message_type"); if (messageType == null) { handleUpstreamMessage(jsonObject); return; } switch (messageType.toString()) { case "ack" : handleAckReceipt(jsonObject); break; case "nack" : handleNackReceipt(jsonObject); break; case "receipt" : handleDeliveryReceipt(jsonObject); break; case "control" : handleControlMessage(jsonObject); break; default: logger.warn("Received unknown GCM message: " + messageType.toString()); } } catch (ParseException e) { logger.warn("GCMClient", "Received unparsable message", e); } catch (Exception e) { logger.warn("GCMClient", "Failed to process packet", e); } }
/** * Parse JSON response. * <p/> * @param in {@link InputStream} to read. * @return Response returned by REST administration service. */ @Override public RestActionReport parse(InputStream in) { RestActionReport report = new RestActionReport(); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { copy(in, out); String respMsg = out.toString("UTF-8"); JSONObject json = (JSONObject)JSONValue.parseWithException(respMsg); parseReport(report, json); } catch (IOException ex) { throw new GlassFishIdeException("Unable to copy JSON response.", ex); } catch (ParseException e) { throw new GlassFishIdeException("Unable to parse JSON response.", e); } return report; }
@Test public void printer() throws Exception{ NodeBuilder builder = store.getRoot().builder(); builder.child("a").setProperty("foo", "bar"); builder.child("a").setProperty(":foo", "bar"); builder.child("a").setProperty(":childOrder", "bar"); builder.child("b").child("c").setProperty("foo", "bar"); builder.child("b").child("c").setProperty(":foo", "bar"); builder.child("b").child(":d").setProperty("foo", "bar"); store.merge(builder, EmptyHook.INSTANCE, CommitInfo.EMPTY); when(pathService.getIndexPaths()).thenReturn(Lists.newArrayList("/a", "/b")); String json = getJSON(); //If there is any error in rendered json //exception would fail the test JSONObject o = (JSONObject) JSONValue.parseWithException(json); assertNull(o.get(":d")); JSONObject a = (JSONObject) o.get("/a"); assertNotNull(a.get("foo")); //Hidden props other than :childOrder should be present assertNotNull(a.get(":foo")); assertNull(a.get(":childOrder")); }