com.jayway.jsonpath.InvalidPathException Java Examples

The following examples show how to use com.jayway.jsonpath.InvalidPathException. 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: JsonInputTest.java    From hop with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonInputPathResolutionSuccess() {
  JsonInputField inputField = new JsonInputField( "value" );
  final String PATH = "${PARAM_PATH}.price";
  inputField.setPath( PATH );
  inputField.setType( IValueMeta.TYPE_STRING );
  final JsonInputMeta inputMeta = createSimpleMeta( "json", inputField );
  IVariables variables = new Variables();
  JsonInput jsonInput = null;
  try {
    jsonInput =
      createJsonInput( "json", inputMeta, variables, new Object[] { getBasicTestJson() } );
    fail( "Without the parameter, this call should fail with an InvalidPathException. If it does not, test fails." );
  } catch ( InvalidPathException pathException ) {
    assertNull( jsonInput );
  }

  variables.setVariable( "PARAM_PATH", "$..book.[*]" );

  try {
    jsonInput = createJsonInput( "json", inputMeta, variables, new Object[] { getBasicTestJson() } );
    assertNotNull( jsonInput );
  } catch ( Exception ex ) {
    fail( "Json Input should be able to resolve the paths with the parameter introduced in the variable space." );
  }
}
 
Example #2
Source File: JacksonJsonPathQuery.java    From camunda-spin with Apache License 2.0 6 votes vote down vote up
public SpinJsonNode element() {
  try {
    Object result = query.read(spinJsonNode.toString(), dataFormat.getJsonPathConfiguration());
    JsonNode node;
    if (result != null) {
      node = dataFormat.createJsonNode(result);
    } else {
      node = dataFormat.createNullJsonNode();
    }
    return dataFormat.createWrapperInstance(node);
  } catch(PathNotFoundException pex) {
    throw LOG.unableToEvaluateJsonPathExpressionOnNode(spinJsonNode, pex);
  } catch (ClassCastException cex) {
    throw LOG.unableToCastJsonPathResultTo(SpinJsonNode.class, cex);
  } catch(InvalidPathException iex) {
    throw LOG.invalidJsonPath(SpinJsonNode.class, iex);
  }
}
 
Example #3
Source File: JsonInputTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonInputPathResolutionSuccess() {
  JsonInputField inputField = new JsonInputField( "value" );
  final String PATH = "${PARAM_PATH}.price";
  inputField.setPath( PATH );
  inputField.setType( ValueMetaInterface.TYPE_STRING );
  final JsonInputMeta inputMeta = createSimpleMeta( "json", inputField );
  VariableSpace variables = new Variables();
  JsonInput jsonInput = null;
  try {
    jsonInput =
      createJsonInput( "json", inputMeta, variables, new Object[] { getBasicTestJson() } );
    fail( "Without the parameter, this call should fail with an InvalidPathException. If it does not, test fails." );
  } catch ( InvalidPathException pathException ) {
    assertNull( jsonInput );
  }

  variables.setVariable( "PARAM_PATH", "$..book.[*]" );

  try {
    jsonInput = createJsonInput( "json", inputMeta, variables, new Object[] { getBasicTestJson() } );
    assertNotNull( jsonInput );
  } catch ( Exception ex ) {
    fail( "Json Input should be able to resolve the paths with the parameter introduced in the variable space." );
  }
}
 
Example #4
Source File: JsonInputTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonInputPathResolution() throws KettleException {
  JsonInputField inputField = new JsonInputField( "value" );
  final String PATH = "$[*].name";
  inputField.setPath( PATH );
  inputField.setType( ValueMetaInterface.TYPE_STRING );
  JsonInputMeta inputMeta = createSimpleMeta( "json", inputField );
  VariableSpace variables = new Variables();
  JsonInput jsonInput = null;
  try {
    jsonInput =
      createJsonInput( "json", inputMeta, variables, new Object[] { getSampleJson() } );

    JsonInputData data = (JsonInputData) Whitebox.getInternalState( jsonInput, "data" );
    FastJsonReader reader = (FastJsonReader) Whitebox.getInternalState( data, "reader" );
    RowSet rowset = reader.parse( new ByteArrayInputStream( getSampleJson().getBytes() ) );
    List results = (List) Whitebox.getInternalState( rowset, "results" );
    JSONArray jsonResult = (JSONArray) results.get( 0 );

    assertEquals( 1, jsonResult.size() );
    assertEquals( "United States of America", jsonResult.get( 0 ) );

  } catch ( InvalidPathException pathException ) {
    assertNull( jsonInput );
  }
}
 
Example #5
Source File: GenericJsonEvent.java    From bender with Apache License 2.0 5 votes vote down vote up
@Override
public Object getField(String field) throws FieldNotFoundException {
  if (this.payload == null) {
    throw new FieldNotFoundException(field + " is not in payload because payload is null");
  }

  JsonObject json = this.payload.getAsJsonObject();
  Object obj;
  try {
    obj = JsonPathProvider.read(json, field);
  } catch(InvalidPathException e) {
    throw new FieldNotFoundException("Field cannot be found because " + field
        + " is an invalid path");
  }

  if (obj == null || obj instanceof JsonNull) {
    throw new FieldNotFoundException(field + " is not in payload.");
  }

  if (obj instanceof JsonPrimitive) {
    if (((JsonPrimitive) obj).isString()) {
      return ((JsonPrimitive) obj).getAsString();
    }
  }

  return obj;
}
 
Example #6
Source File: JacksonJsonNode.java    From camunda-spin with Apache License 2.0 5 votes vote down vote up
public SpinJsonPathQuery jsonPath(String expression) {
  ensureNotNull("expression", expression);
  try {
    JsonPath query = JsonPath.compile(expression);
    return new JacksonJsonPathQuery(this, query, dataFormat);
  } catch(InvalidPathException pex) {
    throw LOG.unableToCompileJsonPathExpression(expression, pex);
  } catch(IllegalArgumentException aex) {
    throw LOG.unableToCompileJsonPathExpression(expression, aex);
  }
}
 
Example #7
Source File: JsonFunctions.java    From calcite with Apache License 2.0 4 votes vote down vote up
public static JsonPathContext withStrictException(String pathSpec, Exception exc) {
  if (exc.getClass() == InvalidPathException.class) {
    exc = RESOURCE.illegalJsonPathSpec(pathSpec).ex();
  }
  return withStrictException(exc);
}