Skip to content

Commit 48ff518

Browse files
jeetmp3Rob Winch
authored and
Rob Winch
committed
Fix Jackson 2.7+
UnmodifiableSetDeserializer added which will ensure Collection$UnmodifiableSet deserialize properly with jackson-databind 2.7+ Fixes gh-4073
1 parent 0b1e3b4 commit 48ff518

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.fasterxml.jackson.annotation.JsonCreator;
2020
import com.fasterxml.jackson.annotation.JsonTypeInfo;
21+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2122

2223
import java.util.Set;
2324

@@ -31,11 +32,13 @@
3132
* </pre>
3233
*
3334
* @author Jitendra Singh
35+
* @see UnmodifiableSetDeserializer
3436
* @see CoreJackson2Module
3537
* @see SecurityJacksonModules
3638
* @since 4.2
3739
*/
3840
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
41+
@JsonDeserialize(using = UnmodifiableSetDeserializer.class)
3942
class UnmodifiableSetMixin {
4043

4144
/**

0 commit comments

Comments
 (0)