com.jayway.jsonpath.spi.json.JsonProvider.length()方法的使用及代码示例

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

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

JsonProvider.length介绍

[英]Get the length of an json array, json object or a json string
[中]获取json数组、json对象或json字符串的长度

代码示例

代码示例来源: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 void sliceTo(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
  int length = ctx.jsonProvider().length(model);
  if (length == 0) {
    return;
  }
  int to = operation.to();
  if (to < 0) {
    //calculate slice end from array length
    to = length + to;
  }
  to = Math.min(length, to);
  logger.debug("Slice to index on array with length: {}. From index: 0 to: {}. Input: {}", length, to, toString());
  for (int i = 0; i < to; i++) {
    handleArrayIndex(i, currentPath, model, ctx);
  }
}

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

private void sliceBetween(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
  int length = ctx.jsonProvider().length(model);
  int from = operation.from();
  int to = operation.to();
  to = Math.min(length, to);
  if (from >= to || length == 0) {
    return;
  }
  logger.debug("Slice between indexes on array with length: {}. From index: {} to: {}. Input: {}", length, from, to, toString());
  for (int i = from; i < to; i++) {
    handleArrayIndex(i, currentPath, model, ctx);
  }
}

代码示例来源: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

@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

@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

@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

/**
   * Shortcut for counting found nodes.
   * @param json json to be parsed
   * @param path path to be evaluated
   * @param expectedResultCount expected number of nodes to be found
   * @param conf conf to use during evaluation
   */
  public static void assertHasResults(final String json, final String path, final int expectedResultCount, Configuration conf) {
    Object result = JsonPath.using(conf).parse(json).read(path);
    assertThat(conf.jsonProvider().length(result)).isEqualTo(expectedResultCount);
  }
}

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

@Override
public void add(Object newVal, Configuration configuration) {
  if(configuration.jsonProvider().isArray(parent)){
    configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal);
  } else {
    throw new InvalidModificationException("Invalid add operation. $ is not an array");
  }
}

代码示例来源: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

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

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: com.jayway.jsonpath/json-path

public void sliceBetween(ArraySliceOperation operation, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
  int length = ctx.jsonProvider().length(model);
  int from = operation.from();
  int to = operation.to();
  to = Math.min(length, to);
  if (from >= to || length == 0) {
    return;
  }
  logger.debug("Slice between indexes on array with length: {}. From index: {} to: {}. Input: {}", length, from, to, toString());
  for (int i = from; i < to; i++) {
    handleArrayIndex(i, currentPath, model, ctx);
  }
}

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

@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: com.jayway.jsonpath/json-path

public int length(Predicate.PredicateContext ctx) {
  return isArray(ctx) ? ctx.configuration().jsonProvider().length(parse(ctx)) : -1;
}

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

public boolean isEmpty(Predicate.PredicateContext ctx) {
  if (isArray(ctx) || isMap(ctx)) return ctx.configuration().jsonProvider().length(parse(ctx)) == 0;
  else if((parse(ctx) instanceof String)) return ((String)parse(ctx)).length() == 0;
  return true;
}

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

@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: com.jayway.jsonpath/json-path

@Override
public void add(Object newVal, Configuration configuration) {
  if(configuration.jsonProvider().isArray(parent)){
    configuration.jsonProvider().setArrayIndex(parent, configuration.jsonProvider().length(parent), newVal);
  } else {
    throw new InvalidModificationException("Invalid add operation. $ is not an array");
  }
}

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

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: com.jayway.jsonpath/json-path

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");
  }
}

相关文章