-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#7653: support autotranslation for clojure
- Loading branch information
1 parent
d4c39f6
commit 94ab10b
Showing
22 changed files
with
750 additions
and
202 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
kernel/base/src/main/java/com/twosigma/beakerx/BaseBeakerXJsonSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.