Skip to content

Commit ed3c44f

Browse files
authored
Add explicit deserializer for ThreadGroup (#4959)
1 parent f96f60f commit ed3c44f

File tree

5 files changed

+69
-13
lines changed

5 files changed

+69
-13
lines changed

pom.xml

-2
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,6 @@
392392
<release>17</release>
393393
<compilerArgs>
394394
<arg>-parameters</arg>
395-
<arg>--add-opens=java.base/java.util=ALL-UNNAMED</arg>
396395
</compilerArgs>
397396
</configuration>
398397
</plugin>
@@ -457,7 +456,6 @@
457456
<release>21</release>
458457
<compilerArgs>
459458
<arg>-parameters</arg>
460-
<arg>--add-opens=java.base/java.util=ALL-UNNAMED</arg>
461459
</compilerArgs>
462460
</configuration>
463461
</plugin>

release-notes/VERSION-2.x

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Project: jackson-databind
4848
(reported by Floris W)
4949
#4953: Allow clearing all caches to avoid classloader leaks
5050
(contributed by Joren I)
51+
#4959: Add explicit deserializer for `ThreadGroup`
5152
5253
2.18.3 (not yet released)
5354

src/main/java/com/fasterxml/jackson/databind/deser/std/JdkDeserializers.java

+5-11
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,13 @@ public class JdkDeserializers
2424
AtomicLong.class,
2525
StackTraceElement.class,
2626
ByteBuffer.class,
27-
Void.class
27+
Void.class,
28+
ThreadGroup.class // @since 2.19
2829
};
2930
for (Class<?> cls : types) { _classNames.add(cls.getName()); }
3031
for (Class<?> cls : FromStringDeserializer.types()) { _classNames.add(cls.getName()); }
3132
}
3233

33-
/**
34-
* @deprecated Since 2.14 use the variant that takes one more argument
35-
*/
36-
@Deprecated // since 2.14
37-
public static JsonDeserializer<?> find(Class<?> rawType, String clsName)
38-
throws JsonMappingException
39-
{
40-
return find(null, rawType, clsName);
41-
}
42-
4334
/**
4435
* @since 2.14
4536
*/
@@ -73,6 +64,9 @@ public static JsonDeserializer<?> find(DeserializationContext ctxt,
7364
if (rawType == Void.class) {
7465
return NullifyingDeserializer.instance;
7566
}
67+
if (rawType == ThreadGroup.class) { // @since 2.19
68+
return new ThreadGroupDeserializer();
69+
}
7670
}
7771
return null;
7872
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.databind.deser.std;
2+
3+
import java.io.IOException;
4+
5+
import com.fasterxml.jackson.databind.DeserializationContext;
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
8+
/**
9+
* Deserializer for the {@link java.lang.ThreadGroup} class: due to limited access,
10+
* will only try to extract {@code "name"} property and ignores everything else.
11+
* This to match automatic serialization by Jackson which does write out
12+
* all accessible properties.
13+
*
14+
* @since 2.19
15+
*/
16+
public class ThreadGroupDeserializer
17+
extends StdNodeBasedDeserializer<ThreadGroup>
18+
{
19+
protected ThreadGroupDeserializer() {
20+
super(ThreadGroup.class);
21+
}
22+
23+
private static final long serialVersionUID = 1L;
24+
25+
@Override
26+
public ThreadGroup convert(JsonNode root, DeserializationContext ctxt) throws IOException {
27+
String name = root.path("name").asText();
28+
if (name == null) {
29+
name = "";
30+
}
31+
return new ThreadGroup(name);
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.fasterxml.jackson.databind.deser.jdk;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
import static org.junit.jupiter.api.Assertions.assertNotNull;
10+
11+
public class ThreadGroupDeserTest extends DatabindTestUtil
12+
{
13+
private final ObjectMapper MAPPER = sharedMapper();
14+
15+
// [databind#4939]
16+
@Test
17+
public void deserThreadGroupFromEmpty() throws Exception {
18+
ThreadGroup tg = MAPPER.readValue("{}", ThreadGroup.class);
19+
assertNotNull(tg);
20+
}
21+
22+
@Test
23+
public void roundtripThreadGroup() throws Exception {
24+
ThreadGroup tg = new ThreadGroup("testTG");
25+
String json = MAPPER.writeValueAsString(tg);
26+
ThreadGroup tg2 = MAPPER.readValue(json, ThreadGroup.class);
27+
assertNotNull(tg2);
28+
assertEquals(tg.getName(), tg2.getName());
29+
}
30+
}

0 commit comments

Comments
 (0)