Skip to content

Commit c53ee19

Browse files
committed
Polish Abstract Deserializer
1 parent fee79cc commit c53ee19

File tree

3 files changed

+30
-49
lines changed

3 files changed

+30
-49
lines changed

core/src/main/java/org/springframework/security/jackson2/AbstractUnmodifiableCollectionDeserializer.java

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,27 @@
1616

1717
package org.springframework.security.jackson2;
1818

19+
import java.io.IOException;
20+
import java.util.ArrayList;
21+
import java.util.Collection;
22+
import java.util.List;
23+
import java.util.Set;
24+
1925
import com.fasterxml.jackson.core.JsonParser;
2026
import com.fasterxml.jackson.databind.DeserializationContext;
2127
import com.fasterxml.jackson.databind.JsonDeserializer;
2228
import com.fasterxml.jackson.databind.JsonNode;
2329
import com.fasterxml.jackson.databind.ObjectMapper;
2430
import com.fasterxml.jackson.databind.node.ArrayNode;
2531

26-
import java.io.IOException;
27-
import java.util.Collection;
28-
import java.util.List;
29-
import java.util.Set;
30-
3132
/**
32-
* Abstract base class for deserializers that create unmodifiable collections from JSON data.
33-
* Subclasses like {@link UnmodifiableListDeserializer} and
34-
* {@link UnmodifiableSetDeserializer} should implement the method to define the
35-
* specific collection type and handle the deserialization logic.
33+
* Abstract base class for deserializers that create unmodifiable collections from JSON
34+
* data. Subclasses like {@link UnmodifiableListDeserializer} and
35+
* {@link UnmodifiableSetDeserializer} should implement the method to define the specific
36+
* collection type and handle the deserialization logic.
3637
*
37-
* @param <T> the type of the unmodifiable collection, such as {@link List} or {@link Set}.
38+
* @param <T> the type of the unmodifiable collection, such as {@link List} or
39+
* {@link Set}.
3840
* @author Hyunmin Choi
3941
*/
4042
abstract class AbstractUnmodifiableCollectionDeserializer<T> extends JsonDeserializer<T> {
@@ -43,35 +45,24 @@ abstract class AbstractUnmodifiableCollectionDeserializer<T> extends JsonDeseria
4345
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
4446
ObjectMapper mapper = (ObjectMapper) jp.getCodec();
4547
JsonNode node = mapper.readTree(jp);
46-
return createUnmodifiableCollection(node, mapper);
48+
Collection<Object> values = new ArrayList<>();
49+
if (node instanceof ArrayNode arrayNode) {
50+
for (JsonNode elementNode : arrayNode) {
51+
values.add(mapper.readValue(elementNode.traverse(mapper), Object.class));
52+
}
53+
}
54+
else if (node != null) {
55+
values.add(mapper.readValue(node.traverse(mapper), Object.class));
56+
}
57+
return createUnmodifiableCollection(values);
4758
}
4859

4960
/**
5061
* Creates an unmodifiable collection from the given JSON node.
51-
*
52-
* @param node the JSON node containing the data to be deserialized.
53-
* @param mapper the {@link ObjectMapper} used to deserialize JSON data.
62+
* @param values the values to add to the unmodifiable collection
5463
* @return an unmodifiable collection with the deserialized elements.
5564
* @throws IOException if an error occurs during deserialization.
5665
*/
57-
protected abstract T createUnmodifiableCollection(JsonNode node, ObjectMapper mapper) throws IOException;
58-
59-
/**
60-
* Adds elements from the JSON node to the provided collection.
61-
*
62-
* @param node the JSON node containing the elements to add.
63-
* @param mapper the {@link ObjectMapper} used for deserialization.
64-
* @param collection the collection to which elements are added.
65-
* @throws IOException if an error occurs during deserialization.
66-
*/
67-
protected void addElements(JsonNode node, ObjectMapper mapper, Collection<Object> collection) throws IOException {
68-
if (node instanceof ArrayNode arrayNode) {
69-
for (JsonNode elementNode : arrayNode) {
70-
collection.add(mapper.readValue(elementNode.traverse(mapper), Object.class));
71-
}
72-
} else if (node != null) {
73-
collection.add(mapper.readValue(node.traverse(mapper), Object.class));
74-
}
75-
}
66+
abstract T createUnmodifiableCollection(Collection<Object> values) throws IOException;
7667

7768
}

core/src/main/java/org/springframework/security/jackson2/UnmodifiableListDeserializer.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616

1717
package org.springframework.security.jackson2;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.fasterxml.jackson.databind.ObjectMapper;
21-
22-
import java.io.IOException;
2319
import java.util.ArrayList;
20+
import java.util.Collection;
2421
import java.util.Collections;
2522
import java.util.List;
2623

@@ -35,10 +32,8 @@
3532
class UnmodifiableListDeserializer extends AbstractUnmodifiableCollectionDeserializer<List> {
3633

3734
@Override
38-
protected List createUnmodifiableCollection(JsonNode node, ObjectMapper mapper) throws IOException {
39-
List<Object> result = new ArrayList<>();
40-
addElements(node, mapper, result);
41-
return Collections.unmodifiableList(result);
35+
List createUnmodifiableCollection(Collection<Object> values) {
36+
return Collections.unmodifiableList(new ArrayList<>(values));
4237
}
4338

4439
}

core/src/main/java/org/springframework/security/jackson2/UnmodifiableSetDeserializer.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616

1717
package org.springframework.security.jackson2;
1818

19-
import com.fasterxml.jackson.databind.JsonNode;
20-
import com.fasterxml.jackson.databind.ObjectMapper;
21-
22-
import java.io.IOException;
19+
import java.util.Collection;
2320
import java.util.Collections;
2421
import java.util.HashSet;
2522
import java.util.Set;
@@ -35,10 +32,8 @@
3532
class UnmodifiableSetDeserializer extends AbstractUnmodifiableCollectionDeserializer<Set> {
3633

3734
@Override
38-
protected Set createUnmodifiableCollection(JsonNode node, ObjectMapper mapper) throws IOException {
39-
Set<Object> resultSet = new HashSet<>();
40-
addElements(node, mapper, resultSet);
41-
return Collections.unmodifiableSet(resultSet);
35+
Set createUnmodifiableCollection(Collection<Object> values) {
36+
return Collections.unmodifiableSet(new HashSet<>(values));
4237
}
4338

4439
}

0 commit comments

Comments
 (0)