下面列出了com.fasterxml.jackson.core.JsonToken#isScalarValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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());
}
}
@Override
public Slice extract(JsonParser jsonParser)
throws IOException
{
JsonToken token = jsonParser.getCurrentToken();
if (token == null) {
throw new JsonParseException(jsonParser, "Unexpected end of value");
}
if (!token.isScalarValue() || token == VALUE_NULL) {
return null;
}
return utf8Slice(jsonParser.getText());
}
private void processTokenNormal(JsonToken token, List<TokenBuffer> result) throws IOException {
this.tokenBuffer.copyCurrentEvent(this.parser);
if ((token.isStructEnd() || token.isScalarValue()) && this.objectDepth == 0 && this.arrayDepth == 0) {
result.add(this.tokenBuffer);
this.tokenBuffer = new TokenBuffer(this.parser, this.deserializationContext);
}
}
private void processTokenArray(JsonToken token, List<TokenBuffer> result) throws IOException {
if (!isTopLevelArrayToken(token)) {
this.tokenBuffer.copyCurrentEvent(this.parser);
}
if (this.objectDepth == 0 && (this.arrayDepth == 0 || this.arrayDepth == 1) &&
(token == JsonToken.END_OBJECT || token.isScalarValue())) {
result.add(this.tokenBuffer);
this.tokenBuffer = new TokenBuffer(this.parser, this.deserializationContext);
}
}
private void processTokenNormal(JsonToken token, List<TokenBuffer> result) throws IOException {
this.tokenBuffer.copyCurrentEvent(this.parser);
if ((token.isStructEnd() || token.isScalarValue()) &&
this.objectDepth == 0 && this.arrayDepth == 0) {
result.add(this.tokenBuffer);
this.tokenBuffer = new TokenBuffer(this.parser);
}
}
private void processTokenArray(JsonToken token, List<TokenBuffer> result) throws IOException {
if (!isTopLevelArrayToken(token)) {
this.tokenBuffer.copyCurrentEvent(this.parser);
}
if (this.objectDepth == 0 &&
(this.arrayDepth == 0 || this.arrayDepth == 1) &&
(token == JsonToken.END_OBJECT || token.isScalarValue())) {
result.add(this.tokenBuffer);
this.tokenBuffer = new TokenBuffer(this.parser);
}
}
@Override
public Set<String> deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
JsonProcessingException {
JsonToken token = jp.getCurrentToken();
if (token.isScalarValue()) {
String list = jp.getText();
list = list.replaceAll("\\s+", ",");
return new LinkedHashSet<String>(Arrays.asList(StringUtils.commaDelimitedListToStringArray(list)));
}
return jp.readValueAs(new TypeReference<Set<String>>() {
});
}
@Override
public Object deserializeWithType(JsonParser p, DeserializationContext context,
TypeDeserializer deserializer)
throws IOException
{
// This is a nasty kludge right here, working around issues like
// [datatype-jsr310#24]. But should work better than not having the work-around.
JsonToken t = p.currentToken();
if ((t != null) && t.isScalarValue()) {
return deserialize(p, context);
}
return deserializer.deserializeTypedFromAny(p, context);
}
public String filter(final String body) {
try {
final JsonParser parser = factory.createParser(body);
final CharArrayWriter writer = new CharArrayWriter(body.length() * 2); // rough estimate of final size
final JsonGenerator generator = factory.createGenerator(writer);
try {
while(true) {
JsonToken nextToken = parser.nextToken();
if(nextToken == null) {
break;
}
generator.copyCurrentEvent(parser);
if(nextToken == JsonToken.FIELD_NAME && fields.contains(parser.getCurrentName())) {
nextToken = parser.nextToken();
generator.writeString(replacement);
if(!nextToken.isScalarValue()) {
parser.skipChildren(); // skip children
}
}
}
} finally {
parser.close();
generator.close();
}
return writer.toString();
} catch(final Exception e) {
log.trace("Unable to filter body for fields {}, compacting result. `{}`", fields, e.getMessage());
return fallbackCompactor.compact(body);
}
}
public static void jsonStreamToRecords(Set<String> indexes, JsonParser jp, String path, Consumer<JsonRecord> consumer) throws IOException {
boolean inArray = false;
int arrayIndex = 0;
while (true) {
JsonToken nextToken = jp.nextToken();
String currentPath = path;
if (nextToken == FIELD_NAME) {
if (inArray) {
currentPath = path + toArrayIndexPath(arrayIndex) + "/";
}
jsonStreamToRecords(indexes, jp, currentPath + validateKey(jp.getCurrentName()) + "/", consumer);
} else if (nextToken == VALUE_NULL) {
if (inArray) {
currentPath = path + toArrayIndexPath(arrayIndex) + "/";
}
consumer.accept(JsonRecord.of(currentPath, String.valueOf(NULL_VALUE_PREFIX), "null", indexFieldValue(indexes, currentPath)));
if( inArray ) {
arrayIndex++;
} else {
return;
}
} else if (nextToken.isScalarValue()) {
if (inArray) {
currentPath = path + toArrayIndexPath(arrayIndex) + "/";
}
String value = jp.getValueAsString();
String ovalue = null;
if( nextToken == JsonToken.VALUE_STRING ) {
value = STRING_VALUE_PREFIX + value; //NOPMD
} else if( nextToken == JsonToken.VALUE_NUMBER_INT || nextToken == JsonToken.VALUE_NUMBER_FLOAT ) {
ovalue = value; // hold on to the original number in th ovalue field.
value = toLexSortableString(value); // encode it so we can lexically sort.
} else if( nextToken == JsonToken.VALUE_TRUE ) {
ovalue = value;
value = String.valueOf(TRUE_VALUE_PREFIX);
} else if( nextToken == JsonToken.VALUE_FALSE ) {
ovalue = value;
value = String.valueOf(FALSE_VALUE_PREFIX);
}
consumer.accept(JsonRecord.of(currentPath, value, ovalue, indexFieldValue(indexes, currentPath)));
if( inArray ) {
arrayIndex++;
} else {
return;
}
} else if (nextToken == END_OBJECT) {
if( inArray ) {
arrayIndex++;
} else {
return;
}
} else if (nextToken == START_ARRAY) {
inArray = true;
} else if (nextToken == END_ARRAY) {
return;
}
}
}