Skip to content

Commit

Permalink
#7610 #7625 fixed deserialization of table objects in autotranslation
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz Mitusinski committed Jul 10, 2018
1 parent d4c39f6 commit dbb113d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
package com.twosigma.beakerx;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.jvm.serialization.BasicObjectSerializer;
import com.twosigma.beakerx.table.TableDisplayToJson;
import com.twosigma.beakerx.table.serializer.AutotranslationDefaultDeserializer;
import com.twosigma.beakerx.table.serializer.TableDisplayDeSerializer;

import java.io.IOException;
import java.io.StringWriter;
Expand All @@ -35,6 +38,8 @@ public DefaultBeakerXJsonSerializer() {
objectMapper.enable(WRITE_ENUMS_USING_TO_STRING);
objectMapper.registerModule(TableDisplayToJson.tableDisplayModule());
objectSerializer = new BasicObjectSerializer();
objectSerializer.addTypeDeserializer(new TableDisplayDeSerializer(objectSerializer));
objectSerializer.addTypeDeserializer(new AutotranslationDefaultDeserializer());
}

@Override
Expand All @@ -54,7 +59,8 @@ public String toJson(Object value) {
@Override
public Object fromJson(String json) {
try {
return objectMapper.readValue(json, Object.class);
JsonNode node = objectMapper.readTree(json);
return objectSerializer.deserialize(node, objectMapper);
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.twosigma.beakerx.table.serializer;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.jvm.serialization.ObjectDeserializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

public class AutotranslationDefaultDeserializer implements ObjectDeserializer{

private final static Logger logger = LoggerFactory.getLogger(AutotranslationDefaultDeserializer.class.getName());

@Override
public boolean canBeUsed(JsonNode n) {
// used as a last deserializer if there is no valid deserializer for processed object
return true;
}

@Override
public Object deserialize(JsonNode n, ObjectMapper mapper) {
try {
return mapper.readValue(n.toString(), Object.class);
} catch (IOException e) {
logger.error("exception in autotranslation default deserialization", e);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static List<List<?>> getValues(BeakerObjectConverter parent, JsonNode n,
List<List<?>> values = null;
List<String> classes = null;
if (n.has("types"))
classes = mapper.readValue(n.get("types").asText(), List.class);
classes = mapper.readValue(n.get("types").toString(), List.class);
if (n.has("values")) {
JsonNode nn = n.get("values");
values = new ArrayList<List<?>>();
Expand All @@ -87,15 +87,15 @@ public static List<List<?>> getValues(BeakerObjectConverter parent, JsonNode n,
public static List<String> getColumns(JsonNode n, ObjectMapper mapper) throws IOException {
List<String> columns = null;
if (n.has("columnNames"))
columns = mapper.readValue(n.get("columnNames").asText(), List.class);
columns = mapper.readValue(n.get("columnNames").toString(), List.class);
return columns;
}

@SuppressWarnings("unchecked")
public static List<String> getClasses(JsonNode n, ObjectMapper mapper) throws IOException {
List<String> classes = null;
if (n.has("types"))
classes = mapper.readValue(n.get("types").asText(), List.class);
classes = mapper.readValue(n.get("types").toString(), List.class);
return classes;
}

Expand All @@ -119,7 +119,7 @@ public static Pair<String, Object> getDeserializeObject(BeakerObjectConverter pa
List<String> classes = TableDisplayDeSerializer.getClasses(n, mapper);

if (n.has("subtype"))
subtype = mapper.readValue(n.get("subtype").asText(), String.class);
subtype = mapper.readValue(n.get("subtype").toString(), String.class);

if (subtype != null && subtype.equals(TableDisplay.DICTIONARY_SUBTYPE)) {
o = getValuesAsDictionary(parent, n, mapper);
Expand Down

0 comments on commit dbb113d

Please sign in to comment.