|
| 1 | +/* |
| 2 | + * Copyright 2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.security.jackson2; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.JsonParser; |
| 20 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 21 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 22 | +import com.fasterxml.jackson.databind.JsonNode; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 25 | + |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +/** |
| 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. |
| 36 | + * |
| 37 | + * @param <T> the type of the unmodifiable collection, such as {@link List} or {@link Set}. |
| 38 | + * @author Hyunmin Choi |
| 39 | + */ |
| 40 | +abstract class AbstractUnmodifiableCollectionDeserializer<T> extends JsonDeserializer<T> { |
| 41 | + |
| 42 | + @Override |
| 43 | + public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { |
| 44 | + ObjectMapper mapper = (ObjectMapper) jp.getCodec(); |
| 45 | + JsonNode node = mapper.readTree(jp); |
| 46 | + return createUnmodifiableCollection(node, mapper); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * 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. |
| 54 | + * @return an unmodifiable collection with the deserialized elements. |
| 55 | + * @throws IOException if an error occurs during deserialization. |
| 56 | + */ |
| 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 | + } |
| 76 | + |
| 77 | +} |
0 commit comments