Search in sources :

Example 1 with Serializer

use of org.finos.legend.engine.plan.execution.result.serialization.Serializer in project legend-engine by finos.

the class TestJsonStreamingResult method doesNotAutocloseJsonArrays.

private void doesNotAutocloseJsonArrays(SerializationFormat format) throws Exception {
    JsonStreamingResult result = new JsonStreamingResult(x -> {
        try {
            x.writeStartArray();
            // throw before closing array to mimic streaming error
            throw new TestException();
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }, null);
    Serializer serializer = result.getSerializer(format);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        serializer.stream(outputStream);
    } catch (TestException e) {
        try {
            new ObjectMapper().readTree(outputStream.toByteArray());
            Assert.fail("Serialized bytes should not contians valid JSON.  Is Jackson autoclosing arrays and objects?");
        } catch (JsonEOFException eofException) {
            assertThat(eofException, hasMessage(CoreMatchers.startsWith("Unexpected end-of-input: expected close marker for Array")));
        }
    }
}
Also used : JsonStreamingResult(org.finos.legend.engine.plan.execution.result.json.JsonStreamingResult) JsonEOFException(com.fasterxml.jackson.core.io.JsonEOFException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Serializer(org.finos.legend.engine.plan.execution.result.serialization.Serializer)

Example 2 with Serializer

use of org.finos.legend.engine.plan.execution.result.serialization.Serializer in project legend-sdlc by finos.

the class LegendPureV1TestCase method getResultAsJson.

private JsonNode getResultAsJson(Result result) {
    if (result instanceof StreamingResult) {
        try {
            Serializer serializer = ((StreamingResult) result).getSerializer(SerializationFormat.DEFAULT);
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
            serializer.stream(byteStream);
            return objectMapper.readTree(byteStream.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (result instanceof ConstantResult) {
        Object value = ((ConstantResult) result).getValue();
        return objectMapper.valueToTree(value);
    }
    throw new RuntimeException("Unhandled result type: " + result.getClass().getSimpleName());
}
Also used : ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Serializer(org.finos.legend.engine.plan.execution.result.serialization.Serializer)

Example 3 with Serializer

use of org.finos.legend.engine.plan.execution.result.serialization.Serializer in project legend-engine by finos.

the class MappingTestRunner method getResultAsJson.

private JsonNode getResultAsJson(Result result) {
    if (result instanceof StreamingResult) {
        try {
            Serializer serializer = ((StreamingResult) result).getSerializer(SerializationFormat.DEFAULT);
            ByteArrayOutputStream byteStream = new ByteArrayOutputStream(1024);
            serializer.stream(byteStream);
            return objectMapper.readTree(byteStream.toByteArray());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    if (result instanceof ConstantResult) {
        Object value = ((ConstantResult) result).getValue();
        return objectMapper.valueToTree(value);
    }
    throw new RuntimeException("Unhandled result type: " + result.getClass().getSimpleName());
}
Also used : ConstantResult(org.finos.legend.engine.plan.execution.result.ConstantResult) StreamingResult(org.finos.legend.engine.plan.execution.result.StreamingResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Serializer(org.finos.legend.engine.plan.execution.result.serialization.Serializer)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 Serializer (org.finos.legend.engine.plan.execution.result.serialization.Serializer)3 ConstantResult (org.finos.legend.engine.plan.execution.result.ConstantResult)2 StreamingResult (org.finos.legend.engine.plan.execution.result.StreamingResult)2 JsonEOFException (com.fasterxml.jackson.core.io.JsonEOFException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 UncheckedIOException (java.io.UncheckedIOException)1 JsonStreamingResult (org.finos.legend.engine.plan.execution.result.json.JsonStreamingResult)1