Java Code Examples for com.fasterxml.jackson.core.JsonParser#getTextCharacters()

The following examples show how to use com.fasterxml.jackson.core.JsonParser#getTextCharacters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ParseSupport.java    From curiostack with MIT License 6 votes vote down vote up
/** Parsers an int32 value out of the input. */
public static int parseInt32(JsonParser parser) throws IOException {
  JsonToken token = parser.currentToken();
  if (token == JsonToken.VALUE_NUMBER_INT) {
    // Use optimized code path for integral primitives, the normal case.
    return parser.getIntValue();
  }
  // JSON doesn't distinguish between integer values and floating point values so "1" and
  // "1.000" are treated as equal in JSON. For this reason we accept floating point values for
  // integer fields as well as long as it actually is an integer (i.e., round(value) == value).
  try {
    BigDecimal value =
        new BigDecimal(
            parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
    return value.intValueExact();
  } catch (Exception e) {
    throw new InvalidProtocolBufferException("Not an int32 value: " + parser.getText());
  }
}
 
Example 2
Source File: ParseSupport.java    From curiostack with MIT License 5 votes vote down vote up
/** Parsers a double value out of the input. */
public static double parseDouble(JsonParser parser) throws IOException {
  JsonToken current = parser.currentToken();
  if (!current.isNumeric()) {
    String json = parser.getText();
    if (json.equals("NaN")) {
      return Double.NaN;
    } else if (json.equals("Infinity")) {
      return Double.POSITIVE_INFINITY;
    } else if (json.equals("-Infinity")) {
      return Double.NEGATIVE_INFINITY;
    }
  }
  try {
    // We don't use Double.parseDouble() here because that function simply
    // accepts all values. Here we readValue the value into a BigDecimal and do
    // explicit range check on it.
    BigDecimal value =
        new BigDecimal(
            parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength());
    if (value.compareTo(MAX_DOUBLE) > 0 || value.compareTo(MIN_DOUBLE) < 0) {
      throw new InvalidProtocolBufferException("Out of range double value: " + parser.getText());
    }
    return value.doubleValue();
  } catch (NumberFormatException e) {
    throw new InvalidProtocolBufferException("Not an double value: " + parser.getText());
  }
}
 
Example 3
Source File: BaseCharCollectionDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 5 votes vote down vote up
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException {
    Intermediate intermediate = createIntermediate();

    if (p.isExpectedStartArrayToken()) {
        JsonToken t;
        while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
            String str;
            if (t == JsonToken.VALUE_STRING) {
                str = p.getText();
            } else {
                CharSequence cs =
                        (CharSequence) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
                str = cs.toString();
            }
            if (str.length() != 1) {
                ctxt.reportInputMismatch(this,
                        "Cannot convert a JSON String of length %d into a char element of " +
                                "char array",
                        str.length());
            }
            add(intermediate, str.charAt(0));
        }
        return finish(intermediate);
    }

    char[] chars = p.getTextCharacters();
    addAll(intermediate, chars, p.getTextOffset(), p.getTextLength());
    return finish(intermediate);
}
 
Example 4
Source File: MincodeParserTest.java    From divolte-collector with Apache License 2.0 5 votes vote down vote up
private static void assertJsonParserText(final JsonParser parser,
                                         final String expectedText) throws IOException {
    assertEquals(expectedText, parser.getText());
    final char[] buffer = parser.getTextCharacters();
    assertNotNull(buffer);
    assertEquals(expectedText, new String(buffer, parser.getTextOffset(), parser.getTextLength()));
}
 
Example 5
Source File: BaseCollectionDeserializer.java    From jackson-datatypes-collections with Apache License 2.0 4 votes vote down vote up
@Override
public T deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException {
    Intermediate intermediate = createIntermediate();

    if (p.isExpectedStartArrayToken()) {
        JsonToken t;
        while ((t = p.nextToken()) != JsonToken.END_ARRAY) {
            String str;
            if (t == JsonToken.VALUE_STRING) {
                str = p.getText();
            } else {
                CharSequence cs = (CharSequence) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
                str = cs.toString();
            }
            if (str.length() != 1) {
                ctxt.reportInputMismatch(this,
                                         "Cannot convert a JSON String of length %d into a char element of " +
                                         "char array",
                                         str.length());
            }
            intermediate.add(str.charAt(0));
        }
        return finish(intermediate);
    }

    char[] chars = p.getTextCharacters();
    if (p.getTextOffset() == 0 && p.getTextLength() == chars.length) {
        intermediate.addAll(chars);
    } else {
        int i = 0;
        // first, copy in batches of BATCH_COPY_SIZE
        if ((p.getTextLength() - i) >= BATCH_COPY_SIZE) {
            char[] buf = new char[BATCH_COPY_SIZE];
            do {
                System.arraycopy(chars, p.getTextOffset() + i, buf, 0, BATCH_COPY_SIZE);
                intermediate.addAll(buf);
                i += BATCH_COPY_SIZE;
            } while ((p.getTextLength() - i) >= BATCH_COPY_SIZE);
        }
        // and finally, copy the remainder.
        if (p.getTextLength() > i) {
            char[] tail = Arrays.copyOfRange(
                    chars, p.getTextOffset() + i, p.getTextOffset() + p.getTextLength());
            intermediate.addAll(tail);
        }
    }
    return finish(intermediate);
}