|
| 1 | +/* |
| 2 | + * Copyright 2015-2016 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 | + * http://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.core.JsonProcessingException; |
| 21 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 22 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 23 | +import com.fasterxml.jackson.databind.JsonNode; |
| 24 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 25 | +import com.fasterxml.jackson.databind.node.ArrayNode; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.HashSet; |
| 30 | +import java.util.Iterator; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +/** |
| 34 | + * Custom deserializer for {@link UnmodifiableSetMixin}. |
| 35 | + * |
| 36 | + * @author Jitendra Singh |
| 37 | + * @see UnmodifiableSetMixin |
| 38 | + * @since 4.2 |
| 39 | + */ |
| 40 | +class UnmodifiableSetDeserializer extends JsonDeserializer<Set> { |
| 41 | + |
| 42 | + @Override |
| 43 | + public Set deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { |
| 44 | + ObjectMapper mapper = (ObjectMapper) jp.getCodec(); |
| 45 | + JsonNode node = mapper.readTree(jp); |
| 46 | + Set<Object> resultSet = new HashSet<Object>(); |
| 47 | + if (node != null) { |
| 48 | + if (node instanceof ArrayNode) { |
| 49 | + ArrayNode arrayNode = (ArrayNode) node; |
| 50 | + Iterator<JsonNode> nodeIterator = arrayNode.iterator(); |
| 51 | + while (nodeIterator.hasNext()) { |
| 52 | + JsonNode elementNode = nodeIterator.next(); |
| 53 | + resultSet.add(mapper.readValue(elementNode.toString(), Object.class)); |
| 54 | + } |
| 55 | + } else { |
| 56 | + resultSet.add(mapper.readValue(node.toString(), Object.class)); |
| 57 | + } |
| 58 | + } |
| 59 | + return Collections.unmodifiableSet(resultSet); |
| 60 | + } |
| 61 | +} |
0 commit comments