Search in sources :

Example 31 with TokenBuffer

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

the class TestJacksonTypes method testTokenBufferWithSequence.

@SuppressWarnings("resource")
public void testTokenBufferWithSequence() throws Exception {
    ObjectMapper m = new ObjectMapper();
    // and then sequence of other things
    JsonParser jp = createParserUsingReader("[ 32, [ 1 ], \"abc\", { \"a\" : true } ]");
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    TokenBuffer buf = m.readValue(jp, TokenBuffer.class);
    // check manually...
    JsonParser bufParser = buf.asParser();
    assertToken(JsonToken.VALUE_NUMBER_INT, bufParser.nextToken());
    assertEquals(32, bufParser.getIntValue());
    assertNull(bufParser.nextToken());
    // then bind to another
    buf = m.readValue(jp, TokenBuffer.class);
    bufParser = buf.asParser();
    assertToken(JsonToken.START_ARRAY, bufParser.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, bufParser.nextToken());
    assertEquals(1, bufParser.getIntValue());
    assertToken(JsonToken.END_ARRAY, bufParser.nextToken());
    assertNull(bufParser.nextToken());
    // third one, with automatic binding
    buf = m.readValue(jp, TokenBuffer.class);
    String str = m.readValue(buf.asParser(), String.class);
    assertEquals("abc", str);
    // and ditto for last one
    buf = m.readValue(jp, TokenBuffer.class);
    Map<?, ?> map = m.readValue(buf.asParser(), Map.class);
    assertEquals(1, map.size());
    assertEquals(Boolean.TRUE, map.get("a"));
    assertEquals(JsonToken.END_ARRAY, jp.nextToken());
    assertNull(jp.nextToken());
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 32 with TokenBuffer

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

the class TestCreatorsDelegating method testDelegateWithTokenBuffer.

// [databind#592]
public void testDelegateWithTokenBuffer() throws Exception {
    Value592 value = MAPPER.readValue("{\"a\":1,\"b\":2}", Value592.class);
    assertNotNull(value);
    Object ob = value.stuff;
    assertEquals(TokenBuffer.class, ob.getClass());
    JsonParser jp = ((TokenBuffer) ob).asParser();
    assertToken(JsonToken.START_OBJECT, jp.nextToken());
    assertToken(JsonToken.FIELD_NAME, jp.nextToken());
    assertEquals("a", jp.getCurrentName());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertEquals(1, jp.getIntValue());
    assertToken(JsonToken.FIELD_NAME, jp.nextToken());
    assertEquals("b", jp.getCurrentName());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertEquals(2, jp.getIntValue());
    assertToken(JsonToken.END_OBJECT, jp.nextToken());
    jp.close();
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 33 with TokenBuffer

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

the class TestConversions method testEmbeddedByteArray.

public void testEmbeddedByteArray() throws Exception {
    TokenBuffer buf = new TokenBuffer(MAPPER, false);
    buf.writeObject(new byte[3]);
    JsonNode node = MAPPER.readTree(buf.asParser());
    buf.close();
    assertTrue(node.isBinary());
    byte[] data = node.binaryValue();
    assertNotNull(data);
    assertEquals(3, data.length);
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 34 with TokenBuffer

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

the class TestDefaultForObject method testTokenBuffer.

// Also, let's ensure TokenBuffer gets properly handled
public void testTokenBuffer() throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
    // Ok, first test JSON Object containing buffer:
    TokenBuffer buf = new TokenBuffer(mapper, false);
    buf.writeStartObject();
    buf.writeNumberField("num", 42);
    buf.writeEndObject();
    String json = mapper.writeValueAsString(new ObjectHolder(buf));
    ObjectHolder holder = mapper.readValue(json, ObjectHolder.class);
    assertNotNull(holder.value);
    assertSame(TokenBuffer.class, holder.value.getClass());
    JsonParser jp = ((TokenBuffer) holder.value).asParser();
    assertToken(JsonToken.START_OBJECT, jp.nextToken());
    assertToken(JsonToken.FIELD_NAME, jp.nextToken());
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertToken(JsonToken.END_OBJECT, jp.nextToken());
    assertNull(jp.nextToken());
    jp.close();
    buf.close();
    // then as an array:
    buf = new TokenBuffer(mapper, false);
    buf.writeStartArray();
    buf.writeBoolean(true);
    buf.writeEndArray();
    json = mapper.writeValueAsString(new ObjectHolder(buf));
    holder = mapper.readValue(json, ObjectHolder.class);
    assertNotNull(holder.value);
    assertSame(TokenBuffer.class, holder.value.getClass());
    jp = ((TokenBuffer) holder.value).asParser();
    assertToken(JsonToken.START_ARRAY, jp.nextToken());
    assertToken(JsonToken.VALUE_TRUE, jp.nextToken());
    assertToken(JsonToken.END_ARRAY, jp.nextToken());
    assertNull(jp.nextToken());
    jp.close();
    buf.close();
    // and finally as scalar
    buf = new TokenBuffer(mapper, false);
    buf.writeNumber(321);
    json = mapper.writeValueAsString(new ObjectHolder(buf));
    holder = mapper.readValue(json, ObjectHolder.class);
    assertNotNull(holder.value);
    assertSame(TokenBuffer.class, holder.value.getClass());
    jp = ((TokenBuffer) holder.value).asParser();
    assertToken(JsonToken.VALUE_NUMBER_INT, jp.nextToken());
    assertEquals(321, jp.getIntValue());
    assertNull(jp.nextToken());
    jp.close();
    buf.close();
}
Also used : TokenBuffer(com.fasterxml.jackson.databind.util.TokenBuffer)

Example 35 with TokenBuffer

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

the class DrillValuesRel method convertToJsonNode.

private static JsonNode convertToJsonNode(RelDataType rowType, ImmutableList<ImmutableList<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)

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