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

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

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

JsonProvider.isMap介绍

[英]checks if object is a map (i.e. no array)
[中]检查对象是否为映射(即无数组)

代码示例

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

@Override
public void put(String key, Object newVal, Configuration configuration) {
  if(configuration.jsonProvider().isMap(parent)){
    configuration.jsonProvider().setProperty(parent, key, newVal);
  } else {
    throw new InvalidModificationException("Invalid put operation. $ is not a map");
  }
}

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

public static void walk(PathToken pt, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx, Predicate predicate) {
  if (ctx.jsonProvider().isMap(model)) {
    walkObject(pt, currentPath, parent, model, ctx, predicate);
  } else if (ctx.jsonProvider().isArray(model)) {
    walkArray(pt, currentPath, parent, model, ctx, predicate);
  }
}

代码示例来源: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: json-path/JsonPath

@Override
public void evaluate(String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx) {
  // Can't assert it in ctor because isLeaf() could be changed later on.
  assert onlyOneIsTrueNonThrow(singlePropertyCase(), multiPropertyMergeCase(), multiPropertyIterationCase());
  if (!ctx.jsonProvider().isMap(model)) {
    if (! isUpstreamDefinite()) {
      return;
    } else {
      String m = model == null ? "null" : model.getClass().getName();
      throw new PathNotFoundException(String.format(
          "Expected to find an object with property %s in path %s but found '%s'. " +
          "This is not a json object according to the JsonProvider: '%s'.",
          getPathFragment(), currentPath, m, ctx.configuration().jsonProvider().getClass().getName()));
    }
  }
  if (singlePropertyCase() || multiPropertyMergeCase()) {
    handleObjectProperty(currentPath, model, ctx, properties);
    return;
  }
  assert multiPropertyIterationCase();
  final List<String> currentlyHandledProperty = new ArrayList<String>(1);
  currentlyHandledProperty.add(null);
  for (final String property : properties) {
    currentlyHandledProperty.set(0, property);
    handleObjectProperty(currentPath, model, ctx, currentlyHandledProperty);
  }
}

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

@Override
public boolean matches(Object model) {
  if (! ctx.jsonProvider().isMap(model)) {
    return false;

代码示例来源: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().getArrayIndex(parent, index);
  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

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

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 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 void evaluate(String currentPath, PathRef ref, Object model, EvaluationContextImpl ctx) {
  if (ctx.jsonProvider().isMap(model)) {
    if (accept(model, ctx.rootDocument(), ctx.configuration(), ctx)) {
      PathRef op = ctx.forUpdate() ? ref : PathRef.NO_OP;
      if (isLeaf()) {
        ctx.addResult(currentPath, op, model);
      } else {
        next().evaluate(currentPath, op, model, ctx);
      }
    }
  } else if (ctx.jsonProvider().isArray(model)){
    int idx = 0;
    Iterable<?> objects = ctx.jsonProvider().toIterable(model);
    for (Object idxModel : objects) {
      if (accept(idxModel, ctx.rootDocument(),  ctx.configuration(), ctx)) {
        handleArrayIndex(idx, currentPath, model, ctx);
      }
      idx++;
    }
  } else {
    if (isUpstreamDefinite()) {
      throw new InvalidPathException(format("Filter: %s can not be applied to primitives. Current context is: %s", toString(), model));
    }
  }
}

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

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

public boolean isMap(Predicate.PredicateContext ctx) {
  return ctx.configuration().jsonProvider().isMap(parse(ctx));
}

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

@Override
public void put(String key, Object newVal, Configuration configuration) {
  if(configuration.jsonProvider().isMap(parent)){
    configuration.jsonProvider().setProperty(parent, key, newVal);
  } else {
    throw new InvalidModificationException("Invalid put operation. $ is not a map");
  }
}

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

public static void walk(PathToken pt, String currentPath, PathRef parent, Object model, EvaluationContextImpl ctx, Predicate predicate) {
  if (ctx.jsonProvider().isMap(model)) {
    walkObject(pt, currentPath, parent, model, ctx, predicate);
  } else if (ctx.jsonProvider().isArray(model)) {
    walkArray(pt, currentPath, parent, model, ctx, predicate);
  }
}

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

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

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

public void put(String key, Object value, Configuration configuration){
  Object target = configuration.jsonProvider().getArrayIndex(parent, index);
  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: 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

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

相关文章