16
16
17
17
package org .springframework .security .jackson2 ;
18
18
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
+
19
25
import com .fasterxml .jackson .core .JsonParser ;
20
26
import com .fasterxml .jackson .databind .DeserializationContext ;
21
27
import com .fasterxml .jackson .databind .JsonDeserializer ;
22
28
import com .fasterxml .jackson .databind .JsonNode ;
23
29
import com .fasterxml .jackson .databind .ObjectMapper ;
24
30
import com .fasterxml .jackson .databind .node .ArrayNode ;
25
31
26
- import java .io .IOException ;
27
- import java .util .Collection ;
28
- import java .util .List ;
29
- import java .util .Set ;
30
-
31
32
/**
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.
36
37
*
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}.
38
40
* @author Hyunmin Choi
39
41
*/
40
42
abstract class AbstractUnmodifiableCollectionDeserializer <T > extends JsonDeserializer <T > {
@@ -43,35 +45,24 @@ abstract class AbstractUnmodifiableCollectionDeserializer<T> extends JsonDeseria
43
45
public T deserialize (JsonParser jp , DeserializationContext ctxt ) throws IOException {
44
46
ObjectMapper mapper = (ObjectMapper ) jp .getCodec ();
45
47
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 );
47
58
}
48
59
49
60
/**
50
61
* 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
54
63
* @return an unmodifiable collection with the deserialized elements.
55
64
* @throws IOException if an error occurs during deserialization.
56
65
*/
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 ;
76
67
77
68
}
0 commit comments