Skip to content

Commit c37038a

Browse files
committed
[6262] Update Deserializer
Bug: #6262 Signed-off-by: Michaël Charfadi <michael.charfadi@obeosoft.com>
1 parent cb294ab commit c37038a

File tree

22 files changed

+241
-258
lines changed

22 files changed

+241
-258
lines changed

packages/charts/backend/sirius-components-collaborative-charts/src/main/java/org/eclipse/sirius/components/collaborative/charts/HierarchyDeserializer.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2022, 2023 Obeo.
2+
* Copyright (c) 2022, 2026 Obeo.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -12,11 +12,6 @@
1212
*******************************************************************************/
1313
package org.eclipse.sirius.components.collaborative.charts;
1414

15-
import com.fasterxml.jackson.core.JsonProcessingException;
16-
import com.fasterxml.jackson.databind.JsonNode;
17-
import com.fasterxml.jackson.databind.ObjectMapper;
18-
import com.fasterxml.jackson.databind.node.ObjectNode;
19-
2015
import java.util.Objects;
2116
import java.util.Optional;
2217

@@ -27,6 +22,11 @@
2722
import org.slf4j.Logger;
2823
import org.slf4j.LoggerFactory;
2924
import org.springframework.stereotype.Service;
25+
import tools.jackson.core.JacksonException;
26+
import tools.jackson.core.JsonParser;
27+
import tools.jackson.databind.DeserializationContext;
28+
import tools.jackson.databind.JsonNode;
29+
import tools.jackson.databind.node.ObjectNode;
3030

3131
/**
3232
* Used to deserialize the hierarchy representation.
@@ -46,12 +46,10 @@ public HierarchyDeserializer(IURLParser urlParser) {
4646

4747
@Override
4848
public boolean canHandle(ObjectNode root) {
49-
// @formatter:off
5049
return Optional.ofNullable(root.get("kind"))
5150
.map(JsonNode::asText)
5251
.filter(this::isHierarchyRepresentation)
5352
.isPresent();
54-
// @formatter:on
5553
}
5654

5755
private boolean isHierarchyRepresentation(String kind) {
@@ -60,10 +58,10 @@ private boolean isHierarchyRepresentation(String kind) {
6058
}
6159

6260
@Override
63-
public Optional<IRepresentation> handle(ObjectMapper mapper, ObjectNode root) {
61+
public Optional<IRepresentation> handle(JsonParser jsonParser, DeserializationContext context, ObjectNode root) {
6462
try {
65-
return Optional.of(mapper.readValue(root.toString(), Hierarchy.class));
66-
} catch (JsonProcessingException exception) {
63+
return Optional.of(context.readTreeAsValue(root, Hierarchy.class));
64+
} catch (JacksonException exception) {
6765
this.logger.warn(exception.getMessage(), exception);
6866
}
6967

packages/core/backend/sirius-components-collaborative/src/main/java/org/eclipse/sirius/components/collaborative/api/IRepresentationDeserializer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2020 Obeo.
2+
* Copyright (c) 2019, 2026 Obeo.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -12,12 +12,12 @@
1212
*******************************************************************************/
1313
package org.eclipse.sirius.components.collaborative.api;
1414

15-
import com.fasterxml.jackson.databind.ObjectMapper;
16-
import com.fasterxml.jackson.databind.node.ObjectNode;
17-
1815
import java.util.Optional;
1916

2017
import org.eclipse.sirius.components.representations.IRepresentation;
18+
import tools.jackson.core.JsonParser;
19+
import tools.jackson.databind.DeserializationContext;
20+
import tools.jackson.databind.node.ObjectNode;
2121

2222
/**
2323
* Used to deserialize a representation with Jackson.
@@ -28,6 +28,6 @@ public interface IRepresentationDeserializer {
2828

2929
boolean canHandle(ObjectNode root);
3030

31-
Optional<IRepresentation> handle(ObjectMapper mapper, ObjectNode root);
31+
Optional<IRepresentation> handle(JsonParser jsonParser, DeserializationContext context, ObjectNode root);
3232

33-
}
33+
}

packages/core/backend/sirius-components-collaborative/src/main/java/org/eclipse/sirius/components/collaborative/api/IStdDeserializerProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2020 Obeo.
2+
* Copyright (c) 2019, 2026 Obeo.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -12,7 +12,8 @@
1212
*******************************************************************************/
1313
package org.eclipse.sirius.components.collaborative.api;
1414

15-
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
15+
16+
import tools.jackson.databind.deser.std.StdDeserializer;
1617

1718
/**
1819
* Interface used to register deserializer.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2023 Obeo.
2+
* Copyright (c) 2019, 2026 Obeo.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -12,33 +12,29 @@
1212
*******************************************************************************/
1313
package org.eclipse.sirius.components.collaborative.representations;
1414

15-
import com.fasterxml.jackson.core.JsonParser;
16-
import com.fasterxml.jackson.core.ObjectCodec;
17-
import com.fasterxml.jackson.databind.DeserializationContext;
18-
import com.fasterxml.jackson.databind.ObjectMapper;
19-
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
20-
import com.fasterxml.jackson.databind.node.ObjectNode;
21-
22-
import java.io.IOException;
15+
import java.io.Serializable;
2316
import java.util.List;
2417
import java.util.Objects;
2518

2619
import org.eclipse.sirius.components.collaborative.api.IRepresentationDeserializer;
2720
import org.eclipse.sirius.components.representations.IRepresentation;
21+
import tools.jackson.core.JacksonException;
22+
import tools.jackson.core.JsonParser;
23+
import tools.jackson.databind.DeserializationContext;
24+
import tools.jackson.databind.deser.std.StdDeserializer;
25+
import tools.jackson.databind.node.ObjectNode;
2826

2927
/**
3028
* Custom deserializer to customize the ObjectMapper.
3129
*
3230
* @author gcoutable
3331
*/
34-
public class RepresentationStdDeserializer extends StdDeserializer<IRepresentation> {
35-
36-
private static final long serialVersionUID = -1759653601179599083L;
32+
public class RepresentationStdDeserializer extends StdDeserializer<IRepresentation> implements Serializable {
3733

3834
private final List<IRepresentationDeserializer> representationDeserializers;
3935

4036
public RepresentationStdDeserializer(List<IRepresentationDeserializer> representationDeserializers) {
41-
this(null, representationDeserializers);
37+
this(IRepresentation.class, representationDeserializers);
4238
}
4339

4440
public RepresentationStdDeserializer(Class<?> valueClass, List<IRepresentationDeserializer> representationDeserializers) {
@@ -47,20 +43,13 @@ public RepresentationStdDeserializer(Class<?> valueClass, List<IRepresentationDe
4743
}
4844

4945
@Override
50-
public IRepresentation deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException {
51-
IRepresentation representation = null;
52-
ObjectCodec objectCodec = jsonParser.getCodec();
53-
if (objectCodec instanceof ObjectMapper mapper) {
54-
ObjectNode root = mapper.readTree(jsonParser);
55-
56-
// @formatter:off
57-
representation = this.representationDeserializers.stream()
58-
.filter(representationDeserializer -> representationDeserializer.canHandle(root))
59-
.findFirst()
60-
.flatMap(representationDeserializer -> representationDeserializer.handle(mapper, root))
61-
.orElse(null);
62-
// @formatter:on
63-
}
64-
return representation;
46+
public IRepresentation deserialize(JsonParser jsonParser, DeserializationContext context) throws JacksonException {
47+
ObjectNode root = (ObjectNode) context.readTree(jsonParser);
48+
49+
return this.representationDeserializers.stream()
50+
.filter(representationDeserializer -> representationDeserializer.canHandle(root))
51+
.findFirst()
52+
.flatMap(representationDeserializer -> representationDeserializer.handle(jsonParser, context, root))
53+
.orElse(null);
6554
}
66-
}
55+
}

packages/core/backend/sirius-components-collaborative/src/main/java/org/eclipse/sirius/components/collaborative/representations/RepresentationStdDeserializerProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019, 2020 Obeo.
2+
* Copyright (c) 2019, 2026 Obeo.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -12,7 +12,7 @@
1212
*******************************************************************************/
1313
package org.eclipse.sirius.components.collaborative.representations;
1414

15-
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
15+
1616

1717
import java.util.List;
1818
import java.util.Objects;
@@ -21,6 +21,7 @@
2121
import org.eclipse.sirius.components.collaborative.api.IStdDeserializerProvider;
2222
import org.eclipse.sirius.components.representations.IRepresentation;
2323
import org.springframework.stereotype.Service;
24+
import tools.jackson.databind.deser.std.StdDeserializer;
2425

2526
/**
2627
* The deserializer provider for {@link IRepresentation}.

packages/deck/backend/sirius-components-collaborative-deck/src/main/java/org/eclipse/sirius/components/collaborative/deck/service/DeckDeserializer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2023 Obeo.
2+
* Copyright (c) 2023, 2026 Obeo.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -12,11 +12,6 @@
1212
*******************************************************************************/
1313
package org.eclipse.sirius.components.collaborative.deck.service;
1414

15-
import com.fasterxml.jackson.core.JsonProcessingException;
16-
import com.fasterxml.jackson.databind.JsonNode;
17-
import com.fasterxml.jackson.databind.ObjectMapper;
18-
import com.fasterxml.jackson.databind.node.ObjectNode;
19-
2015
import java.util.Optional;
2116

2217
import org.eclipse.sirius.components.collaborative.api.IRepresentationDeserializer;
@@ -25,6 +20,11 @@
2520
import org.slf4j.Logger;
2621
import org.slf4j.LoggerFactory;
2722
import org.springframework.stereotype.Service;
23+
import tools.jackson.core.JacksonException;
24+
import tools.jackson.core.JsonParser;
25+
import tools.jackson.databind.DeserializationContext;
26+
import tools.jackson.databind.JsonNode;
27+
import tools.jackson.databind.node.ObjectNode;
2828

2929
/**
3030
* Used to deserialize a deck diagram.
@@ -45,10 +45,10 @@ public boolean canHandle(ObjectNode root) {
4545
}
4646

4747
@Override
48-
public Optional<IRepresentation> handle(ObjectMapper mapper, ObjectNode root) {
48+
public Optional<IRepresentation> handle(JsonParser jsonParser, DeserializationContext context, ObjectNode root) {
4949
try {
50-
return Optional.of(mapper.readValue(root.toString(), Deck.class));
51-
} catch (JsonProcessingException exception) {
50+
return Optional.of(context.readTreeAsValue(root, Deck.class));
51+
} catch (JacksonException exception) {
5252
this.logger.warn(exception.getMessage(), exception);
5353
}
5454

packages/diagrams/backend/sirius-components-collaborative-diagrams/src/main/java/org/eclipse/sirius/components/collaborative/diagrams/ILayoutStrategyDeserializer.java

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2022, 2023 Obeo.
2+
* Copyright (c) 2022, 2026 Obeo.
33
* This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -12,54 +12,50 @@
1212
*******************************************************************************/
1313
package org.eclipse.sirius.components.collaborative.diagrams;
1414

15-
import com.fasterxml.jackson.core.JacksonException;
16-
import com.fasterxml.jackson.core.JsonParser;
17-
import com.fasterxml.jackson.core.ObjectCodec;
18-
import com.fasterxml.jackson.databind.DeserializationContext;
19-
import com.fasterxml.jackson.databind.ObjectMapper;
20-
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
21-
import com.fasterxml.jackson.databind.node.ObjectNode;
22-
23-
import java.io.IOException;
15+
import java.io.Serializable;
2416

2517
import org.eclipse.sirius.components.diagrams.FreeFormLayoutStrategy;
2618
import org.eclipse.sirius.components.diagrams.ILayoutStrategy;
2719
import org.eclipse.sirius.components.diagrams.ListLayoutStrategy;
20+
import tools.jackson.core.JacksonException;
21+
import tools.jackson.core.JsonParser;
22+
import tools.jackson.databind.DeserializationContext;
23+
import tools.jackson.databind.JsonNode;
24+
import tools.jackson.databind.deser.std.StdDeserializer;
25+
import tools.jackson.databind.node.ObjectNode;
2826

2927
/**
3028
* Custom deserializer for layout strategy since jackson need to know how to find the concrete class matching the JSON
3129
* data.
3230
*
3331
* @author gcoutable
3432
*/
35-
public class ILayoutStrategyDeserializer extends StdDeserializer<ILayoutStrategy> {
36-
37-
private static final long serialVersionUID = -4765219889537073762L;
33+
public class ILayoutStrategyDeserializer extends StdDeserializer<ILayoutStrategy> implements Serializable {
3834

3935
public ILayoutStrategyDeserializer() {
40-
this(null);
36+
this(ILayoutStrategy.class);
4137
}
4238

4339
public ILayoutStrategyDeserializer(Class<?> valueClass) {
4440
super(valueClass);
4541
}
4642

4743
@Override
48-
public ILayoutStrategy deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JacksonException {
44+
public ILayoutStrategy deserialize(JsonParser jsonParser, DeserializationContext context) throws JacksonException {
4945
ILayoutStrategy layoutStrategy = null;
50-
ObjectCodec objectCodec = jsonParser.getCodec();
51-
if (objectCodec instanceof ObjectMapper mapper) {
52-
ObjectNode root = mapper.readTree(jsonParser);
53-
switch (root.get("kind").asText()) {
54-
case ListLayoutStrategy.KIND:
55-
layoutStrategy = mapper.readValue(root.toString(), ListLayoutStrategy.class);
56-
break;
57-
case FreeFormLayoutStrategy.KIND:
58-
layoutStrategy = mapper.readValue(root.toString(), FreeFormLayoutStrategy.class);
59-
break;
60-
default:
61-
break;
62-
}
46+
JsonNode rootNode = context.readTree(jsonParser);
47+
ObjectNode root = (ObjectNode) rootNode;
48+
49+
switch (root.get("kind").asText()) {
50+
case ListLayoutStrategy.KIND:
51+
layoutStrategy = context.readTreeAsValue(root, ListLayoutStrategy.class);
52+
break;
53+
case FreeFormLayoutStrategy.KIND:
54+
layoutStrategy = context.readTreeAsValue(root, FreeFormLayoutStrategy.class);
55+
break;
56+
default:
57+
break;
58+
6359
}
6460
return layoutStrategy;
6561
}

0 commit comments

Comments
 (0)