public static JsonNode stringToJsonNode(String jsonString) throws IOException { return DEFAULT_READER.readTree(jsonString); }
public static JsonNode inputStreamToJsonNode(InputStream jsonInputStream) throws IOException { return DEFAULT_READER.readTree(jsonInputStream); }
private boolean isAccumuloView(byte[] data) throws IOException { // AccumuloView contains a 'data' node return mapper.reader().readTree(new String(data)).has("data"); }
private boolean isAccumuloTable(byte[] data) throws IOException { // AccumuloTable does not contain a 'data' node return !mapper.reader().readTree(new String(data)).has("data"); }
/** * For use in tests * * @param json incoming JSON to parse * @return a node tree * @throws IOException on any parsing problems */ public static ContainerNode parse(String json) throws IOException { JsonNode jsonNode = READER.readTree(json); if (!(jsonNode instanceof ContainerNode)) { throw new IOException("Wrong JSON data: " + json); } return (ContainerNode) jsonNode; } }
@Override public void fillResponseFromJson(MatrixResponse matrixResponse, String responseAsString) throws IOException { fillResponseFromJson(matrixResponse, objectMapper.reader().readTree(responseAsString)); }
public void fillResponseFromJson(MatrixResponse matrixResponse, String responseAsString) throws IOException { fillResponseFromJson(matrixResponse, objectMapper.reader().readTree(responseAsString)); }
public List<String> getLiveBrokers() { List<String> brokerUrls = new ArrayList<>(); try { byte[] brokerResourceNodeData = zkClient.readData(BROKER_EXTERNAL_VIEW_PATH, true); brokerResourceNodeData = unpackZnodeIfNecessary(brokerResourceNodeData); JsonNode jsonObject = OBJECT_READER.readTree(new ByteArrayInputStream(brokerResourceNodeData)); JsonNode brokerResourceNode = jsonObject.get("mapFields"); Iterator<Entry<String, JsonNode>> resourceEntries = brokerResourceNode.fields(); while (resourceEntries.hasNext()) { JsonNode resource = resourceEntries.next().getValue(); Iterator<Entry<String, JsonNode>> brokerEntries = resource.fields(); while (brokerEntries.hasNext()) { Entry<String, JsonNode> brokerEntry = brokerEntries.next(); String brokerName = brokerEntry.getKey(); if (brokerName.startsWith("Broker_") && "ONLINE".equals(brokerEntry.getValue().asText())) { // Turn Broker_12.34.56.78_1234 into 12.34.56.78:1234 String brokerHostPort = brokerName.replace("Broker_", "").replace("_", ":"); brokerUrls.add(brokerHostPort); } } } } catch (Exception e) { LOGGER.warn("Exception while reading External view from zookeeper", e); // ignore } return brokerUrls; }
byte[] brokerResourceNodeData = zkClient.readData("/EXTERNALVIEW/brokerResource", true); brokerResourceNodeData = unpackZnodeIfNecessary(brokerResourceNodeData); JsonNode jsonObject = OBJECT_READER.readTree(new ByteArrayInputStream(brokerResourceNodeData)); JsonNode brokerResourceNode = jsonObject.get("mapFields");
@Override public TrueFxTicker deserialize(JsonParser parser, DeserializationContext context) throws IOException, JsonProcessingException { ArrayNode array = mapper.readerFor(TrueFxTicker.class).with(schema).readTree(parser); String pair = array.get(0).asText(); long timestamp = array.get(1).asLong(); BigDecimal bid = new BigDecimal(array.get(2).asText()); BigDecimal bidBP = new BigDecimal(array.get(3).asText()); BigDecimal ask = new BigDecimal(array.get(4).asText()); BigDecimal askBP = new BigDecimal(array.get(5).asText()); BigDecimal low = new BigDecimal(array.get(6).asText()); BigDecimal high = new BigDecimal(array.get(7).asText()); BigDecimal open = new BigDecimal(array.get(8).asText()); return new TrueFxTicker(pair, timestamp, bid, bidBP, ask, askBP, low, high, open); } }
@Override public BrokerResponse get(long timeout, TimeUnit unit) throws ExecutionException { try { LOGGER.debug("Sending query {} to {}", _query, _url); Response httpResponse = _response.get(timeout, unit); LOGGER.debug("Completed query, HTTP status is {}", httpResponse.getStatusCode()); if (httpResponse.getStatusCode() != 200) { throw new PinotClientException( "Pinot returned HTTP status " + httpResponse.getStatusCode() + ", expected 200"); } String responseBody = httpResponse.getResponseBody(); return BrokerResponse.fromJson(OBJECT_READER.readTree(responseBody)); } catch (Exception e) { throw new ExecutionException(e); } } }
JsonNode getResponseJson = objectMapper.reader().readTree(str); fillResponseFromJson(matrixResponse, getResponseJson);
@Override public GHResponse route(GHRequest request) { ResponseBody rspBody = null; try { Request okRequest = createRequest(request); rspBody = getClientForRequest(request).newCall(okRequest).execute().body(); JsonNode json = objectMapper.reader().readTree(rspBody.byteStream()); GHResponse res = new GHResponse(); res.addErrors(readErrors(json)); if (res.hasErrors()) return res; JsonNode paths = json.get("paths"); boolean tmpElevation = request.getHints().getBool("elevation", elevation); boolean tmpTurnDescription = request.getHints().getBool("turn_description", true); for (JsonNode path : paths) { PathWrapper altRsp = createPathWrapper(path, tmpElevation, tmpTurnDescription); res.add(altRsp); } return res; } catch (Exception ex) { throw new RuntimeException("Problem while fetching path " + request.getPoints() + ": " + ex.getMessage(), ex); } finally { Helper.close(rspBody); } }
JsonNode getResponseJson = objectMapper.reader().readTree(str);
/** * Verify that the Helios master generates and returns a hash if the submitted job creation * request does not include one. */ @Test public void testHashLessJobCreation() throws Exception { startDefaultMaster(); final Job job = Job.newBuilder() .setName(testJobName) .setVersion(testJobVersion) .setImage(BUSYBOX) .setCommand(IDLE_COMMAND) .setCreatingUser(TEST_USER) .build(); // Remove the hash from the id in the json serialized job final ObjectNode json = (ObjectNode) Json.reader().readTree(Json.asString(job)); json.set("id", TextNode.valueOf(testJobName + ":" + testJobVersion)); final HttpURLConnection req = post("/jobs?user=" + TEST_USER, Json.asBytes(json)); assertEquals(req.getResponseCode(), 200); final CreateJobResponse res = Json.read(toByteArray(req.getInputStream()), CreateJobResponse.class); assertEquals(OK, res.getStatus()); assertTrue(res.getErrors().isEmpty()); assertEquals(job.getId().toString(), res.getId()); }
@Test public void testLoadInvalidDataSource() throws URISyntaxException, IOException { URI baseURI = TextEntitySourceTest.class.getResource("ratings.csv").toURI(); JsonNode node = reader.readTree("\"foobar\""); try { StaticDataSource daoProvider = StaticDataSource.fromJSON(node, baseURI); fail("JSON parsing succeeded, should have failed on string"); } catch (IllegalArgumentException e) { /* expected */ } }
@Test public void testLoadRatingsMap() throws IOException, URISyntaxException, ClassNotFoundException, IllegalAccessException { URI baseURI = TextEntitySourceTest.class.getResource("ratings.csv").toURI(); JsonNode node = reader.readTree("{\"ratings\":{\"file\": \"ratings.csv\", \"format\": \"csv\"}}"); StaticDataSource daoProvider = StaticDataSource.fromJSON(node, baseURI); DataAccessObject dao = daoProvider.get(); verifyRatingsCsvData(dao); Field f = FieldUtils.getField(EntityCollectionDAO.class, "storage", true); Class<?> cls = Class.forName("org.lenskit.data.store.PackedEntityCollection"); Map<EntityType,EntityCollection> storage = (Map<EntityType, EntityCollection>) f.get(dao); assertThat(storage.get(CommonTypes.RATING), instanceOf(cls)); }
@Test public void testLoadRatingsList() throws IOException, URISyntaxException, ClassNotFoundException, IllegalAccessException { URI baseURI = TextEntitySourceTest.class.getResource("ratings.csv").toURI(); JsonNode node = reader.readTree("[{\"file\": \"ratings.csv\", \"format\": \"csv\"}]"); StaticDataSource daoProvider = StaticDataSource.fromJSON(node, baseURI); DataAccessObject dao = daoProvider.get(); verifyRatingsCsvData(dao); Field f = FieldUtils.getField(EntityCollectionDAO.class, "storage", true); Class<?> cls = Class.forName("org.lenskit.data.store.PackedEntityCollection"); Map<EntityType,EntityCollection> storage = (Map<EntityType, EntityCollection>) f.get(dao); assertThat(storage.get(CommonTypes.RATING), instanceOf(cls)); }
@Test public void testLoadRatings() throws IOException, URISyntaxException { URI baseURI = TextEntitySourceTest.class.getResource("ratings.csv").toURI(); JsonNode node = reader.readTree("{\"file\": \"ratings.csv\", \"format\": \"csv\"}"); StaticDataSource daoProvider = StaticDataSource.fromJSON(node, baseURI); // we should have one text source for ratings assertThat(daoProvider.getSourcesForType(CommonTypes.RATING), contains(instanceOf(TextEntitySource.class))); DataAccessObject dao = daoProvider.get(); verifyRatingsCsvData(dao); }
@Test public void testConfigureReader() throws IOException { ObjectReader reader = new ObjectMapper().reader(); JsonNode json = reader.readTree("{\"entity_type\": \"item\"}"); EntityFormat fmt = JSONEntityFormat.fromJSON(null, ClassLoaders.inferDefault(), json); assertThat(fmt.getEntityType(), equalTo(CommonTypes.ITEM)); LineEntityParser lep = fmt.makeParser(Collections.EMPTY_LIST); Entity res = lep.parse("{\"id\": 204, \"name\": \"hamster\", \"extra\": \"wumpus\"}"); assertThat(res, notNullValue()); assertThat(res.getId(), equalTo(204L)); assertThat(res.get(CommonAttributes.NAME), equalTo("hamster")); assertThat(res.get("extra"), (Matcher) equalTo("wumpus")); }