本文整理匯總了Java中com.fasterxml.jackson.core.JsonParser.currentToken方法的典型用法代碼示例。如果您正苦於以下問題:Java JsonParser.currentToken方法的具體用法?Java JsonParser.currentToken怎麽用?Java JsonParser.currentToken使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.fasterxml.jackson.core.JsonParser
的用法示例。
在下文中一共展示了JsonParser.currentToken方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: readArray
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
@Nullable
public static List<Object> readArray(JsonParser jp) throws IOException {
if (jp.currentToken() != JsonToken.START_ARRAY) {
return null;
}
List<Object> result = new ArrayList<>();
JsonToken token = jp.nextToken();
while (token != JsonToken.END_ARRAY) {
if (token == JsonToken.START_ARRAY) {
result.add(readArray(jp));
}
else if (token == JsonToken.START_OBJECT) {
result.add(readObject(jp));
}
else {
result.add(getValue(jp));
}
token = jp.nextToken();
}
return result;
}
示例3: doMerge
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
@Override
public void doMerge(JsonParser parser, int currentDepth, Message.Builder messageBuilder)
throws IOException {
Value.Builder builder = (Value.Builder) messageBuilder;
JsonToken token = parser.currentToken();
if (token.isBoolean()) {
builder.setBoolValue(ParseSupport.parseBool(parser));
} else if (token.isNumeric()) {
builder.setNumberValue(ParseSupport.parseDouble(parser));
} else if (token == JsonToken.VALUE_NULL) {
builder.setNullValue(NullValue.NULL_VALUE);
} else if (token.isScalarValue()) {
builder.setStringValue(ParseSupport.parseString(parser));
} else if (token == JsonToken.START_OBJECT) {
Struct.Builder structBuilder = builder.getStructValueBuilder();
StructMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, structBuilder);
} else if (token == JsonToken.START_ARRAY) {
ListValue.Builder listValueBuilder = builder.getListValueBuilder();
ListValueMarshaller.INSTANCE.mergeValue(parser, currentDepth + 1, listValueBuilder);
} else {
throw new IllegalStateException("Unexpected json data: " + parser.getText());
}
}
示例4: parseBytes
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
/** Parsers a bytes value out of the input. */
static ByteString parseBytes(JsonParser parser) throws IOException {
JsonToken json = parser.currentToken();
byte[] result = null;
try {
// Use Guava to decode base64, which can handle more variants than Jackson.
result =
BaseEncoding.base64()
.decode(
CharBuffer.wrap(
parser.getTextCharacters(), parser.getTextOffset(), parser.getTextLength()));
} catch (IllegalArgumentException e) {
// Fall through
}
if (result == null) {
throw new InvalidProtocolBufferException("Not a bytes value: " + json);
}
return ByteString.copyFrom(result);
}
示例5: readObject
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
@Nullable
public static Map<String, Object> readObject(JsonParser jp) throws IOException {
if (jp.currentToken() != JsonToken.START_OBJECT) {
return null;
}
Map<String, Object> result = new HashMap<>();
JsonToken token = jp.nextToken();
while (token != JsonToken.END_OBJECT) {
String key = jp.getText();
token = jp.nextToken();
if (token == JsonToken.START_ARRAY) {
result.put(key, readArray(jp));
}
else if (token == JsonToken.START_OBJECT) {
result.put(key, readObject(jp));
}
else {
result.put(key, getValue(jp));
}
token = jp.nextToken();
}
return result;
}
示例6: getValue
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
@Nullable
private static Object getValue(JsonParser jp) throws IOException {
switch (jp.currentToken()) {
case VALUE_FALSE:
case VALUE_TRUE:
return jp.getBooleanValue();
case VALUE_STRING:
return jp.getValueAsString();
case VALUE_NUMBER_INT:
switch (jp.getNumberType()) {
case INT:
return jp.getIntValue();
case LONG:
return jp.getLongValue();
case BIG_INTEGER:
return jp.getBigIntegerValue();
default:
return null;
}
case VALUE_NUMBER_FLOAT:
switch (jp.getNumberType()) {
case FLOAT:
return jp.getFloatValue();
case DOUBLE:
return jp.getDoubleValue();
case BIG_DECIMAL:
return jp.getDecimalValue();
default:
return null;
}
default:
return null;
}
}
示例7: processSegment
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
protected T processSegment(JsonParser jp,
ISegmentAdapter<IBaseSegmentDTO, T> adapter) throws IOException {
if(jp.currentToken() != JsonToken.START_OBJECT) {
jp.nextValue();
}
IBaseSegmentDTO dto = jp.readValueAs(adapter.getDtoClass());
return adapter.adaptReverse(dto);
}
示例8: processSegment
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
protected IBaseSegmentDTO processSegment(JsonParser jp,
ISegmentAdapter<IBaseSegmentDTO, T> adapter) throws IOException {
if(jp.currentToken() != JsonToken.START_OBJECT) {
jp.nextValue();
}
IBaseSegmentDTO dto = jp.readValueAs(adapter.getDtoClass());
return dto;
}
示例9: mergeValue
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
void mergeValue(JsonParser parser, int currentDepth, Message.Builder builder) throws IOException {
ParseSupport.checkRecursionLimit(currentDepth);
JsonToken json = parser.currentToken();
if (json == null) {
// Nested messages will already have current token set, but top-level ones will not.
json = parser.nextToken();
}
if (json != JsonToken.START_OBJECT) {
throw new InvalidProtocolBufferException(
"Expected start of object, got: " + parser.getText());
}
doMerge(parser, currentDepth, builder);
// Object end will be handled in ParseSupport.checkObjectEnd.
}
示例10: parseObjectStart
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
/**
* Checks the current token is '{' and advances past it. For parsing the beginning of JSON objects
* ({@link Message}s and {@link java.util.Map}s).
*/
static void parseObjectStart(JsonParser parser) throws IOException {
JsonToken json = parser.currentToken();
if (json != JsonToken.START_OBJECT) {
throw new InvalidProtocolBufferException("Expected an object but found: " + json);
}
parser.nextToken();
}
示例11: parseLong
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
/** Parses a long out of the input, using the optimized path when the value is not quoted. */
private static long parseLong(JsonParser parser) throws IOException {
if (parser.currentToken() == JsonToken.VALUE_NUMBER_INT) {
return parser.getLongValue();
}
return NumberInput.parseLong(parser.getText());
}
示例12: parseBool
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
/** Parsers a bool value out of the input. */
static boolean parseBool(JsonParser parser) throws IOException {
JsonToken token = parser.currentToken();
if (token.isBoolean()) {
return parser.getBooleanValue();
}
String json = parser.getText();
if (json.equals("true")) {
return true;
} else if (json.equals("false")) {
return false;
}
throw new InvalidProtocolBufferException("Invalid bool value: " + json);
}
示例13: 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());
}
}
示例14: parseString
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
/** Parsers a string value out of the input. */
static String parseString(JsonParser parser) throws IOException {
JsonToken json = parser.currentToken();
String result = null;
try {
result = parser.getValueAsString();
} catch (IOException e) {
// Fall through
}
if (result == null) {
throw new InvalidProtocolBufferException("Not a string value: " + json);
}
return result;
}
示例15: read
import com.fasterxml.jackson.core.JsonParser; //導入方法依賴的package包/類
public static GuildInfo read(JsonParser json) throws IOException {
if (json.nextToken() != JsonToken.START_OBJECT)
throw new JsonParseException(json, "Expected to start a new object");
List<UUID> members = null;
String name = null;
while (json.nextToken() != JsonToken.END_OBJECT) {
String field = json.getCurrentName();
if (field.equals("name")) {
if (json.nextToken() != JsonToken.VALUE_STRING)
throw new JsonParseException(json, "Field 'name' was expected to be a string");
name = json.getValueAsString();
continue;
}
if (field.equals("members")) {
if (json.nextToken() != JsonToken.START_ARRAY)
throw new JsonParseException(json, "Field 'members' was expected to be an array");
List<UUID> newMembers = new ArrayList<>();
while (json.nextToken() != JsonToken.END_ARRAY) {
if (json.currentToken() != JsonToken.START_OBJECT)
throw new JsonParseException(json, "Expected to start a new member object");
while (json.nextToken() != JsonToken.END_OBJECT) {
String field2 = json.getCurrentName();
if (field2.equals("uuid")) {
if (json.nextToken() != JsonToken.VALUE_STRING)
throw new JsonParseException(json, "Field 'uuid' of a member was expected to be a string");
newMembers.add(PlayerInfo.idFromString(json.getValueAsString()));
continue;
}
json.nextToken();
json.skipChildren();
}
}
members = Collections.unmodifiableList(newMembers);
continue;
}
json.nextToken();
json.skipChildren();
}
if (members == null)
throw new IllegalArgumentException("Field 'members' is missing");
if (name == null)
throw new IllegalArgumentException("Field 'name' is missing");
return new GuildInfo(members, name);
}