Skip to content

Commit

Permalink
Serializer type name for anonymous classes (#6345)
Browse files Browse the repository at this point in the history
* Extract common routine for mapping an object to the name of its type

* Add anonymous subclasses to serializer tests, and adjust serializer utility to handle anonymous subclasses

* Rename/move SerializerUtil, fix and test for anonymous Scala classes
  • Loading branch information
jpallas authored and scottdraves committed Nov 20, 2017
1 parent d4c6e67 commit a394b65
Show file tree
Hide file tree
Showing 30 changed files with 112 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.twosigma.beakerx.chart.serializer;


import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.Chart;
import com.fasterxml.jackson.core.JsonGenerator;

Expand All @@ -37,7 +38,7 @@ protected void serialize(T chart, JsonGenerator jgen) throws IOException {

super.serialize(chart, jgen);

String type = chart.getClass().getSimpleName();
String type = SerializerUtil.getTypeName(chart);
if ("SimpleTimePlot".equals(type)){
jgen.writeObjectField("type", "TimePlot");
}else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.xychart.CombinedPlot;
import com.twosigma.beakerx.chart.xychart.XYChart;
import java.io.IOException;
Expand All @@ -38,7 +39,7 @@ public void serialize(CombinedPlot plot, JsonGenerator jgen, SerializerProvider
{
jgen.writeStartObject();
super.serialize(plot, jgen);
jgen.writeObjectField("type", plot.getClass().getSimpleName());
jgen.writeObjectField("type", SerializerUtil.getTypeName(plot));
jgen.writeObjectField("init_width", plot.getInitWidth());
jgen.writeObjectField("init_height", plot.getInitHeight());
jgen.writeObjectField("title", plot.getTitle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.Color;
import com.twosigma.beakerx.chart.xychart.NanoPlot;
import com.twosigma.beakerx.chart.xychart.plotitem.ConstantBand;
Expand All @@ -38,7 +39,7 @@ public void serialize(ConstantBand constantBand, JsonGenerator jgen, SerializerP
jgen.writeStartObject();

boolean isNanoPlot = NanoPlot.isNanoPlotClass(constantBand.getPlotType());
jgen.writeObjectField(TYPE, constantBand.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(constantBand));
jgen.writeObjectField("x", isNanoPlot ? processLargeNumbers(constantBand.getX()) : constantBand.getX());
jgen.writeObjectField("y", constantBand.getY());
jgen.writeObjectField("visible", constantBand.getVisible());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.Color;
import com.twosigma.beakerx.chart.xychart.NanoPlot;
import com.twosigma.beakerx.chart.xychart.plotitem.ConstantLine;
Expand All @@ -37,7 +38,7 @@ public void serialize(ConstantLine constantLine, JsonGenerator jgen, SerializerP
jgen.writeStartObject();

boolean isNanoPlot = NanoPlot.isNanoPlotClass(constantLine.getPlotType());
jgen.writeObjectField(TYPE, constantLine.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(constantLine));
jgen.writeObjectField("x", isNanoPlot ? processLargeNumber(constantLine.getX()) : constantLine.getX());
jgen.writeObjectField("y", constantLine.getY());
jgen.writeObjectField("visible", constantLine.getVisible());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.xychart.plotitem.Crosshair;
import com.twosigma.beakerx.chart.Color;
import java.io.IOException;
Expand All @@ -37,7 +38,7 @@ public void serialize(Crosshair crosshair, JsonGenerator jgen, SerializerProvide
throws IOException, JsonProcessingException {

jgen.writeStartObject();
jgen.writeObjectField(TYPE, crosshair.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(crosshair));
if (crosshair.getColor() instanceof Color) {
jgen.writeObjectField("color", crosshair.getColor());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.Graphics;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -36,7 +37,7 @@ public class GraphicsSerializer <T extends Graphics> extends JsonSerializer<T> {
@Override
public void serialize(T graphics, JsonGenerator jgen, SerializerProvider sp)
throws IOException, JsonProcessingException {
jgen.writeObjectField(TYPE, graphics.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(graphics));
jgen.writeObjectField("uid", graphics.getUid());
jgen.writeObjectField("visible", graphics.getVisible());
jgen.writeObjectField("yAxis", graphics.getYAxis());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.legend.LegendPosition;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand All @@ -37,7 +38,7 @@ public void serialize(LegendPosition legendPosition, JsonGenerator jgen, Seriali
throws IOException, JsonProcessingException {

jgen.writeStartObject();
jgen.writeObjectField(TYPE, legendPosition.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(legendPosition));
if (legendPosition.getPosition() != null) {
jgen.writeObjectField(POSITION, legendPosition.getPosition().name());
}else{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.xychart.plotitem.Rasters;
import com.twosigma.beakerx.message.Message;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
Expand All @@ -39,7 +40,7 @@ public void serialize(Rasters rasters, JsonGenerator jgen, SerializerProvider sp

jgen.writeStartObject();

jgen.writeObjectField(TYPE, rasters.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(rasters));
jgen.writeObjectField("x", rasters.getX());
jgen.writeObjectField("y", rasters.getY());
jgen.writeObjectField("opacity", rasters.getOpacity());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.xychart.NanoPlot;
import com.twosigma.beakerx.chart.xychart.plotitem.Text;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand All @@ -37,7 +38,7 @@ public void serialize(Text text, JsonGenerator jgen, SerializerProvider sp)

boolean isNanoPlot = NanoPlot.isNanoPlotClass(text.getPlotType());
jgen.writeStartObject();
jgen.writeObjectField(TYPE, text.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(text));
jgen.writeObjectField("x", isNanoPlot ? processLargeNumber(text.getX()) : text.getX());
jgen.writeObjectField("y", text.getY());
jgen.writeObjectField("show_pointer", text.getShowPointer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import net.sf.jtreemap.swing.TreeMapNode;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand All @@ -35,7 +36,7 @@ public void serialize(TreeMapNode treeMapNode,
JsonProcessingException {
jgen.writeStartObject();

jgen.writeObjectField("type", treeMapNode.getClass().getSimpleName());
jgen.writeObjectField("type", SerializerUtil.getTypeName(treeMapNode));
jgen.writeObjectField("weight", treeMapNode.getWeight());

if (treeMapNode.getValue() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.twosigma.beakerx.chart.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.chart.xychart.plotitem.YAxis;
import java.io.IOException;
import com.fasterxml.jackson.databind.JsonSerializer;
Expand All @@ -33,7 +34,7 @@ public void serialize(YAxis yAxis, JsonGenerator jgen, SerializerProvider sp)
throws IOException, JsonProcessingException {

jgen.writeStartObject();
jgen.writeObjectField(TYPE, yAxis.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(yAxis));
jgen.writeObjectField("label", yAxis.getLabel());
jgen.writeObjectField("auto_range", yAxis.getAutoRange());
jgen.writeObjectField(AUTO_RANGE_INCLUDES_ZERO, yAxis.getAutoRangeIncludesZero());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.jvm.serialization.BeakerObjectConverter;

public class BeakerDashboard extends Observable {
Expand Down Expand Up @@ -163,7 +164,7 @@ public void serialize(BeakerDashboard value, JsonGenerator jgen, SerializerProvi
synchronized(value) {
jgen.writeStartObject();
jgen.writeObjectField("update_time", System.currentTimeMillis());
jgen.writeObjectField("type", value.getClass().getSimpleName());
jgen.writeObjectField("type", SerializerUtil.getTypeName(value));

if (value.getTheStyle()!=null) jgen.writeStringField("thestyle", value.getTheStyle());
if (value.getTheClass()!=null) jgen.writeStringField("theclass", value.getTheClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.google.inject.Inject;
import com.google.inject.Provider;
import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.jvm.object.OutputContainer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void serialize(T value,
synchronized (value) {
jgen.writeStartObject();

jgen.writeObjectField("type", value.getClass().getSimpleName());
jgen.writeObjectField("type", SerializerUtil.getTypeName(value));

serialize(value, jgen);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.twosigma.beakerx.jvm.serialization;

import com.google.inject.Provider;
import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.jvm.object.OutputContainerLayoutManager;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -45,7 +46,7 @@ public void serialize(T value,

synchronized (value) {
jgen.writeStartObject();
jgen.writeObjectField("type", value.getClass().getSimpleName());
jgen.writeObjectField("type", SerializerUtil.getTypeName(value));
jgen.writeObjectField("borderDisplayed", value.isBorderDisplayed());
serialize(value, jgen);
jgen.writeEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.table.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.table.highlight.HeatmapHighlighter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -46,7 +47,7 @@ protected void serializeObj(H value,
SerializerProvider provider)
throws IOException, JsonProcessingException {

jgen.writeObjectField(TYPE, value.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(value));
jgen.writeObjectField("colName", value.getColName());
jgen.writeObjectField(STYLE, value.getStyle());
jgen.writeObjectField("minVal", value.getMinVal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.table.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.table.highlight.UniqueEntriesHighlighter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -37,7 +38,7 @@ public void serialize(UniqueEntriesHighlighter value,

synchronized (value) {
jgen.writeStartObject();
jgen.writeObjectField(TYPE, value.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(value));
jgen.writeObjectField(COL_NAME, value.getColName());
jgen.writeObjectField(STYLE, value.getStyle());
jgen.writeEndObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.twosigma.beakerx.table.serializer;

import com.twosigma.beakerx.util.SerializerUtil;
import com.twosigma.beakerx.table.highlight.ValueHighlighter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand All @@ -37,7 +38,7 @@ public void serialize(ValueHighlighter value,

synchronized (value) {
jgen.writeStartObject();
jgen.writeObjectField(TYPE, value.getClass().getSimpleName());
jgen.writeObjectField(TYPE, SerializerUtil.getTypeName(value));
jgen.writeObjectField(COL_NAME, value.getColName());
jgen.writeObjectField(COLORS, value.getColors());
jgen.writeEndObject();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2017 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.util;

public class SerializerUtil {
/**
* Get the serialization type-name for the object. This is usually the class name.
* If the class is anonymous or inner, walk up the hierarchy to find the ordinary class and use its name.
* Note: anonymous classes in Scala don't follow the same rules as Java.
*/
static public String getTypeName(Object object) {
Class<?> currentClass = object.getClass();
while (currentClass.getCanonicalName() == null) {
currentClass = currentClass.getSuperclass();
}
return currentClass.getSimpleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void initTestStubData() throws IOException {
@Test
public void serializeConstantBand_resultJsonHasType() throws IOException {
//when
ConstantBand constantBand = new ConstantBand();
ConstantBand constantBand = new ConstantBand() {};
constantBandSerializer.serialize(constantBand, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void initTestStubData() throws IOException {
@Test
public void serializeConstantLine_resultJsonHasType() throws IOException {
//when
ConstantLine constantLine = new ConstantLine();
ConstantLine constantLine = new ConstantLine() {};
constantLineSerializer.serialize(constantLine, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void initClassStubData() {
public void initTestStubData() throws IOException {
sw = new StringWriter();
jgen = mapper.getJsonFactory().createJsonGenerator(sw);
line = new Line();
line = new Line() {};
line.setX(Arrays.asList(1, 2, 3));
line.setY(Arrays.asList(1, 2, 3));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ public class LegendPositionSerializerTest {
@BeforeClass
public static void initClassStubData() {
mapper = new ObjectMapper();
legendPositionSerializer = new LegendPositionSerializer();
legendPositionSerializer = new LegendPositionSerializer() {};
}

@Before
public void initTestStubData() throws IOException {
sw = new StringWriter();
jgen = mapper.getJsonFactory().createJsonGenerator(sw);
legendPosition = new LegendPosition();
legendPosition = new LegendPosition() {};
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static void setUpClass() throws IOException {

@Before
public void setUp() throws Exception {
rasters = new Rasters();
rasters = new Rasters() {};
List<Number> value = Collections.singletonList(1);
rasters.setY(value);
rasters.setWidth(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static void initClassStubData() {
public void initTestStubData() throws IOException {
sw = new StringWriter();
jgen = mapper.getJsonFactory().createJsonGenerator(sw);
text = new Text();
text = new Text() {};
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void initTestStubData() throws IOException {
@Test
public void serializeTreeMapNode_resultJsonHasType() throws IOException {
//when
TreeMapNode treeMapNode = new TreeMapNode("label");
TreeMapNode treeMapNode = new TreeMapNode("label") {};
treeMapNodeSerializer.serialize(treeMapNode, jgen, new DefaultSerializerProvider.Impl());
jgen.flush();
//then
Expand Down
Loading

0 comments on commit a394b65

Please sign in to comment.