com.fasterxml.jackson.databind.JsonNode.canConvertToLong()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(141)

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

JsonNode.canConvertToLong介绍

[英]Method that can be used to check whether this node is a numeric node ( #isNumber would return true) AND its value fits within Java's 64-bit signed integer type, long. Note that floating-point numbers are convertible if the integral part fits without overflow (as per standard Java coercion rules)

NOTE: this method does not consider possible value type conversion from JSON String into Number; so even if this method returns false, it is possible that #asLong could still succeed if node is a JSON String representing integral number, or boolean.
[中]方法,该方法可用于检查此节点是否为数字节点(#isNumber将返回true),其值是否符合Java的64位带符号整数类型long。请注意,如果整数部分适合而没有溢出,则浮点数是可转换的(根据标准Java强制规则)
注意:此方法不考虑从JSON字符串转换为数字的可能值类型;所以,即使此方法返回false,如果节点是表示整数或布尔值的JSON字符串,则#asLong仍可能成功。

代码示例

代码示例来源:origin: auth0/java-jwt

@Override
public Date asDate() {
  if (!data.canConvertToLong()) {
    return null;
  }
  long seconds = data.asLong();
  return new Date(seconds * 1000);
}

代码示例来源:origin: auth0/java-jwt

Date getDateFromSeconds(Map<String, JsonNode> tree, String claimName) {
  JsonNode node = tree.get(claimName);
  if (node == null || node.isNull()) {
    return null;
  }
  if (!node.canConvertToLong()) {
    throw new JWTDecodeException(String.format("The claim '%s' contained a non-numeric date value.", claimName));
  }
  final long ms = node.asLong() * 1000;
  return new Date(ms);
}

代码示例来源:origin: java-json-tools/json-schema-validator

/**
   * Test whether a numeric instance is a long
   *
   * <p>We use both a test on the instance type and Jackson's {@link
   * JsonNode#canConvertToLong()}. The first test is needed since the
   * latter method will also return true if the value is a decimal which
   * integral part fits into a long, and we don't want that.</p>
   *
   * @param node the node to test
   * @return true if both conditions are true
   */
  private static boolean valueIsLong(final JsonNode node)
  {
    return NodeType.getNodeType(node) == NodeType.INTEGER
      && node.canConvertToLong();
  }
}

代码示例来源:origin: apache/incubator-druid

@Override
 public Object apply(JsonNode node)
 {
  if (node == null || node.isMissingNode() || node.isNull()) {
   return null;
  }
  if (node.isIntegralNumber()) {
   if (node.canConvertToLong()) {
    return node.asLong();
   } else {
    return node.asDouble();
   }
  }
  if (node.isFloatingPointNumber()) {
   return node.asDouble();
  }
  final String s = node.asText();
  final CharsetEncoder enc = StandardCharsets.UTF_8.newEncoder();
  if (s != null && !enc.canEncode(s)) {
   // Some whacky characters are in this string (e.g. \uD900). These are problematic because they are decodeable
   // by new String(...) but will not encode into the same character. This dance here will replace these
   // characters with something more sane.
   return StringUtils.fromUtf8(StringUtils.toUtf8(s));
  } else {
   return s;
  }
 }
};

代码示例来源:origin: java-json-tools/json-schema-validator

private static boolean valueIsLong(final JsonNode node)
{
  if (!node.canConvertToLong())
    return false;
  if (NodeType.getNodeType(node) == NodeType.INTEGER)
    return true;
  return node.decimalValue().remainder(BigDecimal.ONE)
    .compareTo(BigDecimal.ZERO) == 0;
}

代码示例来源:origin: com.auth0/java-jwt

@Override
public Date asDate() {
  if (!data.canConvertToLong()) {
    return null;
  }
  long seconds = data.asLong();
  return new Date(seconds * 1000);
}

代码示例来源:origin: com.auth0/java-jwt

Date getDateFromSeconds(Map<String, JsonNode> tree, String claimName) {
  JsonNode node = tree.get(claimName);
  if (node == null || node.isNull()) {
    return null;
  }
  if (!node.canConvertToLong()) {
    throw new JWTDecodeException(String.format("The claim '%s' contained a non-numeric date value.", claimName));
  }
  final long ms = node.asLong() * 1000;
  return new Date(ms);
}

代码示例来源:origin: ws.wamp.jawampa/jawampa-core

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 3
        || !messageNode.get(1).canConvertToLong()
        || !messageNode.get(2).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    long subscriptionId = messageNode.get(2).asLong();
    return new SubscribedMessage(requestId, subscriptionId);
  }
}

代码示例来源:origin: Matthias247/jawampa

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 3
        || !messageNode.get(1).canConvertToLong()
        || !messageNode.get(2).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    long subscriptionId = messageNode.get(2).asLong();
    return new SubscribedMessage(requestId, subscriptionId);
  }
}

代码示例来源:origin: Matthias247/jawampa

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 3
        || !messageNode.get(1).canConvertToLong()
        || !messageNode.get(2).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    long subscriptionId = messageNode.get(2).asLong();
    return new UnsubscribeMessage(requestId, subscriptionId);
  }
}

代码示例来源:origin: greenaddress/GreenBits

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 3
        || !messageNode.get(1).canConvertToLong()
        || !messageNode.get(2).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    long subscriptionId = messageNode.get(2).asLong();
    return new UnsubscribeMessage(requestId, subscriptionId);
  }
}

代码示例来源:origin: ws.wamp.jawampa/jawampa-core

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode)
      throws WampError {
    if (messageNode.size() != 3
        || !messageNode.get(1).canConvertToLong()
        || !messageNode.get(2).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    long registrationId = messageNode.get(2).asLong();
    return new UnregisterMessage(requestId, registrationId);
  }
}

代码示例来源:origin: ws.wamp.jawampa/jawampa-core

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 3
        || !messageNode.get(1).canConvertToLong()
        || !messageNode.get(2).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    long subscriptionId = messageNode.get(2).asLong();
    return new UnsubscribeMessage(requestId, subscriptionId);
  }
}

代码示例来源:origin: Matthias247/jawampa

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 2
        || !messageNode.get(1).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    return new UnsubscribedMessage(requestId);
  }
}

代码示例来源:origin: greenaddress/GreenBits

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 2
        || !messageNode.get(1).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    return new UnsubscribedMessage(requestId);
  }
}

代码示例来源:origin: ws.wamp.jawampa/jawampa-core

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 2
        || !messageNode.get(1).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    return new UnregisteredMessage(requestId);
  }
}

代码示例来源:origin: ws.wamp.jawampa/jawampa-core

@Override
  public WampMessage fromObjectArray(ArrayNode messageNode) throws WampError {
    if (messageNode.size() != 2
        || !messageNode.get(1).canConvertToLong())
      throw new WampError(ApplicationError.INVALID_MESSAGE);
    long requestId = messageNode.get(1).asLong();
    return new UnsubscribedMessage(requestId);
  }
}

代码示例来源:origin: org.apache.beam/beam-sdks-java-core

/**
 * Extracts long value from JsonNode if it is within bounds.
 *
 * <p>Throws {@link UnsupportedRowJsonException} if value is out of bounds.
 */
static ValueExtractor<Long> longValueExtractor() {
 return ValidatingValueExtractor.<Long>builder()
   .setExtractor(JsonNode::longValue)
   .setValidator(jsonNode -> jsonNode.isIntegralNumber() && jsonNode.canConvertToLong())
   .build();
}

代码示例来源:origin: com.atlassian.oai/swagger-request-validator-core

@Override
  public void validate(final ProcessingReport report,
             final MessageBundle bundle,
             final FullData data) throws ProcessingException {
    final JsonNode instance = data.getInstance().getNode();

    if (!instance.canConvertToLong()) {
      report.warn(newMsg(data, bundle, "warn.format.int64.overflow")
          .put("key", "warn.format.int64.overflow")
          .putArgument("value", instance));
    }
  }
}

代码示例来源:origin: com.github.bjansen/swagger-schema-validator

@Override
  public void validate(final ProcessingReport report,
             final MessageBundle bundle,
             final FullData data) throws ProcessingException {
    final JsonNode instance = data.getInstance().getNode();

    if (!instance.canConvertToLong()) {
      report.warn(newMsg(data, bundle, "warn.format.int64.overflow")
        .put("key", "warn.format.int64.overflow")
        .putArgument("value", instance));
    }
  }
}

相关文章