Skip to content

Commit

Permalink
Rollback usage of incoming object codec (#3283)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbey authored Nov 29, 2024
1 parent 39e5c0c commit e9b21ac
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package org.finos.legend.engine.protocol;

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.node.ArrayNode;
import com.fasterxml.jackson.databind.node.BooleanNode;
import com.fasterxml.jackson.databind.node.DoubleNode;
Expand All @@ -27,6 +27,7 @@
import com.fasterxml.jackson.databind.node.LongNode;
import com.fasterxml.jackson.databind.node.NullNode;
import com.fasterxml.jackson.databind.node.TextNode;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
Expand All @@ -38,6 +39,7 @@

public class AnyDeserializer extends JsonDeserializer<Object>
{
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private final List<Class<?>> classes;

public AnyDeserializer(List<Class<?>> classes)
Expand All @@ -48,9 +50,9 @@ public AnyDeserializer(List<Class<?>> classes)
@Override
public Object deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException
{
JsonNode node = jsonParser.readValueAsTree();
JsonNode node = OBJECT_MAPPER.readTree(jsonParser);

Object value = deserialize(jsonParser.getCodec(), node);
Object value = deserialize(node);

if (value == null && !(node instanceof NullNode))
{
Expand All @@ -60,26 +62,26 @@ public Object deserialize(JsonParser jsonParser, DeserializationContext deserial
return value;
}

private List<Object> deserialize(ObjectCodec codec, ArrayNode node)
private List<Object> deserialize(ArrayNode node)
{
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(node.elements(), Spliterator.ORDERED), false).map(node1 -> deserialize(codec, node1)).collect(Collectors.toList());
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(node.elements(), Spliterator.ORDERED), false).map(this::deserialize).collect(Collectors.toList());
}

private Object deserialize(ObjectCodec codec, JsonNode node)
private Object deserialize(JsonNode node)
{
if (node instanceof ArrayNode)
{
return deserialize(codec, (ArrayNode) node);
return deserialize((ArrayNode) node);
}

if (node instanceof NullNode)
{
return null;
}

for (Class<?> clazz : this.classes)
for (Class clazz : this.classes)
{
Object value = tryDeserialize(codec, node, clazz);
Object value = tryDeserialize(node, clazz);
if (value != null)
{
return value;
Expand All @@ -89,7 +91,7 @@ private Object deserialize(ObjectCodec codec, JsonNode node)
throw new RuntimeException(String.format("Failed to deserialize '%s' to types [%s]", node, this.classes));
}

private Object tryDeserialize(ObjectCodec codec, JsonNode node, Class<?> clazz)
private Object tryDeserialize(JsonNode node, Class clazz)
{
try
{
Expand All @@ -113,12 +115,12 @@ private Object tryDeserialize(ObjectCodec codec, JsonNode node, Class<?> clazz)
{
return null;
}
return codec.treeToValue(node, clazz);
return OBJECT_MAPPER.treeToValue(node, clazz);

}
catch (Exception e)
{
return null;
}
}
}
}

0 comments on commit e9b21ac

Please sign in to comment.