Search in sources :

Example 1 with CodeCell

use of com.twosigma.beakerx.CodeCell in project beakerx by twosigma.

the class BeakerCodeCellListDeserializer method deserialize.

@Override
public BeakerCodeCellList deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
    ObjectMapper mapper = (ObjectMapper) jp.getCodec();
    JsonNode node = mapper.readTree(jp);
    List<CodeCell> l = new ArrayList<CodeCell>();
    if (node.isArray()) {
        for (JsonNode o : node) {
            Object obj = objectSerializerProvider.get().deserialize(o, mapper);
            if (obj instanceof CodeCell)
                l.add((CodeCell) obj);
        }
    }
    BeakerCodeCellList r = new BeakerCodeCellList();
    r.theList = l;
    return r;
}
Also used : ArrayList(java.util.ArrayList) CodeCell(com.twosigma.beakerx.CodeCell) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with CodeCell

use of com.twosigma.beakerx.CodeCell in project beakerx by twosigma.

the class CodeCellListDeserializerTest method deserialize_resultObjectHasBeakerCodeCellList.

@Test
public void deserialize_resultObjectHasBeakerCodeCellList() throws Exception {
    // given
    ObjectMapper mapper = new ObjectMapper();
    String str = "[{\"type\":\"CodeCell\",\"execution_count\":null," + "\"cell_type\":null,\"outputs\":null,\"metadata\":null,\"source\":null}," + "{\"type\":\"CodeCell\",\"execution_count\":null,\"" + "cell_type\":null,\"outputs\":null,\"metadata\":null,\"source\":null}]";
    BeakerCodeCellListDeserializer deserializer = new BeakerCodeCellListDeserializer(() -> {
        BasicObjectSerializer boSerializer = new BasicObjectSerializer();
        boSerializer.addTypeDeserializer(new CodeCell.DeSerializer(boSerializer));
        return boSerializer;
    });
    // when
    BeakerCodeCellList bkList = (BeakerCodeCellList) deserializer.deserialize(mapper.getFactory().createParser(str), null);
    // then
    Assertions.assertThat(bkList).isNotNull();
    Assertions.assertThat(bkList.theList.size()).isEqualTo(2);
}
Also used : CodeCell(com.twosigma.beakerx.CodeCell) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 3 with CodeCell

use of com.twosigma.beakerx.CodeCell in project beakerx by twosigma.

the class CodeCellDeSerializerTest method deserialize_resultObjectHasExecutionCount.

@Test
public void deserialize_resultObjectHasExecutionCount() throws Exception {
    String execCount = "5";
    // given
    ObjectMapper mapper = new ObjectMapper();
    JsonNode actualObj = mapper.readTree("{\"type\":\"CodeCell\",\"execution_count\":" + execCount + ",\"cell_type\":null," + "\"outputs\":null,\"metadata\":null,\"source\":null}");
    CodeCell.DeSerializer deserializer = new CodeCell.DeSerializer(new BasicObjectSerializer());
    // when
    CodeCell bkCell = (CodeCell) deserializer.deserialize(actualObj, mapper);
    // then
    Assertions.assertThat(bkCell).isNotNull();
    Assertions.assertThat(bkCell.getExecutionCount()).isEqualTo(execCount);
}
Also used : CodeCell(com.twosigma.beakerx.CodeCell) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 4 with CodeCell

use of com.twosigma.beakerx.CodeCell in project beakerx by twosigma.

the class CodeCellSerializerTest method setUp.

@Before
public void setUp() throws Exception {
    KernelManager.register(new KernelTest());
    codeCell = new CodeCell();
}
Also used : KernelTest(com.twosigma.beakerx.KernelTest) CodeCell(com.twosigma.beakerx.CodeCell) Before(org.junit.Before)

Example 5 with CodeCell

use of com.twosigma.beakerx.CodeCell in project beakerx by twosigma.

the class GetCodeCellsHandler method getBeakerCodeCells.

private List<CodeCell> getBeakerCodeCells(Object value) {
    List<CodeCell> codeCellList = null;
    StringWriter sw = new StringWriter();
    JsonGenerator jgen = null;
    try {
        jgen = objectMapper.getFactory().createGenerator(sw);
        objectSerializer.writeObject(value, jgen, true);
        jgen.flush();
        sw.flush();
        codeCellList = Arrays.asList(objectMapper.readValue(sw.toString(), CodeCell[].class));
    } catch (IOException e) {
        e.printStackTrace();
    }
    return codeCellList;
}
Also used : StringWriter(java.io.StringWriter) CodeCell(com.twosigma.beakerx.CodeCell) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException)

Aggregations

CodeCell (com.twosigma.beakerx.CodeCell)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 Test (org.junit.Test)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 KernelTest (com.twosigma.beakerx.KernelTest)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1