Search in sources :

Example 46 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project drill by apache.

the class TestRestJson method runQuery.

private void runQuery(QueryWrapper query) throws IOException {
    ObjectWriter writer = mapper.writerFor(QueryWrapper.class);
    String json = writer.writeValueAsString(query);
    String url = String.format("http://localhost:%d/query.json", portNumber);
    Request request = new Request.Builder().url(url).post(RequestBody.create(json, JSON_MEDIA_TYPE)).build();
    try (Response response = httpClient.newCall(request).execute()) {
        try (InputStream inStream = response.body().byteStream()) {
            IOUtils.skip(inStream, Integer.MAX_VALUE);
        }
    }
}
Also used : Response(okhttp3.Response) InputStream(java.io.InputStream) Request(okhttp3.Request) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 47 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project Fast-Android-Networking by amitshekhariitbhu.

the class JacksonParserFactory method requestBodyParser.

@Override
public Parser<?, RequestBody> requestBodyParser(Type type) {
    JavaType javaType = mapper.getTypeFactory().constructType(type);
    ObjectWriter writer = mapper.writerFor(javaType);
    return new JacksonRequestBodyParser<>(writer);
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter)

Example 48 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project drill by apache.

the class TestRepeated method listOfList.

// 
// @Test
// public void repeatedMap() {
// 
// /**
// * We're going to try to create an object that looks like:
// *
// *  {
// *    a: [
// *      {x: 1, y: 2}
// *      {x: 2, y: 1}
// *    ]
// *  }
// *
// */
// MapVector v = new MapVector("", allocator);
// ComplexWriter writer = new ComplexWriterImpl("col", v);
// 
// MapWriter map = writer.rootAsMap();
// 
// map.start();
// ListWriter list = map.list("a");
// MapWriter inner = list.map();
// 
// IntHolder holder = new IntHolder();
// IntWriter xCol = inner.integer("x");
// IntWriter yCol = inner.integer("y");
// 
// inner.start();
// 
// holder.value = 1;
// xCol.write(holder);
// holder.value = 2;
// yCol.write(holder);
// 
// inner.end();
// 
// inner.start();
// 
// holder.value = 2;
// xCol.write(holder);
// holder.value = 1;
// yCol.write(holder);
// 
// inner.end();
// 
// IntWriter numCol = map.integer("nums");
// holder.value = 14;
// numCol.write(holder);
// 
// map.end();
// 
// 
// assertTrue(writer.ok());
// 
// }
@Test
public void listOfList() throws Exception {
    /**
     * We're going to try to create an object that looks like:
     *
     *  {
     *    a: [
     *      [1,2,3],
     *      [2,3,4]
     *    ],
     *    nums: 14,
     *    b: [
     *      { c: 1 },
     *      { c: 2 , x: 15}
     *    ]
     *  }
     */
    final MapVector mapVector = new MapVector("", allocator, null);
    final ComplexWriterImpl writer = new ComplexWriterImpl("col", mapVector);
    writer.allocate();
    {
        final MapWriter map = writer.rootAsMap();
        final ListWriter list = map.list("a");
        list.startList();
        final ListWriter innerList = list.list();
        final IntWriter innerInt = innerList.integer();
        innerList.startList();
        final IntHolder holder = new IntHolder();
        holder.value = 1;
        innerInt.write(holder);
        holder.value = 2;
        innerInt.write(holder);
        holder.value = 3;
        innerInt.write(holder);
        innerList.endList();
        innerList.startList();
        holder.value = 4;
        innerInt.write(holder);
        holder.value = 5;
        innerInt.write(holder);
        innerList.endList();
        list.endList();
        final IntWriter numCol = map.integer("nums");
        holder.value = 14;
        numCol.write(holder);
        final MapWriter repeatedMap = map.list("b").map();
        repeatedMap.start();
        holder.value = 1;
        repeatedMap.integer("c").write(holder);
        repeatedMap.end();
        repeatedMap.start();
        holder.value = 2;
        repeatedMap.integer("c").write(holder);
        final BigIntHolder h = new BigIntHolder();
        h.value = 15;
        repeatedMap.bigInt("x").write(h);
        repeatedMap.end();
        map.end();
    }
    {
        writer.setPosition(1);
        final MapWriter map = writer.rootAsMap();
        final ListWriter list = map.list("a");
        list.startList();
        final ListWriter innerList = list.list();
        final IntWriter innerInt = innerList.integer();
        innerList.startList();
        final IntHolder holder = new IntHolder();
        holder.value = -1;
        innerInt.write(holder);
        holder.value = -2;
        innerInt.write(holder);
        holder.value = -3;
        innerInt.write(holder);
        innerList.endList();
        innerList.startList();
        holder.value = -4;
        innerInt.write(holder);
        holder.value = -5;
        innerInt.write(holder);
        innerList.endList();
        list.endList();
        final IntWriter numCol = map.integer("nums");
        holder.value = -28;
        numCol.write(holder);
        final MapWriter repeatedMap = map.list("b").map();
        repeatedMap.start();
        holder.value = -1;
        repeatedMap.integer("c").write(holder);
        repeatedMap.end();
        repeatedMap.start();
        holder.value = -2;
        repeatedMap.integer("c").write(holder);
        final BigIntHolder h = new BigIntHolder();
        h.value = -30;
        repeatedMap.bigInt("x").write(h);
        repeatedMap.end();
        map.end();
    }
    final ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    final JsonWriter jsonWriter = new JsonWriter(stream, true, true);
    final FieldReader reader = mapVector.getChild("col", MapVector.class).getReader();
    reader.setPosition(0);
    jsonWriter.write(reader);
    reader.setPosition(1);
    jsonWriter.write(reader);
    writer.close();
}
Also used : MapWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.MapWriter) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) ListWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ListWriter) IntHolder(org.apache.drill.exec.expr.holders.IntHolder) BigIntHolder(org.apache.drill.exec.expr.holders.BigIntHolder) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ComplexWriterImpl(org.apache.drill.exec.vector.complex.impl.ComplexWriterImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonWriter(org.apache.drill.exec.vector.complex.fn.JsonWriter) FieldReader(org.apache.drill.exec.vector.complex.reader.FieldReader) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MapVector(org.apache.drill.exec.vector.complex.MapVector) Test(org.junit.Test) BaseTest(org.apache.drill.test.BaseTest)

Example 49 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project flink by apache.

the class RexWindowBoundSerdeTest method testSerde.

@Test
public void testSerde() throws IOException {
    SerdeContext serdeCtx = new SerdeContext(null, new FlinkContextImpl(false, TableConfig.getDefault(), new ModuleManager(), null, CatalogManagerMocks.createEmptyCatalogManager(), null), Thread.currentThread().getContextClassLoader(), FlinkTypeFactory.INSTANCE(), FlinkSqlOperatorTable.instance());
    ObjectReader objectReader = JsonSerdeUtil.createObjectReader(serdeCtx);
    ObjectWriter objectWriter = JsonSerdeUtil.createObjectWriter(serdeCtx);
    assertEquals(RexWindowBounds.CURRENT_ROW, objectReader.readValue(objectWriter.writeValueAsString(RexWindowBounds.CURRENT_ROW), RexWindowBound.class));
    assertEquals(RexWindowBounds.UNBOUNDED_FOLLOWING, objectReader.readValue(objectWriter.writeValueAsString(RexWindowBounds.UNBOUNDED_FOLLOWING), RexWindowBound.class));
    assertEquals(RexWindowBounds.UNBOUNDED_PRECEDING, objectReader.readValue(objectWriter.writeValueAsString(RexWindowBounds.UNBOUNDED_PRECEDING), RexWindowBound.class));
    RexBuilder builder = new RexBuilder(FlinkTypeFactory.INSTANCE());
    RexWindowBound windowBound = RexWindowBounds.following(builder.makeLiteral("test"));
    assertEquals(windowBound, objectReader.readValue(objectWriter.writeValueAsString(windowBound), RexWindowBound.class));
    windowBound = RexWindowBounds.preceding(builder.makeLiteral("test"));
    assertEquals(windowBound, objectReader.readValue(objectWriter.writeValueAsString(windowBound), RexWindowBound.class));
}
Also used : FlinkContextImpl(org.apache.flink.table.planner.calcite.FlinkContextImpl) RexWindowBound(org.apache.calcite.rex.RexWindowBound) ObjectWriter(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter) RexBuilder(org.apache.calcite.rex.RexBuilder) ObjectReader(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectReader) ModuleManager(org.apache.flink.table.module.ModuleManager) Test(org.junit.Test)

Example 50 with ObjectWriter

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter in project flink by apache.

the class ResolvedCatalogTableSerdeTest method testDontSerializeExternalInlineTable.

@Test
void testDontSerializeExternalInlineTable() {
    SerdeContext serdeCtx = configuredSerdeContext();
    ObjectWriter objectWriter = JsonSerdeUtil.createObjectWriter(serdeCtx);
    assertThatThrownBy(() -> objectWriter.writeValueAsString(new ResolvedCatalogTable(new ExternalCatalogTable(Schema.newBuilder().fromResolvedSchema(FULL_RESOLVED_SCHEMA).build()), FULL_RESOLVED_SCHEMA))).satisfies(FlinkAssertions.anyCauseMatches(ValidationException.class, "Cannot serialize the table as it's an external inline table"));
}
Also used : ValidationException(org.apache.flink.table.api.ValidationException) ResolvedCatalogTable(org.apache.flink.table.catalog.ResolvedCatalogTable) ExternalCatalogTable(org.apache.flink.table.catalog.ExternalCatalogTable) JsonSerdeTestUtil.configuredSerdeContext(org.apache.flink.table.planner.plan.nodes.exec.serde.JsonSerdeTestUtil.configuredSerdeContext) ObjectWriter(org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectWriter) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)140 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)61 IOException (java.io.IOException)31 Test (org.junit.Test)30 File (java.io.File)17 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)15 ArrayList (java.util.ArrayList)12 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)11 JavaType (com.fasterxml.jackson.databind.JavaType)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)10 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)7 FileOutputStream (java.io.FileOutputStream)7 OutputStream (java.io.OutputStream)7 StringWriter (java.io.StringWriter)7 Map (java.util.Map)7 JCommander (com.beust.jcommander.JCommander)6 ParameterException (com.beust.jcommander.ParameterException)6 SimpleFilterProvider (com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider)6 RateLimiter (com.google.common.util.concurrent.RateLimiter)6 FileInputStream (java.io.FileInputStream)6