com.fasterxml.jackson.core.JsonParser.getText()方法的使用及代码示例

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

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

JsonParser.getText介绍

[英]Method for accessing textual representation of the current token; if no current token (before first call to #nextToken, or after encountering end-of-input), returns null. Method can be called for any token type.
[中]用于访问当前令牌的文本表示的方法;如果没有当前令牌(在第一次调用#nextToken之前,或在遇到输入结束之后),则返回null。方法可以为任何令牌类型调用。

代码示例

代码示例来源:origin: redisson/redisson

/**
 * Method that fetches next token (as if calling {@link #nextToken}) and
 * if it is {@link JsonToken#VALUE_STRING} returns contained String value;
 * otherwise returns null.
 * It is functionally equivalent to:
 *<pre>
 *  return (nextToken() == JsonToken.VALUE_STRING) ? getText() : null;
 *</pre>
 * but may be faster for parser to process, and can therefore be used if caller
 * expects to get a String value next from input.
 */
public String nextTextValue() throws IOException {
  return (nextToken() == JsonToken.VALUE_STRING) ? getText() : null;
}

代码示例来源:origin: immutables/immutables

private String getTokenString() {
 if (parser.getCurrentToken() != null) {
  try {
   return "'" + parser.getText() + "'";
  } catch (Exception ex) {
   return "?";
  }
 }
 return "";
}

代码示例来源:origin: redisson/redisson

/**
 * Helper method used for accessing String value, if possible, doing
 * necessary conversion or throwing exception as necessary.
 * 
 * @since 2.1
 */
protected final String _parseString(JsonParser p, DeserializationContext ctxt) throws IOException
{
  JsonToken t = p.getCurrentToken();
  if (t == JsonToken.VALUE_STRING) {
    return p.getText();
  }
  // 07-Nov-2016, tatu: Caller should take care of unwrapping and there shouldn't
  //    be need for extra pass here...
  /*
  // [databind#381]
  if ((t == JsonToken.START_ARRAY) && ctxt.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)) {
    p.nextToken();
    final String parsed = _parseString(p, ctxt);
    _verifyEndArrayForSingle(p, ctxt);
    return parsed;            
  }
  */
  String value = p.getValueAsString();
  if (value != null) {
    return value;
  }
  return (String) ctxt.handleUnexpectedToken(String.class, p);
}

代码示例来源:origin: redisson/redisson

/**
 * Helper method that may be used to support fallback for Empty String / Empty Array
 * non-standard representations; usually for things serialized as JSON Objects.
 * 
 * @since 2.5
 */
@SuppressWarnings("unchecked")
protected T _deserializeFromEmpty(JsonParser p, DeserializationContext ctxt)
  throws IOException
{
  JsonToken t = p.getCurrentToken();
  if (t == JsonToken.START_ARRAY) {
    if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT)) {
      t = p.nextToken();
      if (t == JsonToken.END_ARRAY) {
        return null;
      }
      return (T) ctxt.handleUnexpectedToken(handledType(), p);
    }
  } else if (t == JsonToken.VALUE_STRING) {
    if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
      String str = p.getText().trim();
      if (str.isEmpty()) {
        return null;
      }
    }
  }
  return (T) ctxt.handleUnexpectedToken(handledType(), p);
}

代码示例来源:origin: spring-projects/spring-security-oauth

private Set<String> parseScope(JsonParser jp) throws JsonParseException, IOException {
    Set<String> scope;
    if (jp.getCurrentToken() == JsonToken.START_ARRAY) {
      scope = new TreeSet<String>();
      while (jp.nextToken() != JsonToken.END_ARRAY) {
        scope.add(jp.getValueAsString());
      }
    } else {
      String text = jp.getText();
      scope = OAuth2Utils.parseParameterList(text);
    }
    return scope;
  }
}

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

@Override
 public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
 {
  JsonToken t = jp.getCurrentToken();
  if (t == JsonToken.VALUE_NUMBER_INT) {
   return DateTimes.utc(jp.getLongValue());
  }
  if (t == JsonToken.VALUE_STRING) {
   String str = jp.getText().trim();
   if (str.length() == 0) { // [JACKSON-360]
    return null;
   }
   // make sure to preserve time zone information when parsing timestamps
   return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str);
  }
  throw ctxt.mappingException(getValueClass());
 }
}

代码示例来源:origin: redisson/redisson

return p.getText();
  if (ctxt.isEnabled(DeserializationFeature.USE_BIG_INTEGER_FOR_INTS)) {
    return p.getBigIntegerValue();
  if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
    return p.getDecimalValue();
default:
return ctxt.handleUnexpectedToken(Object.class, p);

代码示例来源:origin: com.fasterxml.jackson.datatype/jackson-datatype-jsr310

String string = parser.getText().trim();
if (string.length() == 0) {
  return null;
JsonToken t = parser.nextToken();
if (t == JsonToken.END_ARRAY) {
  return null;
if (context.isEnabled(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
    && (t == JsonToken.VALUE_STRING || t==JsonToken.VALUE_EMBEDDED_OBJECT)) {
  final LocalDate parsed = deserialize(parser, context);
  if (parser.nextToken() != JsonToken.END_ARRAY) {
    handleMissingEndArrayForSingle(parser, context);
  int day = parser.nextIntValue(-1);
  if (parser.nextToken() != JsonToken.END_ARRAY) {
    throw context.wrongTokenException(parser, handledType(), JsonToken.END_ARRAY,
        "Expected array to end");

代码示例来源:origin: kairosdb/kairosdb

JsonToken token = parser.nextToken();
if (token != JsonToken.START_ARRAY )
  throw deserializationContext.mappingException("Invalid data point syntax.");
   parser.nextToken();
  long timestamp = parser.getLongValue();
  parser.nextToken();
  String value = parser.getText();
  token = parser.nextToken();
  if (token != JsonToken.END_ARRAY)
    throw deserializationContext.mappingException("Invalid data point syntax.");
  token = parser.nextToken();

代码示例来源:origin: redisson/redisson

if (ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
    String str = p.getText().trim();
    if (str.isEmpty()) {
      return null;
p = tb.asParser(p);
p.nextToken();

代码示例来源:origin: redisson/redisson

@Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
  JsonToken curr = p.getCurrentToken();
    CompactStringObjectMap lookup = ctxt.isEnabled(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
        ? _getToStringLookup(ctxt) : _lookupByName;
    final String name = p.getText();
    Object result = lookup.find(name);
    if (result == null) {
    if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
      return ctxt.handleWeirdNumberValue(_enumClass(), index,
          "not allowed to deserialize Enum value out of number: disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow"
        && ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)) {
      return _enumDefaultValue;

代码示例来源:origin: bootique/bootique

@Override
  public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException {
    switch (parser.getCurrentTokenId()) {
      case JsonTokenId.ID_NUMBER_FLOAT:
        BigDecimal value = parser.getDecimalValue();
        long seconds = value.longValue();
        int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds);
        return Duration.ofSeconds(seconds, nanoseconds);

      case JsonTokenId.ID_NUMBER_INT:
        return Duration.ofSeconds(parser.getLongValue());

      case JsonTokenId.ID_STRING:
        String string = parser.getText().trim();
        if (string.length() == 0) {
          return null;
        }
        return Duration.parse(string);
    }
    throw context.mappingException("Expected type float, integer, or string.");
  }
}

代码示例来源:origin: redisson/redisson

@SuppressWarnings("unchecked")
protected T handleNonArray(JsonParser p, DeserializationContext ctxt) throws IOException
{
  // Empty String can become null...
  if (p.hasToken(JsonToken.VALUE_STRING)
      && ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) {
    if (p.getText().length() == 0) {
      return null;
    }
  }
  boolean canWrap = (_unwrapSingle == Boolean.TRUE) ||
      ((_unwrapSingle == null) &&
          ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY));
  if (canWrap) {
    return handleSingleElementUnwrapped(p, ctxt);
  }
  return (T) ctxt.handleUnexpectedToken(_valueClass, p);
}

代码示例来源:origin: redisson/redisson

protected void _verifyNumberForScalarCoercion(DeserializationContext ctxt, JsonParser p) throws IOException
{
  MapperFeature feat = MapperFeature.ALLOW_COERCION_OF_SCALARS;
  if (!ctxt.isEnabled(feat)) {
    // 31-Mar-2017, tatu: Since we don't know (or this deep, care) about exact type,
    //   access as a String: may require re-encoding by parser which should be fine
    String valueDesc = p.getText();
    ctxt.reportInputMismatch(this, "Cannot coerce Number (%s) %s (enable `%s.%s` to allow)",
      valueDesc, _coercedTypeDesc(), feat.getClass().getSimpleName(), feat.name());
  }
}

代码示例来源:origin: dreamhead/moco

@Override
  public HttpProtocolVersion deserialize(final JsonParser jp, final DeserializationContext ctx) throws IOException {
    try {
      return HttpProtocolVersion.versionOf(strip(jp.getText()));
    } catch (IllegalArgumentException e) {
      return (HttpProtocolVersion) ctx.handleUnexpectedToken(HttpProtocolVersion.class, jp);
    }
  }
}

代码示例来源:origin: redisson/redisson

protected java.util.Date _parseDate(JsonParser p, DeserializationContext ctxt)
  throws IOException
{
  switch (p.getCurrentTokenId()) {
  case JsonTokenId.ID_STRING:
    return _parseDate(p.getText().trim(), ctxt);
  case JsonTokenId.ID_NUMBER_INT:
    {
      long ts;
      try {
        ts = p.getLongValue();
      } catch (JsonParseException e) {
        Number v = (Number) ctxt.handleWeirdNumberValue(_valueClass, p.getNumberValue(),
            "not a valid 64-bit long for creating `java.util.Date`");
        ts = v.longValue();
      }
      return new java.util.Date(ts);
    }
  case JsonTokenId.ID_NULL:
    return (java.util.Date) getNullValue(ctxt);
  case JsonTokenId.ID_START_ARRAY:
    return _parseDateFromArray(p, ctxt);
  }
  return (java.util.Date) ctxt.handleUnexpectedToken(_valueClass, p);
}

代码示例来源:origin: dreamhead/moco

@Override
public CookieContainer deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException {
  JsonToken currentToken = jp.getCurrentToken();
  if (currentToken == JsonToken.VALUE_STRING) {
    return CookieContainer.newContainer(strip(jp.getText()));
  }
  if (currentToken == JsonToken.START_OBJECT) {
    jp.nextToken();
    InternalCookieContainer container = jp.readValueAs(InternalCookieContainer.class);
    return container.toContainer();
  }
  return (CookieContainer) ctxt.handleUnexpectedToken(CookieContainer.class, jp);
}

代码示例来源:origin: protostuff/protostuff

@Override
public String readString() throws IOException
{
  if (parser.getCurrentToken() != VALUE_STRING)
    throw new JsonInputException("Expected token: string but was " + parser.getCurrentToken());
  final String value = parser.getText();
  if (lastRepeated && parser.nextToken() == END_ARRAY)
    lastRepeated = false;
  return value;
}

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

@Override
 public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
 {
  JsonToken t = jp.getCurrentToken();
  if (t == JsonToken.VALUE_NUMBER_INT) {
   return DateTimes.utc(jp.getLongValue());
  }
  if (t == JsonToken.VALUE_STRING) {
   String str = jp.getText().trim();
   if (str.length() == 0) { // [JACKSON-360]
    return null;
   }
   // make sure to preserve time zone information when parsing timestamps
   return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str);
  }
  throw ctxt.mappingException(getValueClass());
 }
}

代码示例来源:origin: redisson/redisson

case JsonTokenId.ID_START_OBJECT:
    JsonToken t = p.nextToken();
    if (t == JsonToken.END_OBJECT) {
      return new LinkedHashMap<String,Object>(2);
case JsonTokenId.ID_START_ARRAY:
    JsonToken t = p.nextToken();
    if (t == JsonToken.END_ARRAY) { // and empty one too
      if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) {
        return NO_OBJECTS;
  if (ctxt.isEnabled(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY)) {
    return mapArrayToArray(p, ctxt);
  return p.getEmbeddedObject();
case JsonTokenId.ID_STRING:
  return p.getText();
  if (ctxt.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS)) {
    return p.getDecimalValue();
return ctxt.handleUnexpectedToken(Object.class, p);

相关文章

JsonParser类方法