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());
}
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();
}
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);
}
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();
}
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();
}
Aggregations