Search in sources :

Example 11 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project jackson-databind by FasterXML.

the class TestJacksonTypes method testTokenBuffer.

/**
     * Verify that {@link TokenBuffer} can be properly serialized
     * automatically, using the "standard" JSON sample document
     */
public void testTokenBuffer() throws Exception {
    // First, copy events from known good source (StringReader)
    JsonParser jp = createParserUsingReader(SAMPLE_DOC_JSON_SPEC);
    TokenBuffer tb = new TokenBuffer(null, false);
    while (jp.nextToken() != null) {
        tb.copyCurrentEvent(jp);
    }
    jp.close();
    // Then serialize as String
    String str = serializeAsString(tb);
    tb.close();
    // and verify it looks ok
    verifyJsonSpecSampleDoc(createParserUsingReader(str), true);
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 12 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project java by wavefrontHQ.

the class JsonMetricsGenerator method generateJsonMetrics.

public static JsonNode generateJsonMetrics(MetricsRegistry registry, boolean includeVMMetrics, boolean includeBuildMetrics, boolean clearMetrics, @Nullable Map<String, String> pointTags, @Nullable MetricTranslator metricTranslator) throws IOException {
    TokenBuffer t = new TokenBuffer(new ObjectMapper(), false);
    writeJson(t, registry, includeVMMetrics, includeBuildMetrics, clearMetrics, pointTags, metricTranslator);
    JsonParser parser = t.asParser();
    return parser.readValueAsTree();
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 13 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project jackson-jaxrs-propertyfiltering by HubSpot.

the class PropertyFilteringMessageBodyWriter method valueToTree.

private JsonNode valueToTree(ObjectMapper mapper, ObjectWriter writer, Object o) {
    if (o == null) {
        return null;
    }
    TokenBuffer buf = new TokenBuffer(mapper, false);
    JsonNode result;
    try {
        writer.writeValue(buf, o);
        JsonParser jp = buf.asParser();
        result = mapper.readTree(jp);
        jp.close();
    } catch (IOException e) {
        // should not occur, no real i/o...
        throw new IllegalArgumentException(e.getMessage(), e);
    }
    return result;
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 14 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project drill by apache.

the class DrillValuesRelBase method convertToJsonNode.

private static JsonNode convertToJsonNode(RelDataType rowType, List<? extends List<RexLiteral>> tuples) throws IOException {
    TokenBuffer out = new TokenBuffer(MAPPER.getFactory().getCodec(), false);
    JsonOutput json = new ExtendedJsonOutput(out);
    json.writeStartArray();
    String[] fields = rowType.getFieldNames().toArray(new String[rowType.getFieldCount()]);
    for (List<RexLiteral> row : tuples) {
        json.writeStartObject();
        int i = 0;
        for (RexLiteral field : row) {
            json.writeFieldName(fields[i]);
            writeLiteral(field, json);
            i++;
        }
        json.writeEndObject();
    }
    json.writeEndArray();
    json.flush();
    return out.asParser().readValueAsTree();
}
Also used : RexLiteral(org.apache.calcite.rex.RexLiteral) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) JsonOutput(org.apache.drill.exec.vector.complex.fn.JsonOutput) ExtendedJsonOutput(org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput) TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) NlsString(org.apache.calcite.util.NlsString)

Example 15 with TokenBuffer

use of com.fasterxml.jackson.databind.util.TokenBuffer in project spring-framework by spring-projects.

the class AbstractJackson2Decoder method decode.

@Override
public Flux<Object> decode(Publisher<DataBuffer> input, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) {
    ObjectMapper mapper = selectObjectMapper(elementType, mimeType);
    if (mapper == null) {
        throw new IllegalStateException("No ObjectMapper for " + elementType);
    }
    boolean forceUseOfBigDecimal = mapper.isEnabled(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
    if (BigDecimal.class.equals(elementType.getType())) {
        forceUseOfBigDecimal = true;
    }
    Flux<DataBuffer> processed = processInput(input, elementType, mimeType, hints);
    Flux<TokenBuffer> tokens = Jackson2Tokenizer.tokenize(processed, mapper.getFactory(), mapper, true, forceUseOfBigDecimal, getMaxInMemorySize());
    ObjectReader reader = getObjectReader(mapper, elementType, hints);
    return tokens.handle((tokenBuffer, sink) -> {
        try {
            Object value = reader.readValue(tokenBuffer.asParser(mapper));
            logValue(value, hints);
            if (value != null) {
                sink.next(value);
            }
        } catch (IOException ex) {
            sink.error(processException(ex));
        }
    });
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Aggregations

TokenBuffer (com.fasterxml.jackson.databind.util.TokenBuffer)37 IOException (java.io.IOException)11 JsonParser (com.fasterxml.jackson.core.JsonParser)3 RexLiteral (org.apache.calcite.rex.RexLiteral)3 NlsString (org.apache.calcite.util.NlsString)3 ExtendedJsonOutput (org.apache.drill.exec.vector.complex.fn.ExtendedJsonOutput)3 JsonOutput (org.apache.drill.exec.vector.complex.fn.JsonOutput)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1 SettableBeanProperty (com.fasterxml.jackson.databind.deser.SettableBeanProperty)1 InvalidFormatException (com.fasterxml.jackson.databind.exc.InvalidFormatException)1 UUID (java.util.UUID)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1