Skip to content

Commit

Permalink
Reuse existing mapper/coder rather than creating new one
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey committed Nov 26, 2024
1 parent 9c9ac99 commit 598321b
Show file tree
Hide file tree
Showing 23 changed files with 80 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,29 @@
package org.finos.legend.engine.protocol.pure.v1.model.data;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory;
import java.io.IOException;
import java.util.Objects;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.context.PackageableElementPointer;
import org.finos.legend.engine.protocol.pure.v1.model.context.PackageableElementType;

import java.io.IOException;
import java.util.Objects;

@JsonDeserialize(using = DataElementReference.DataElementReferenceDeserializer.class)
public class DataElementReference extends EmbeddedData
{
public PackageableElementPointer dataElement;

public static class DataElementReferenceDeserializer extends JsonDeserializer<DataElementReference>
{

private static ObjectMapper objectMapper = PureProtocolObjectMapperFactory.getNewObjectMapper();

@Override
public DataElementReference deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
ObjectCodec codec = jsonParser.getCodec();
JsonNode node = codec.readTree(jsonParser);
JsonNode dataElementNode = node.get("dataElement");
DataElementReference dataElementReference = new DataElementReference();
if (dataElementNode != null)
Expand All @@ -51,12 +47,12 @@ public DataElementReference deserialize(JsonParser jsonParser, DeserializationCo
dataElementReference.dataElement = new PackageableElementPointer(
PackageableElementType.DATA,
dataElementNode.textValue(),
Objects.isNull(node.get("sourceInformation")) ? null : objectMapper.treeToValue(node.get("sourceInformation"), SourceInformation.class)
Objects.isNull(node.get("sourceInformation")) ? null : codec.treeToValue(node.get("sourceInformation"), SourceInformation.class)
);
}
else if (dataElementNode.isObject())
{
dataElementReference.dataElement = objectMapper.treeToValue(dataElementNode, PackageableElementPointer.class);
dataElementReference.dataElement = codec.treeToValue(dataElementNode, PackageableElementPointer.class);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.finos.legend.engine.protocol.pure.v1.model.data;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -39,13 +40,11 @@ public class ModelStoreData extends EmbeddedData

public static class ModelStoreDeserializer extends JsonDeserializer<ModelStoreData>
{

private static ObjectMapper objectMapper = PureProtocolObjectMapperFactory.getNewObjectMapper();

@Override
public ModelStoreData deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
ObjectCodec codec = jsonParser.getCodec();
JsonNode node = codec.readTree(jsonParser);
JsonNode instances = node.get("instances");
JsonNode modelDataJsonNode = node.get("modelData");
ModelStoreData result = new ModelStoreData();
Expand All @@ -58,7 +57,7 @@ public ModelStoreData deserialize(JsonParser jsonParser, DeserializationContext
Iterator<JsonNode> elements = modelDataNode.elements();
while (elements.hasNext())
{
result.modelData.add(objectMapper.treeToValue(elements.next(), ModelTestData.class));
result.modelData.add(codec.treeToValue(elements.next(), ModelTestData.class));
}
}
else
Expand All @@ -78,7 +77,7 @@ else if (instances != null)
Map.Entry<String, JsonNode> instance = itr.next();
String model = instance.getKey();
JsonNode instanceVal = instance.getValue();
ValueSpecification val = objectMapper.treeToValue(instanceVal, ValueSpecification.class);
ValueSpecification val = codec.treeToValue(instanceVal, ValueSpecification.class);
ModelInstanceTestData modelInstanceTestData = new ModelInstanceTestData();
modelInstanceTestData.model = model;
modelInstanceTestData.instances = val;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
package org.finos.legend.engine.protocol.pure.v1.model.valueSpecification;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.fasterxml.jackson.databind.node.POJONode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.TextNode;
import java.io.IOException;
import java.util.List;
import org.eclipse.collections.api.factory.Lists;
import org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.ClassInstance;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.classInstance.AggregateValue;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.classInstance.ExecutionContextInstance;
Expand All @@ -41,20 +42,16 @@
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.classInstance.graph.RootGraphFetchTree;
import org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.raw.classInstance.path.Path;

import java.io.IOException;
import java.util.List;

@JsonDeserialize(using = ClassInstanceWrapper.ClassInstanceWrapperDeserializer.class)
public class ClassInstanceWrapper extends ValueSpecification
{
protected static ObjectMapper om = PureProtocolObjectMapperFactory.getNewObjectMapper();

public static class ClassInstanceWrapperDeserializer extends JsonDeserializer<ValueSpecification>
{
@Override
public ValueSpecification deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
ObjectCodec codec = jsonParser.getCodec();
JsonNode node = codec.readTree(jsonParser);
JsonNode jtype = node.get("_type");
String type = jtype == null ? null : jtype.asText();
if (!(node instanceof NullNode))
Expand All @@ -69,68 +66,68 @@ public ValueSpecification deserialize(JsonParser jsonParser, DeserializationCont
node.fieldNames().forEachRemaining(fields::add);
if ("path".equals(type) || node.get("path") != null)
{
Path p = om.treeToValue(node, Path.class);
Path p = codec.treeToValue(node, Path.class);
return new ClassInstance("path", p, p.sourceInformation);
}
if ("rootGraphFetchTree".equals(type) || node.get("class") != null)
{
((ObjectNode) node).set("_type", new TextNode("rootGraphFetchTree"));
RootGraphFetchTree p = om.treeToValue(node, RootGraphFetchTree.class);
RootGraphFetchTree p = codec.treeToValue(node, RootGraphFetchTree.class);
return new ClassInstance("rootGraphFetchTree", p, p.sourceInformation);
}
if ("listInstance".equals(type) || node.get("values") != null || fields.isEmpty() || (fields.size() == 1 && "sourceInformation".equals(fields.get(0))))
{
PureList p = om.treeToValue(node, PureList.class);
PureList p = codec.treeToValue(node, PureList.class);
return new ClassInstance("listInstance", p, p.sourceInformation);
}
if ("pair".equals(type) || node.get("first") != null)
{
Pair p = om.treeToValue(node, Pair.class);
Pair p = codec.treeToValue(node, Pair.class);
return new ClassInstance("pair", p, p.sourceInformation);
}
if ("aggregateValue".equals(type) || node.get("mapFn") != null && node.get("name") == null)
{
AggregateValue p = om.treeToValue(node, AggregateValue.class);
AggregateValue p = codec.treeToValue(node, AggregateValue.class);
return new ClassInstance("aggregateValue", p, p.sourceInformation);
}
if ("tdsAggregateValue".equals(type) || node.get("mapFn") != null && node.get("name") != null)
{
TDSAggregateValue p = om.treeToValue(node, TDSAggregateValue.class);
TDSAggregateValue p = codec.treeToValue(node, TDSAggregateValue.class);
return new ClassInstance("tdsAggregateValue", p, p.sourceInformation);
}
if ("tdsColumnInformation".equals(type) || node.get("columnFn") != null)
{
TDSColumnInformation p = om.treeToValue(node, TDSColumnInformation.class);
TDSColumnInformation p = codec.treeToValue(node, TDSColumnInformation.class);
return new ClassInstance("tdsColumnInformation", p, p.sourceInformation);
}
if ("tdsSortInformation".equals(type) || node.get("direction") != null)
{
TDSSortInformation p = om.treeToValue(node, TDSSortInformation.class);
TDSSortInformation p = codec.treeToValue(node, TDSSortInformation.class);
return new ClassInstance("tdsSortInformation", p, p.sourceInformation);
}
if ("tdsOlapRank".equals(type) || node.get("function") != null && node.get("columnName") == null)
{
TdsOlapRank p = om.treeToValue(node, TdsOlapRank.class);
TdsOlapRank p = codec.treeToValue(node, TdsOlapRank.class);
return new ClassInstance("tdsOlapRank", p, p.sourceInformation);
}
if ("tdsOlapAggregation".equals(type) || node.get("function") != null && node.get("columnName") != null)
{
TdsOlapAggregation p = om.treeToValue(node, TdsOlapAggregation.class);
TdsOlapAggregation p = codec.treeToValue(node, TdsOlapAggregation.class);
return new ClassInstance("tdsOlapAggregation", p, p.sourceInformation);
}
if ("runtimeInstance".equals(type) || node.get("runtime") != null)
{
RuntimeInstance p = om.treeToValue(node, RuntimeInstance.class);
RuntimeInstance p = codec.treeToValue(node, RuntimeInstance.class);
return new ClassInstance("runtimeInstance", p, p.sourceInformation);
}
if ("executionContextInstance".equals(type) || node.get("executionContext") != null)
{
ExecutionContextInstance p = om.treeToValue(node, ExecutionContextInstance.class);
ExecutionContextInstance p = codec.treeToValue(node, ExecutionContextInstance.class);
return new ClassInstance("executionContextInstance", p, p.sourceInformation);
}
if ("alloySerializationConfig".equals(type) || node.get("typeKeyName") != null)
{
SerializationConfig p = om.treeToValue(node, SerializationConfig.class);
SerializationConfig p = codec.treeToValue(node, SerializationConfig.class);
return new ClassInstance("alloySerializationConfig", p, p.sourceInformation);
}
throw new RuntimeException("NOT SUPPORTED\n" + node.toPrettyString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,20 @@
package org.finos.legend.engine.protocol.pure.v1.model.valueSpecification;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import org.eclipse.collections.api.factory.Lists;
import org.finos.legend.engine.protocol.pure.v1.PureProtocolObjectMapperFactory;
import java.io.IOException;
import org.finos.legend.engine.protocol.pure.v1.model.SourceInformation;
import org.finos.legend.engine.protocol.pure.v1.model.packageableElement.domain.Multiplicity;
import org.finos.legend.engine.protocol.pure.v1.model.type.GenericType;
import org.finos.legend.engine.protocol.pure.v1.model.type.PackageableType;

import java.io.IOException;

@JsonDeserialize(using = Variable.VariableDeserializer.class)
public class Variable extends ValueSpecification
{
private static ObjectMapper om = PureProtocolObjectMapperFactory.getNewObjectMapper();

public String name;
public GenericType genericType;
public Multiplicity multiplicity;
Expand Down Expand Up @@ -69,7 +64,8 @@ public static class VariableDeserializer extends JsonDeserializer<ValueSpecifica
@Override
public ValueSpecification deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
ObjectCodec codec = jsonParser.getCodec();
JsonNode node = codec.readTree(jsonParser);
Variable variable = new Variable();
variable.name = node.get("name").asText();

Expand All @@ -83,19 +79,19 @@ public ValueSpecification deserialize(JsonParser jsonParser, DeserializationCont

else if (node.get("genericType") != null)
{
variable.genericType = om.treeToValue(node.get("genericType"), GenericType.class);
variable.genericType = codec.treeToValue(node.get("genericType"), GenericType.class);
}
if (node.get("multiplicity") != null)
{
variable.multiplicity = om.treeToValue(node.get("multiplicity"), Multiplicity.class);
variable.multiplicity = codec.treeToValue(node.get("multiplicity"), Multiplicity.class);
}
if (node.get("sourceInformation") != null)
{
variable.sourceInformation = om.treeToValue(node.get("sourceInformation"), SourceInformation.class);
variable.sourceInformation = codec.treeToValue(node.get("sourceInformation"), SourceInformation.class);
}
if (node.get("supportsStream") != null)
{
variable.supportsStream = om.treeToValue(node.get("supportsStream"), Boolean.class);
variable.supportsStream = codec.treeToValue(node.get("supportsStream"), Boolean.class);
}
return variable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static class ClassDeserializer extends JsonDeserializer<ValueSpecificatio
@Override
public ValueSpecification deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
return convert(jsonParser.getCodec().readTree(jsonParser));
return convert(jsonParser);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static class EnumDeserializer extends JsonDeserializer<ValueSpecification
@Override
public ValueSpecification deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
return convert(jsonParser.getCodec().readTree(jsonParser));
return convert(jsonParser);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.finos.legend.engine.protocol.pure.v1.model.valueSpecification.deprecated;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
Expand Down Expand Up @@ -47,13 +48,14 @@ public static class HackedClassDeserializer extends JsonDeserializer<ValueSpecif
@Override
public ValueSpecification deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
JsonNode node = jsonParser.getCodec().readTree(jsonParser);
ObjectCodec codec = jsonParser.getCodec();
JsonNode node = codec.readTree(jsonParser);
JsonNode name = node.get("fullPath");
ValueSpecification result = new GenericTypeInstance(name.asText());
JsonNode sourceInformation = node.get("sourceInformation");
if (sourceInformation != null)
{
result.sourceInformation = om.treeToValue(sourceInformation, SourceInformation.class);
result.sourceInformation = codec.treeToValue(sourceInformation, SourceInformation.class);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ValueSpecification deserialize(JsonParser jsonParser, DeserializationCont
JsonNode sourceInformation = node.get("sourceInformation");
if (sourceInformation != null)
{
result.sourceInformation = om.treeToValue(sourceInformation, SourceInformation.class);
result.sourceInformation = jsonParser.getCodec().treeToValue(sourceInformation, SourceInformation.class);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static class MappingInstanceDeserializer extends JsonDeserializer<ValueSp
@Override
public ValueSpecification deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
return convert(jsonParser.getCodec().readTree(jsonParser));
return convert(jsonParser);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
@JsonDeserialize(using = PrimitiveType.PrimitiveTypeDeserializer.class)
public class PrimitiveType extends PackageableElementPtr
{
private static ObjectMapper om = PureProtocolObjectMapperFactory.getNewObjectMapper();

private PrimitiveType()
{

Expand Down Expand Up @@ -64,7 +62,7 @@ public ValueSpecification deserialize(JsonParser jsonParser, DeserializationCont
JsonNode sourceInformation = node.get("sourceInformation");
if (sourceInformation != null)
{
result.sourceInformation = om.treeToValue(sourceInformation, SourceInformation.class);
result.sourceInformation = jsonParser.getCodec().treeToValue(sourceInformation, SourceInformation.class);
}
return result;
}
Expand Down
Loading

0 comments on commit 598321b

Please sign in to comment.