Java Code Examples for com.fasterxml.jackson.core.JsonToken#isScalarValue()

The following examples show how to use com.fasterxml.jackson.core.JsonToken#isScalarValue() . 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: WellKnownTypeMarshaller.java    From curiostack with MIT License 6 votes vote down vote up
@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());
  }
}
 
Example 2
Source File: JsonExtract.java    From presto with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 3
Source File: Jackson2Tokenizer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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);
	}

}
 
Example 4
Source File: Jackson2Tokenizer.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 5
Source File: Jackson2Tokenizer.java    From java-technology-stack with MIT License 5 votes vote down vote up
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);
	}

}
 
Example 6
Source File: Jackson2Tokenizer.java    From java-technology-stack with MIT License 5 votes vote down vote up
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);
	}
}
 
Example 7
Source File: Jackson2ArrayOrStringDeserializer.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
@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>>() {
	});
}
 
Example 8
Source File: JSR310StringParsableDeserializer.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 9
Source File: JacksonJsonFieldBodyFilter.java    From logbook with MIT License 5 votes vote down vote up
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);
    }
}
 
Example 10
Source File: JsonRecordSupport.java    From syndesis with Apache License 2.0 4 votes vote down vote up
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;
        }
    }
}