1616
1717package 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+
1925import com .fasterxml .jackson .core .JsonParser ;
2026import com .fasterxml .jackson .databind .DeserializationContext ;
2127import com .fasterxml .jackson .databind .JsonDeserializer ;
2228import com .fasterxml .jackson .databind .JsonNode ;
2329import com .fasterxml .jackson .databind .ObjectMapper ;
2430import 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 */
4042abstract 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}
0 commit comments