Java Code Examples for org.codehaus.jackson.JsonNode#findValue()

The following examples show how to use org.codehaus.jackson.JsonNode#findValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CurrencyDaoYahooFinanceOfflineSampleImpl.java    From website with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
protected CurrencyRates loadCurrencyRates() throws Exception {
    logger.info("loading currency rates");
    ClassPathResource classPathResource = new ClassPathResource("yahoo-sample-currency-rates.json");
    
    try (InputStream instream = classPathResource.getInputStream();) {
        CurrencyRates currencyRates = new CurrencyRates();
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonRootNode = objectMapper.readTree(instream);
        List<JsonNode> resourceJsonNodes = jsonRootNode.findValues("resource");
        // resourceJsonNodes.
        for (JsonNode resourceJsonNode : resourceJsonNodes) {
            JsonNode fieldsNode = resourceJsonNode.findValue("fields");
            String currencyName = fieldsNode.get("name").getValueAsText();
            currencyName = StringUtils.remove(currencyName, "USD/");
            BigDecimal currencyRate = new BigDecimal(fieldsNode.get("price").getValueAsText());
            currencyRates.getRates().put(currencyName, currencyRate);
        }
        currencyRates.getRates().put("USD", new BigDecimal(1));
        setLastLoadedCurrencyRates(currencyRates);
        return getLastLoadedCurrencyRates();
    }
}
 
Example 2
Source File: TestGeoJsonSerDe.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void TestIntWrite() throws Exception {
       ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "num");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "int");
	AbstractSerDe jserde = mkSerDe(proptab);
       StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

       // {"properties":{"num":7}}
       addWritable(stuff, 7);
	Writable jsw = jserde.serialize(stuff, rowOI);
	JsonNode jn = new ObjectMapper().readTree(((Text)jsw).toString());
	jn = jn.findValue("properties");
	jn = jn.findValue("num");
	Assert.assertEquals(7, jn.getIntValue());
}
 
Example 3
Source File: TestGeoJsonSerDe.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void TestEpochWrite() throws Exception {
       ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "when");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "date");
	AbstractSerDe jserde = mkSerDe(proptab);
       StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

       // {"properties":{"when":147147147147}}
	long epoch = 0L;  // 147147147147L;
	java.sql.Date expected = new java.sql.Date(epoch - TimeZone.getDefault().getOffset(epoch));
       addWritable(stuff, expected);
	Writable jsw = jserde.serialize(stuff, rowOI);
	JsonNode jn = new ObjectMapper().readTree(((Text)jsw).toString());
	jn = jn.findValue("properties");
	jn = jn.findValue("when");
	java.sql.Date actual = new java.sql.Date(jn.getLongValue());
	long day = 24*3600*1000;  // DateWritable stores days not milliseconds.
	Assert.assertEquals(epoch/day, jn.getLongValue()/day);
}
 
Example 4
Source File: TestGeoJsonSerDe.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void TestPointWrite() throws Exception {
       ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "shape");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "binary");
	AbstractSerDe jserde = mkSerDe(proptab);
       StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

       // {"properties":{},"geometry":{"type":"Point","coordinates":[15.0,5.0]}}
       addWritable(stuff, new Point(15.0, 5.0));
	Writable jsw = jserde.serialize(stuff, rowOI);
       String rslt = ((Text)jsw).toString();
	JsonNode jn = new ObjectMapper().readTree(rslt);
	jn = jn.findValue("geometry");
	Assert.assertNotNull(jn.findValue("type"));
	Assert.assertNotNull(jn.findValue("coordinates"));
}
 
Example 5
Source File: TestEsriJsonSerDe.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void TestIntWrite() throws Exception {
       ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "num");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "int");
	AbstractSerDe jserde = mkSerDe(proptab);
       StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

       // {"attributes":{"num":7}}
       addWritable(stuff, 7);
	Writable jsw = jserde.serialize(stuff, rowOI);
	JsonNode jn = new ObjectMapper().readTree(((Text)jsw).toString());
	jn = jn.findValue("attributes");
	jn = jn.findValue("num");
	Assert.assertEquals(7, jn.getIntValue());
}
 
Example 6
Source File: TestEsriJsonSerDe.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void TestEpochWrite() throws Exception {
	ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "when");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "date");
	AbstractSerDe jserde = mkSerDe(proptab);
	StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

	// {"attributes":{"when":147147147147}}
	long epoch = 147147147147L;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MMM-dd");
	sdf.setTimeZone(TimeZone.getTimeZone("America/New_York"));
	java.sql.Date expected = new java.sql.Date(epoch);
	String expString = sdf.format(expected);
	//System.err.println(expected.getTime());
	addWritable(stuff, expected);
	Writable jsw = jserde.serialize(stuff, rowOI);
	JsonNode jn = new ObjectMapper().readTree(((Text)jsw).toString());
	jn = jn.findValue("attributes");
	jn = jn.findValue("when");
	java.sql.Date actual = new java.sql.Date(jn.getLongValue());
	String actualDateString = sdf.format(actual);
	Assert.assertEquals(expString, actualDateString);  // workaround DateWritable,j.s.Date
}
 
Example 7
Source File: TestEsriJsonSerDe.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void TestTimeWrite() throws Exception {
       ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "when");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "timestamp");
	AbstractSerDe jserde = mkSerDe(proptab);
       StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

       // {"attributes":{"when":147147147147}}
       long epoch = 147147147147L;
	java.sql.Timestamp expected = new java.sql.Timestamp(epoch);
       addWritable(stuff, expected);
	Writable jsw = jserde.serialize(stuff, rowOI);
	JsonNode jn = new ObjectMapper().readTree(((Text)jsw).toString());
	jn = jn.findValue("attributes");
	jn = jn.findValue("when");
	java.sql.Timestamp actual = new java.sql.Timestamp(jn.getLongValue());
	Assert.assertEquals(expected, actual);
}
 
Example 8
Source File: TestEsriJsonSerDe.java    From spatial-framework-for-hadoop with Apache License 2.0 6 votes vote down vote up
@Test
public void TestPointWrite() throws Exception {
       ArrayList<Object> stuff = new ArrayList<Object>();
	Properties proptab = new Properties();
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMNS, "shape");
	proptab.setProperty(HiveShims.serdeConstants.LIST_COLUMN_TYPES, "binary");
	AbstractSerDe jserde = mkSerDe(proptab);
       StructObjectInspector rowOI = (StructObjectInspector)jserde.getObjectInspector();

       // {"attributes":{},"geometry":{"x":15.0,"y":5.0}}
       addWritable(stuff, new Point(15.0, 5.0));
	Writable jsw = jserde.serialize(stuff, rowOI);
       String rslt = ((Text)jsw).toString();
	JsonNode jn = new ObjectMapper().readTree(rslt);
	jn = jn.findValue("geometry");
	Assert.assertNotNull(jn.findValue("x"));
	Assert.assertNotNull(jn.findValue("y"));
}
 
Example 9
Source File: NiFiClient.java    From ranger with Apache License 2.0 5 votes vote down vote up
public List<String> getResources(ResourceLookupContext context) throws Exception {
    final WebResource resource = getWebResource();
    final ClientResponse response = getResponse(resource, "application/json");

    if (Response.Status.OK.getStatusCode() != response.getStatus()) {
        String errorMsg = IOUtils.toString(response.getEntityInputStream());
        throw new Exception("Unable to retrieve resources from NiFi due to: " + errorMsg);
    }

    JsonNode rootNode = mapper.readTree(response.getEntityInputStream());
    if (rootNode == null) {
        throw new Exception("Unable to retrieve resources from NiFi");
    }

    JsonNode resourcesNode = rootNode.findValue("resources");
    List<String> identifiers = resourcesNode.findValuesAsText("identifier");

    final String userInput = context.getUserInput();
    if (StringUtils.isBlank(userInput)) {
        return identifiers;
    } else {
        List<String> filteredIdentifiers = new ArrayList<>();

        for (String identifier : identifiers) {
            if (identifier.contains(userInput)) {
                filteredIdentifiers.add(identifier);
            }
        }

        return filteredIdentifiers;
    }
}
 
Example 10
Source File: JsonRestSourceHandler.java    From ingestion with Apache License 2.0 5 votes vote down vote up
private JsonNode findValue(JsonNode jsonNode, String path) {
    JsonNode node = jsonNode;
    if (!DEFAULT_JSON_PATH.equals(path)) {
        node = jsonNode.findValue(path);
    }
    return node;
}
 
Example 11
Source File: SliTokenExtractor.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Override
public Token extract(String response) {
    Preconditions.checkEmptyString(response,
            "Response body is incorrect. Can't extract a token from an empty string");
    JsonNode root;
    try {
        root = mapper.readTree(response);
        JsonNode token = root.findValue("access_token");
        return new Token(token.asText(), "", response);
        
    } catch (Exception e) {
        return null;
    }
}