本文整理匯總了Java中com.fasterxml.jackson.core.JsonParser.getTextLength方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonParser.getTextLength方法的具體用法?Java JsonParser.getTextLength怎麽用?Java JsonParser.getTextLength使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.fasterxml.jackson.core.JsonParser
的用法示例。
在下文中一共展示了JsonParser.getTextLength方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: parseInt32
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
/** Parsers an int32 value out of the input. */
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());
}
}
示例2: parseDouble
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
/** Parsers a double value out of the input. */
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());
}
}