com.jayway.jsonpath.spi.json.JsonProvider类的使用及代码示例

x33g5p2x  于2022-01-22 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(122)

本文整理了Java中com.jayway.jsonpath.spi.json.JsonProvider类的一些代码示例,展示了JsonProvider类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JsonProvider类的具体详情如下:
包路径:com.jayway.jsonpath.spi.json.JsonProvider
类名称:JsonProvider

JsonProvider介绍

暂无

代码示例

代码示例来源:origin: json-path/JsonPath

/**
   * Evaluate the JSON document at the point of need using the JSON parameter and associated document model which may
   * itself originate from yet another function thus recursively invoking late binding methods.
   *
   * @return
   */
  @Override
  public Object get() {
    return jsonProvider.parse(jsonParameter.getJson());
  }
}

代码示例来源:origin: json-path/JsonPath

@Override
public String jsonString() {
  return configuration.jsonProvider().toJson(json);
}

代码示例来源:origin: json-path/JsonPath

public void add(Object value, Configuration configuration){
  Object target = configuration.jsonProvider().getMapValue(parent, property);
  if(targetInvalid(target)){
    return;
  }
  if(configuration.jsonProvider().isArray(target)){
    configuration.jsonProvider().setArrayIndex(target, configuration.jsonProvider().length(target), value);
  } else {
    throw new InvalidModificationException("Can only add to an array");
  }
}

代码示例来源:origin: json-path/JsonPath

@Override
  public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
    JsonProvider jsonProvider = ctx.configuration().jsonProvider();
    if (parameters != null && parameters.size() > 0) {
      for (Parameter param : parameters) {
        if (jsonProvider.isArray(model)) {
          int len = jsonProvider.length(model);
          jsonProvider.setArrayIndex(model, len, param.getValue());
        }
      }
    }
    return model;
  }
}

代码示例来源:origin: json-path/JsonPath

protected void renameInMap(Object targetMap, String oldKeyName, String newKeyName, Configuration configuration){
  if(configuration.jsonProvider().isMap(targetMap)){
    if(configuration.jsonProvider().getMapValue(targetMap, oldKeyName) == JsonProvider.UNDEFINED){
      throw new PathNotFoundException("No results for Key "+oldKeyName+" found in map!");
    }
    configuration.jsonProvider().setProperty(targetMap, newKeyName, configuration.jsonProvider().getMapValue(targetMap, oldKeyName));
    configuration.jsonProvider().removeProperty(targetMap, oldKeyName);
  } else {
    throw new InvalidModificationException("Can only rename properties in a map");
  }
}

代码示例来源:origin: json-path/JsonPath

@Override
public <T> T map(Object source, Class<T> targetType, Configuration configuration) {
  if(source == null){
    return null;
  }
  if (targetType.isAssignableFrom(source.getClass())) {
    return (T) source;
  }
  try {
    if(!configuration.jsonProvider().isMap(source) && !configuration.jsonProvider().isArray(source)){
      return factory.call().getMapper(targetType).convert(source);
    }
    String s = configuration.jsonProvider().toJson(source);
    return (T) JSONValue.parse(s, targetType);
  } catch (Exception e) {
    throw new MappingException(e);
  }
}

代码示例来源:origin: com.github.lafa.jsonpath/json-path

if (isLeaf()) {
  if (ctx.configuration().getComputeRoot()) {
    if (null == ctx.configuration().jsonProvider().getProperty(ctx.getParent(), property)) {
      ctx.configuration().jsonProvider().setProperty(ctx.getParent(), property, propertyVal);
  Object curr = null;
  if (ctx.configuration().getComputeRoot()) {
    curr = ctx.configuration().jsonProvider().getProperty(prev, property);
    if (null == curr) {
      if (ctx.configuration().jsonProvider().isMap(propertyVal)) {
        curr = ctx.configuration().jsonProvider().createMap();
      } else if (ctx.configuration().jsonProvider().isArray(propertyVal)) {
        curr = ctx.configuration().jsonProvider().createArray();
      } else {
        throw new IllegalArgumentException("unknown type");
      ctx.configuration().jsonProvider().setProperty(prev, property, curr);
      if (ctx.configuration().jsonProvider().isMap(curr) || ctx.configuration().jsonProvider().isArray(curr)) {
        if (ctx.configuration().jsonProvider().length(curr) == 0) {
          ctx.configuration().jsonProvider().removeProperty(prev, property);
Object merged = ctx.jsonProvider().createMap();
for (String property : properties) {
  Object propertyVal;
    if (null == ctx.configuration().jsonProvider().getProperty(ctx.getParent(), property)) {
      ctx.configuration().jsonProvider().setProperty(ctx.getParent(), property, propertyVal);

代码示例来源:origin: json-path/JsonPath

@SuppressWarnings("unchecked")
@Override
public <T> T getValue(boolean unwrap) {
  if (path.isDefinite()) {
    if(resultIndex == 0){
      throw new PathNotFoundException("No results for path: " + path.toString());
    }
    int len = jsonProvider().length(valueResult);
    Object value = (len > 0) ? jsonProvider().getArrayIndex(valueResult, len-1) : null;
    if (value != null && unwrap){
     value = jsonProvider().unwrap(value);
    }
    return (T) value;
  }
  return (T)valueResult;
}

代码示例来源:origin: json-path/JsonPath

@Override
public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
  if (ctx.jsonProvider().isMap(model)) {
    for (String property : ctx.jsonProvider().getPropertyKeys(model)) {
      handleObjectProperty(currentPath, model, ctx, Collections.singletonList(property));
    }
  } else if (ctx.jsonProvider().isArray(model)) {
    for (int idx = 0; idx < ctx.jsonProvider().length(model); idx++) {
      try {
        handleArrayIndex(idx, currentPath, model, ctx);
      } catch (PathNotFoundException p){
        if(ctx.options().contains(Option.REQUIRE_PROPERTIES)){
          throw p;
        }
      }
    }
  }
}

代码示例来源:origin: json-path/JsonPath

public void put(String key, Object value, Configuration configuration){
  Object target = configuration.jsonProvider().getMapValue(parent, property);
  if(targetInvalid(target)){
    return;
  }
  if(configuration.jsonProvider().isMap(target)){
    configuration.jsonProvider().setProperty(target, key, value);
  } else {
    throw new InvalidModificationException("Can only add properties to a map");
  }
}

代码示例来源:origin: json-path/JsonPath

res = path.evaluate(doc, ctx.root(), ctx.configuration()).getValue();
  res = ctx.configuration().jsonProvider().unwrap(res);
  else if (res instanceof Boolean) return ValueNode.createBooleanNode(res.toString());
  else if (res == null) return NULL_NODE;
  else if (ctx.configuration().jsonProvider().isArray(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, List.class, ctx.configuration()));
  else if (ctx.configuration().jsonProvider().isMap(res)) return ValueNode.createJsonNode(ctx.configuration().mappingProvider().map(res, Map.class, ctx.configuration()));
  else throw new JsonPathException("Could not convert " + res.toString() + " to a ValueNode");
} catch (PathNotFoundException e) {

代码示例来源:origin: json-path/JsonPath

@Override
public void convert(MapFunction mapFunction, Configuration configuration) {
  Object currentValue = configuration.jsonProvider().getMapValue(parent, property);
  configuration.jsonProvider().setProperty(parent, property, mapFunction.map(currentValue, configuration));
}

代码示例来源:origin: json-path/JsonPath

@Override
  public Object invoke(String currentPath, PathRef parent, Object model, EvaluationContext ctx, List<Parameter> parameters) {
    if(ctx.configuration().jsonProvider().isArray(model)){
      return ctx.configuration().jsonProvider().length(model);
    } else if(ctx.configuration().jsonProvider().isMap(model)){
      return ctx.configuration().jsonProvider().length(model);
    }
    return null;
  }
}

代码示例来源:origin: json-path/JsonPath

@Override
public <T> T map(final Object source, final Class<T> targetType, final Configuration configuration) {
 if (source == null) {
  return null;
 }
 if (targetType.isAssignableFrom(source.getClass())) {
  return (T) source;
 }
 try {
  if (targetType.isAssignableFrom(ArrayList.class) && configuration.jsonProvider().isArray(source)) {
   int length = configuration.jsonProvider().length(source);
   @SuppressWarnings("rawtypes")
   ArrayList list = new ArrayList(length);
   for (Object o : configuration.jsonProvider().toIterable(source)) {
    list.add(o);
   }
   return (T) list;
  }
 } catch (Exception e) {
 }
 throw new MappingException("Cannot convert a " + source.getClass().getName() + " to a " + targetType
   + " use Tapestry's TypeCoercer instead.");
}

代码示例来源:origin: json-path/JsonPath

public static void walkObject(PathToken pt, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx, Predicate predicate) {
  if (predicate.matches(model)) {
    pt.evaluate(currentPath, parent, model, ctx);
  }
  Collection<String> properties = ctx.jsonProvider().getPropertyKeys(model);
  for (String property : properties) {
    String evalPath = currentPath + "['" + property + "']";
    Object propertyModel = ctx.jsonProvider().getMapValue(model, property);
    if (propertyModel != JsonProvider.UNDEFINED) {
      walk(pt, evalPath, PathRef.create(model, property), propertyModel, ctx, predicate);
    }
  }
}

代码示例来源:origin: json-path/JsonPath

private void sliceFrom(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
  int length = ctx.jsonProvider().length(model);
  int from = operation.from();
  if (from < 0) {
    //calculate slice start from array length
    from = length + from;
  }
  from = Math.max(0, from);
  logger.debug("Slice from index on array with length: {}. From index: {} to: {}. Input: {}", length, from, length - 1, toString());
  if (length == 0 || from >= length) {
    return;
  }
  for (int i = from; i < length; i++) {
    handleArrayIndex(i, currentPath, model, ctx);
  }
}

代码示例来源:origin: json-path/JsonPath

private static Object readObjectProperty(String property, Object model, EvaluationContextImpl ctx) {
  return ctx.jsonProvider().getMapValue(model, property);
}

代码示例来源:origin: json-path/JsonPath

Object res = path.evaluate(jsonObject, jsonObject, configuration).getValue(false);
  if (optAlwaysReturnList && path.isDefinite()) {
    Object array = configuration.jsonProvider().createArray();
    configuration.jsonProvider().setArrayIndex(array, 0, res);
    return (T) array;
  } else {
} else {
  if (optAsPathList) {
    return (T) configuration.jsonProvider().createArray();
  } else {
    if (optAlwaysReturnList) {
      return (T) configuration.jsonProvider().createArray();
    } else {
      return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());

代码示例来源:origin: json-path/JsonPath

protected void handleArrayIndex(int index, String currentPath, Object model, EvaluationContextImpl ctx) {
  String evalPath = Utils.concat(currentPath, "[", String.valueOf(index), "]");
  PathRef pathRef = ctx.forUpdate() ? PathRef.create(model, index) : PathRef.NO_OP;
  int effectiveIndex = index < 0 ? ctx.jsonProvider().length(model) + index : index;
  try {
    Object evalHit = ctx.jsonProvider().getArrayIndex(model, effectiveIndex);
    if (isLeaf()) {
      ctx.addResult(evalPath, pathRef, evalHit);
    } else {
      next().evaluate(evalPath, pathRef, evalHit, ctx);
    }
  } catch (IndexOutOfBoundsException e) {
  }
}

代码示例来源:origin: json-path/JsonPath

/**
 * Verify the function returns the correct result based on the input expectedValue
 *
 * @param pathExpr
 *      The path expression to execute
 *
 * @param json
 *      The json document (actual content) to parse
 *
 * @param expectedValue
 *      The expected value to be returned from the test
 */
protected void verifyFunction(Configuration conf, String pathExpr, String json, Object expectedValue) {
  Object result = using(conf).parse(json).read(pathExpr);
  assertThat(conf.jsonProvider().unwrap(result)).isEqualTo(expectedValue);
}

相关文章