org.codehaus.jackson.JsonNode Java Examples

The following examples show how to use org.codehaus.jackson.JsonNode. 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: BlueprintCommandsTest.java    From ambari-shell with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddBlueprintForFileReadPrecedence() throws IOException {
  File file = new File("src/test/resources/testBlueprint.json");
  String json = IOUtils.toString(new FileInputStream(file));
  JsonNode jsonNode = mock(JsonNode.class);
  when(objectMapper.readTree(json.getBytes())).thenReturn(jsonNode);
  when(jsonNode.get("Blueprints")).thenReturn(jsonNode);
  when(jsonNode.get("blueprint_name")).thenReturn(jsonNode);
  when(jsonNode.asText()).thenReturn("blueprintName");

  String result = blueprintCommands.addBlueprint("url", file);

  verify(ambariClient).addBlueprint(json);
  verify(context).setHint(Hints.BUILD_CLUSTER);
  verify(context).setBlueprintsAvailable(true);
  assertEquals("Blueprint: 'blueprintName' has been added", result);
}
 
Example #2
Source File: APIDashboardTask.java    From kardio with Apache License 2.0 6 votes vote down vote up
/**
 * @param envInfo
 * @param appName
 * @return
 * @throws Exception
 */
private static String getMarathonAppLaunchDate(EnvironmentVO envInfo,String appName) throws Exception {
	String appVerUrl = envInfo.getMarathonUrl()+"//" + appName + "/versions";
	String appVersionJson = null;
	try{
		appVersionJson = RestCommunicationHandler.getResponse(appVerUrl, true, "Basic ", envInfo.getMarathonCred());
	}catch(FileNotFoundException ex){
		logger.error("Unable to get Version of API : AppName: " + appName + "; URL :"+ appVerUrl, ex);
	}

	if(appVersionJson == null){
		return null;
	}
	ObjectMapper mapper = new ObjectMapper();
	JsonNode rootNode = mapper.readTree(appVersionJson).get("versions");
	String launchDate = null; 
	if(rootNode.isArray()){
		launchDate = rootNode.get(rootNode.size() - 1).asText();
	} else {
		logger.info("Launch Date for "+ appName +" is null");
	}
	return launchDate;
}
 
Example #3
Source File: ShuffleRewriter.java    From Cubert with Apache License 2.0 6 votes vote down vote up
@Override
public JsonNode rewrite(JsonNode plan,
                        Set<String> namesUsed,
                        boolean debugMode,
                        boolean revisit)
{
    this.namesUsed = namesUsed;

    ObjectNode newPlan = (ObjectNode) cloneNode(plan);

    ArrayNode jobs = mapper.createArrayNode();

    for (JsonNode job : plan.path("jobs"))
    {
        jobs.add(rewriteJob(job));
    }

    newPlan.remove("jobs");
    newPlan.put("jobs", jobs);

    return newPlan;
}
 
Example #4
Source File: TestWorkflowAccessor.java    From helix with Apache License 2.0 6 votes vote down vote up
@Test(dependsOnMethods = "testGetWorkflows")
public void testGetWorkflow() throws IOException {
  System.out.println("Start test :" + TestHelper.getTestMethodName());
  String body = get("clusters/" + CLUSTER_NAME + "/workflows/" + WORKFLOW_NAME, null,
      Response.Status.OK.getStatusCode(), true);
  JsonNode node = OBJECT_MAPPER.readTree(body);
  Assert.assertNotNull(node.get(WorkflowAccessor.WorkflowProperties.WorkflowConfig.name()));
  Assert.assertNotNull(node.get(WorkflowAccessor.WorkflowProperties.WorkflowContext.name()));

  TaskExecutionInfo lastScheduledTask = OBJECT_MAPPER
      .treeToValue(node.get(WorkflowAccessor.WorkflowProperties.LastScheduledTask.name()),
          TaskExecutionInfo.class);
  Assert.assertTrue(lastScheduledTask
      .equals(new TaskExecutionInfo(null, null, null, TaskExecutionInfo.TIMESTAMP_NOT_SET)));
  String workflowId =
      node.get(WorkflowAccessor.WorkflowProperties.WorkflowConfig.name()).get("WorkflowID")
          .getTextValue();
  Assert.assertEquals(workflowId, WORKFLOW_NAME);
  System.out.println("End test :" + TestHelper.getTestMethodName());
}
 
Example #5
Source File: MaxAggregation.java    From Cubert with Apache License 2.0 6 votes vote down vote up
@Override
public BlockSchema outputSchema(BlockSchema inputSchema, JsonNode json) throws PreconditionException
{
    String[] inputColNames = JsonUtils.asArray(json, "input");
    if (inputColNames.length != 1)
        throw new PreconditionException(PreconditionExceptionType.INVALID_CONFIG,
                                        "Only one column expected for MAX. Found: "
                                                + JsonUtils.get(json, "input"));

    String inputColName = inputColNames[0];
    if (!inputSchema.hasIndex(inputColName))
        throw new PreconditionException(PreconditionExceptionType.COLUMN_NOT_PRESENT,
                                        inputColName);

    String outputColName = JsonUtils.getText(json, "output");
    DataType inputType = inputSchema.getType(inputSchema.getIndex(inputColName));

    return new BlockSchema(inputType + " " + outputColName);
}
 
Example #6
Source File: RankOperator.java    From Cubert with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see com.linkedin.rcf.operator.TupleOperator#setInput(java.util.Map,
 *      org.codehaus.jackson.JsonNode)
 */
@Override
public void setInput(Map<String, Block> input, JsonNode json, BlockProperties props) throws IOException,
InterruptedException
{
    inputBlock = input.values().iterator().next();
    BlockSchema inputSchema = inputBlock.getProperties().getSchema();

    outputSchema = getOutputSchema(json, inputSchema);
    outputTuple = TupleFactory.getInstance().newTuple(outputSchema.getNumColumns());
    rankColumnIndex = outputSchema.getNumColumns() - 1;

    pivotBlockOp = new PivotBlockOperator();
    String[] groupByColumns = JsonUtils.asArray(JsonUtils.get(json, "groupBy"));
    pivotBlockOp.setInput(inputBlock, groupByColumns, false);

    resetState();
}
 
Example #7
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 #8
Source File: FlowRestfulApi.java    From DataSphereStudio with Apache License 2.0 6 votes vote down vote up
@POST
@Path("/updateFlowBaseInfo")
public Response updateFlowBaseInfo(@Context HttpServletRequest req, JsonNode json) throws DSSErrorException {
    Long flowID = json.get("id").getLongValue();
    String name = json.get("name")==null?null:json.get("name").getTextValue();
    String description = json.get("description") == null?null:json.get("description").getTextValue();
    Long taxonomyID = json.get("taxonomyID") == null?null:json.get("taxonomyID").getLongValue();
    Long projectVersionID = json.get("projectVersionID").getLongValue();
    String uses = json.get("uses") == null?null:json.get("uses").getTextValue();
    publishManager.checkeIsPublishing(projectVersionID);
    // TODO: 2019/6/13  projectVersionID的更新校验
    //这里可以不做事务
    DWSFlow dwsFlow = new DWSFlow();
    dwsFlow.setId(flowID);
    dwsFlow.setName(name);
    dwsFlow.setDescription(description);
    dwsFlow.setUses(uses);
    flowService.updateFlowBaseInfo(dwsFlow,projectVersionID,taxonomyID);
    return Message.messageToResponse(Message.ok());
}
 
Example #9
Source File: LanguageResourceService.java    From guacamole-client with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the given language resource, returning the resulting JsonNode.
 * If the resource cannot be read because it does not exist, null is
 * returned.
 *
 * @param resource
 *     The language resource to parse. Language resources must have the
 *     mimetype "application/json".
 *
 * @return
 *     A JsonNode representing the root of the parsed JSON tree, or null if
 *     the given resource does not exist.
 *
 * @throws IOException
 *     If an error occurs while parsing the resource as JSON.
 */
private JsonNode parseLanguageResource(Resource resource) throws IOException {

    // Get resource stream
    InputStream stream = resource.asStream();
    if (stream == null)
        return null;

    // Parse JSON tree
    try {
        JsonNode tree = mapper.readTree(stream);
        return tree;
    }

    // Ensure stream is always closed
    finally {
        stream.close();
    }

}
 
Example #10
Source File: DefaultCubeAggregator.java    From Cubert with Apache License 2.0 6 votes vote down vote up
@Override
public void setup(Block block, BlockSchema outputSchema, JsonNode json) throws IOException
{
    BlockSchema inputSchema = block.getProperties().getSchema();

    // determine the input column name and index within the input tuple
    String inColName = null;
    if (json.has("input") && !json.get("input").isNull())
        inColName = JsonUtils.asArray(json, "input")[0];

    if (inColName != null)
        valueIndex = inputSchema.getIndex(inColName);
    else
        valueIndex = -1;

    // determine the name of output column name the index within the output tuple
    String outColName = JsonUtils.getText(json, "output");
    outIndex = outputSchema.getIndex(outColName);
}
 
Example #11
Source File: RecordWithMetadataToEnvelopedRecordWithMetadataTest.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessWithString()
    throws DataConversionException, IOException {
  ObjectMapper objectMapper = new ObjectMapper();
  String innerRecord = "abracadabra";
  RecordWithMetadataToEnvelopedRecordWithMetadata converter = new RecordWithMetadataToEnvelopedRecordWithMetadata();
  RecordWithMetadata<String> record = new RecordWithMetadata<>(innerRecord, new Metadata());

  Iterator<RecordWithMetadata<byte[]>> recordWithMetadataIterator =
      converter.convertRecord("", record, null).iterator();
  RecordWithMetadata recordWithMetadata = recordWithMetadataIterator.next();

  JsonNode parsedElement = objectMapper.readValue((byte[]) recordWithMetadata.getRecord(), JsonNode.class);
  Assert.assertEquals(parsedElement.get("mId").getTextValue(), record.getMetadata().getGlobalMetadata().getId());
  Assert.assertEquals(parsedElement.get("r").getTextValue(), innerRecord);

  Assert
      .assertEquals(recordWithMetadata.getMetadata().getGlobalMetadata().getContentType(), "lnkd+recordWithMetadata");
  Assert.assertNull(recordWithMetadata.getMetadata().getGlobalMetadata().getInnerContentType());
}
 
Example #12
Source File: KrakenExchange.java    From libdynticker with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected List<Pair> getPairsFromAPI() throws IOException {
	List<Pair> pairs = new ArrayList<Pair>();
	JsonNode node = readJsonFromUrl("https://api.kraken.com/0/public/AssetPairs");
	if(node.get("error").getElements().hasNext())
		throw new IOException(node.get("error").get(0).asText());
	else {
		String coin;
		for(JsonNode asset : node.get("result")) {
			coin = asset.get("base").asText();
			if(coin.charAt(0) == 'X')
				coin = coin.substring(1);
			pairs.add(new Pair(coin, asset.get("quote").asText().substring(1)));
		}
	}
	return pairs;
}
 
Example #13
Source File: TopTweet.java    From flink-examples with MIT License 6 votes vote down vote up
@Override
public void flatMap(String tweetJsonStr, Collector<Tuple2<String, Integer>> collector) throws Exception {
    JsonNode tweetJson = mapper.readTree(tweetJsonStr);
    JsonNode entities = tweetJson.get("entities");
    if (entities == null) return;

    JsonNode hashtags = entities.get("hashtags");
    if (hashtags == null) return;

    for (Iterator<JsonNode> iter = hashtags.getElements(); iter.hasNext();) {
        JsonNode node = iter.next();
        String hashtag = node.get("text").getTextValue();

        if (hashtag.matches("\\w+")) {
            collector.collect(new Tuple2<>(hashtag, 1));
        }
    }
}
 
Example #14
Source File: BTC38Exchange.java    From libdynticker with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
protected List<Pair> getPairsFromAPI() throws IOException {
	List<Pair> pairs = new ArrayList<Pair>();
	String[] exchanges = { "cny", "btc" };
	String addr = "http://api.btc38.com/v1/ticker.php?c=all&mk_type=";
	for(String exch : exchanges) {
		JsonNode node = readJsonFromUrl(addr + exch);
		Iterator<String> coins = node.getFieldNames();
		for(String coin; coins.hasNext();) {
			coin = coins.next();
			if(node.get(coin).get("ticker").isObject()) {
				pairs.add(new Pair(coin.toUpperCase(), exch.toUpperCase()));
			}
		}
	}
	return pairs;
}
 
Example #15
Source File: Oauth2JwtClientTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test
public void testOK() throws UnsupportedEncodingException, IOException, InterruptedException, URISyntaxException {
    // start server
    JwtTestServer server = new JwtTestServer(UseCase.OK);
    Thread serverThread = new Thread(server);
    serverThread.start();

    Map<String, String> params = new HashMap<>();
    params.put("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");

    // Get Access Token
    JsonNode accessToken = Oauth2JwtClient.builder()//
            .withJwsHeaders(jwsHeaders())//
            .withJwtClaims(claims())//
            .signWithX509Key(x509Key(), org.talend.components.common.oauth.X509Key.Algorithm.SHA256withRSA)//
            .fromTokenEndpoint("http://" + endpoint + ":" + server.getLocalPort())//
            .withPlayloadParams(params)//
            .build()//
            .getAccessToken();

    serverThread.join(30000);

    assertNotNull(accessToken);
    assertNotNull(accessToken.get("access_token"));
}
 
Example #16
Source File: JobExecutor.java    From Cubert with Apache License 2.0 5 votes vote down vote up
public static JsonNode asArray(JsonNode node, String property)
{
    JsonNode n = node.get(property);
    if (n.isArray())
        return node.path(property);
    else
    {
        singletonArray.removeAll();
        singletonArray.add(n);
        return singletonArray;
    }
}
 
Example #17
Source File: AvroUtils.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/***
 * Copy properties from old Avro Schema to new Avro Schema
 * @param oldSchema Old Avro Schema to copy properties from
 * @param newSchema New Avro Schema to copy properties to
 */
private static void copyProperties(Schema oldSchema, Schema newSchema) {
  Preconditions.checkNotNull(oldSchema);
  Preconditions.checkNotNull(newSchema);

  Map<String, JsonNode> props = oldSchema.getJsonProps();
  copyProperties(props, newSchema);
}
 
Example #18
Source File: MercadoBitcoinExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	String url = "https://www.mercadobitcoin.net/api/";
	if(pair.equals(BTCBRL))
		url += "ticker/";
	else if(pair.equals(LTCBRL))
		url += "ticker_litecoin/";
	JsonNode node = readJsonFromUrl(url);
	if(node.has("ticker"))
		return parseTicker(node, pair);
	else
		throw new NoMarketDataException(pair);
}
 
Example #19
Source File: Directive.java    From dcs-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public Directive deserialize(JsonParser jp, DeserializationContext ctx)
        throws IOException {
    ObjectReader reader = ObjectMapperUtil.instance().getObjectReader();
    ObjectNode obj = (ObjectNode) reader.readTree(jp);
    Iterator<Map.Entry<String, JsonNode>> elementsIterator = obj.getFields();

    String rawMessage = obj.toString();
    DialogRequestIdHeader header = null;
    JsonNode payloadNode = null;
    ObjectReader headerReader =
            ObjectMapperUtil.instance().getObjectReader(DialogRequestIdHeader.class);
    while (elementsIterator.hasNext()) {
        Map.Entry<String, JsonNode> element = elementsIterator.next();
        if (element.getKey().equals("header")) {
            header = headerReader.readValue(element.getValue());
        }
        if (element.getKey().equals("payload")) {
            payloadNode = element.getValue();
        }
    }
    if (header == null) {
        throw ctx.mappingException("Missing header");
    }
    if (payloadNode == null) {
        throw ctx.mappingException("Missing payload");
    }

    return createDirective(header, payloadNode, rawMessage);
}
 
Example #20
Source File: JsonPathReader.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected SchemaAccessStrategy getSchemaAccessStrategy(final String strategy, final SchemaRegistry schemaRegistry, final PropertyContext context) {
    final RecordSourceFactory<JsonNode> jsonSourceFactory = (var, in) -> new JsonRecordSource(in);
    final Supplier<SchemaInferenceEngine<JsonNode>> inferenceSupplier = () -> new JsonSchemaInference(new TimeValueInference(dateFormat, timeFormat, timestampFormat));

    return SchemaInferenceUtil.getSchemaAccessStrategy(strategy, context, getLogger(), jsonSourceFactory, inferenceSupplier,
        () -> super.getSchemaAccessStrategy(strategy, schemaRegistry, context));
}
 
Example #21
Source File: JsonUtils.java    From urule with Apache License 2.0 5 votes vote down vote up
public static Value parseValueNode(JsonNode valueNode){
	Value value=null;
	ValueType valueType=ValueType.valueOf(getJsonValue(valueNode, "valueType"));
	for(ValueDeserializer des:valueDeserializers){
		if(des.support(valueType)){
			value = des.deserialize(valueNode);
			break;
		}
	}
	return value;
}
 
Example #22
Source File: CustomFieldDeSerializer.java    From jira-rest-client with Apache License 2.0 5 votes vote down vote up
@Override
public Object deserialize(JsonParser jp, DeserializationContext ctxt)
		throws IOException, JsonProcessingException {
	logger.info("Deserialize...");

	ObjectCodec oc = jp.getCodec();
	JsonNode node = oc.readTree(jp);
	
	for (int i = 0; i < node.size(); i++)
	{
		JsonNode child = node.get(i);
		
		if (child == null) {
			logger.info(i + "th Child node is null");
			continue;
		}
		//String
		if (child.isTextual()) {
			Iterator<String> it = child.getFieldNames();
			while (it.hasNext()) {
				String field = it.next();
				logger.info("in while loop " + field);
				if (field.startsWith("customfield")) {
					
				}
			}
		}
	}
	return null;
}
 
Example #23
Source File: TheRockTradingExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected List<Pair> getPairsFromAPI() throws IOException {
	List<Pair> pairs = new ArrayList<Pair>();
	JsonNode node = readJsonFromUrl("https://api.therocktrading.com/v1/funds");
	JsonNode elements = node.get("funds");
	for(JsonNode element : elements){
		String id = element.get("id").asText();
		String coin = element.get("trade_currency").asText();
		String exchange = element.get("base_currency").asText();
		pairs.add(new Pair(coin, exchange, id));
	}
	return Collections.unmodifiableList(pairs);
}
 
Example #24
Source File: BitFlyerExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	// https://api.bitflyer.jp/v1/ticker?product_code=ETH_BTC
	String productCode = pair.getCoin() + "_" + pair.getExchange();
	JsonNode node = readJsonFromUrl("https://api.bitflyer.jp/v1/ticker?product_code=" + productCode);
	if(node.get("product_code").asText().equals(productCode))
		return parseTicker(node, pair);
	else
		throw new NoMarketDataException(pair);
}
 
Example #25
Source File: WindowsEnrollment.java    From product-emm with Apache License 2.0 5 votes vote down vote up
@Test(groups = Constants.WindowsEnrollment.WINDOWS_ENROLLMENT_GROUP, description = "Test Windows BST.",
        dependsOnMethods = {"testDiscoveryPost"})
public void testBST() throws Exception {
    String token = "token";
    ObjectMapper mapper = new ObjectMapper();
    JsonNode root = mapper.readTree(Constants.WindowsEnrollment.BSD_PAYLOAD);
    JsonNode node = root.path("credentials");
    ((ObjectNode) node).put(token, OAuthUtil.getOAuthToken(backendHTTPURL, backendHTTPSURL));
    client.setHttpHeader(Constants.CONTENT_TYPE, Constants.APPLICATION_JSON);
    MDMResponse response = client.post(Constants.WindowsEnrollment.BSD_URL, root.toString());
    bsd = response.getBody();
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatus());
}
 
Example #26
Source File: GatecoinExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public String parseTicker(JsonNode node, Pair pair) throws IOException {
	JsonNode transactions = node.get("transactions");
	if(transactions.size() > 0)
		return transactions.get(0).get("price").asText();
	else
		return "NaN";
}
 
Example #27
Source File: GatecoinExchange.java    From libdynticker with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected String getTicker(Pair pair) throws IOException {
	// https://api.gatecoin.com/Public/Transactions/BTCEUR?count=1
	JsonNode node = readJsonFromUrl("https://api.gatecoin.com/Public/Transactions/" + pair.getCoin() + pair.getExchange() + "?count=1");
	String message = node.get("responseStatus").get("message").asText();
	if(message.equals("OK"))
		return parseTicker(node, pair);
	else
		throw new IOException(message);
}
 
Example #28
Source File: JsonUtil.java    From hbase-indexer with Apache License 2.0 5 votes vote down vote up
public static byte[] getBinary(JsonNode node, String prop) throws JsonFormatException {
    if (node.get(prop) == null) {
        throw new JsonFormatException("Missing required property: " + prop);
    }
    try {
        return node.get(prop).getBinaryValue();
    } catch (IOException e) {
        throw new JsonFormatException("Error reading binary data in property " + prop, e);
    }
}
 
Example #29
Source File: ParameterValueDeserializer.java    From urule with Apache License 2.0 5 votes vote down vote up
@Override
public Value deserialize(JsonNode jsonNode) {
	ParameterValue value=new ParameterValue();
	value.setArithmetic(JsonUtils.parseComplexArithmetic(jsonNode));
	value.setVariableLabel(JsonUtils.getJsonValue(jsonNode, "variableLabel"));
	value.setVariableName(JsonUtils.getJsonValue(jsonNode, "variableName"));
	return value;
}
 
Example #30
Source File: GenericEntityDeserializer.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private List<Object> processArray(final String key, final ArrayNode asJsonArray) {
    List<Object> list = new LinkedList<Object>();
    
    Iterator<JsonNode> it = asJsonArray.getElements();
    while (it.hasNext()) {
        list.add(processElement(key, it.next()));
    }
    return list;
}