Skip to content

Commit

Permalink
#7653: support autotranslation for clojure
Browse files Browse the repository at this point in the history
  • Loading branch information
jaroslawmalekcodete committed Jul 10, 2018
1 parent d4c39f6 commit 94ab10b
Show file tree
Hide file tree
Showing 22 changed files with 750 additions and 202 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.jvm.serialization.BeakerObjectConverter;
import com.twosigma.beakerx.table.TableDisplayToJson;

import java.io.IOException;
import java.io.StringWriter;

import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING;

public abstract class BaseBeakerXJsonSerializer implements BeakerXJsonSerializer {

private final ObjectMapper objectMapper;
private final BeakerObjectConverter serializer;

public BaseBeakerXJsonSerializer() {
this.objectMapper = new ObjectMapper();
this.objectMapper.enable(WRITE_ENUMS_USING_TO_STRING);
this.objectMapper.registerModule(TableDisplayToJson.tableDisplayModule());
this.serializer = createSerializer();
}

protected abstract BeakerObjectConverter createSerializer();

@Override
public String toJson(Object value) {
StringWriter sw = new StringWriter();
try {
JsonGenerator jgen = objectMapper.getFactory().createGenerator(sw);
boolean success = serializer.writeObject(value, jgen, true);
jgen.flush();
sw.flush();
if (success) {
return sw.toString();
} else {
return value.toString();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public Object fromJson(String json) {
try {
return serializer.deserialize(objectMapper.readTree(json), objectMapper);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,13 @@
*/
package com.twosigma.beakerx;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.twosigma.beakerx.jvm.serialization.BasicObjectSerializer;
import com.twosigma.beakerx.table.TableDisplayToJson;
import com.twosigma.beakerx.jvm.serialization.BeakerObjectConverter;

import java.io.IOException;
import java.io.StringWriter;

import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING;

public class DefaultBeakerXJsonSerializer implements BeakerXJsonSerializer {

private final BasicObjectSerializer objectSerializer;
private ObjectMapper objectMapper;

public DefaultBeakerXJsonSerializer() {
this.objectMapper = new ObjectMapper();
objectMapper.enable(WRITE_ENUMS_USING_TO_STRING);
objectMapper.registerModule(TableDisplayToJson.tableDisplayModule());
objectSerializer = new BasicObjectSerializer();
}
public class DefaultBeakerXJsonSerializer extends BaseBeakerXJsonSerializer {

@Override
public String toJson(Object value) {
try {
StringWriter sw = new StringWriter();
JsonGenerator jgen = objectMapper.getFactory().createGenerator(sw);
objectSerializer.writeObject(value, jgen, true);
jgen.flush();
sw.flush();
return sw.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
protected BeakerObjectConverter createSerializer() {
return new BasicObjectSerializer();
}

@Override
public Object fromJson(String json) {
try {
return objectMapper.readValue(json, Object.class);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.twosigma.beakerx.evaluator.InternalVariable;
import com.twosigma.beakerx.jvm.object.SimpleEvaluationObject;
import com.twosigma.beakerx.kernel.KernelConfigurationFile;
import com.twosigma.beakerx.kernel.ConfigurationFile;
import com.twosigma.beakerx.kernel.comm.Comm;
import com.twosigma.beakerx.kernel.comm.TargetNamesEnum;

Expand Down Expand Up @@ -170,12 +170,12 @@ public String getContext() {
return this.autotranslationService.getContextAsString();
}

public static NamespaceClient create(String id, KernelConfigurationFile configurationFile, CommRepository commRepository) {
public static NamespaceClient create(String id, ConfigurationFile configurationFile, CommRepository commRepository) {
return create(id, configurationFile, new DefaultBeakerXJsonSerializer(), commRepository);
}

public static NamespaceClient create(String id,
KernelConfigurationFile configurationFile,
ConfigurationFile configurationFile,
BeakerXJsonSerializer serializer,
CommRepository commRepository) {
if (configurationFile.getContext().isPresent()) {
Expand Down
Loading

0 comments on commit 94ab10b

Please sign in to comment.