Skip to content

Commit e38907e

Browse files
committed
Fix a small issue with id of SimpleModule; also change return types, ctors
1 parent 5ce12da commit e38907e

File tree

5 files changed

+93
-87
lines changed

5 files changed

+93
-87
lines changed

src/main/java/com/fasterxml/jackson/databind/cfg/MapperBuilderState.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class MapperBuilderState
5858
/**
5959
* Modules registered in registration order, if any; `null` if none.
6060
*/
61-
protected final Object[] _modules;
61+
protected final com.fasterxml.jackson.databind.Module[] _modules;
6262

6363
/*
6464
/**********************************************************************
@@ -154,11 +154,11 @@ public MapperBuilderState(MapperBuilder<?,?> src)
154154
}
155155
}
156156

157-
private static Object[] _toArray(Collection<?> coll)
157+
private static com.fasterxml.jackson.databind.Module[] _toArray(Collection<?> coll)
158158
{
159159
if (coll == null || coll.isEmpty()) {
160160
return null;
161161
}
162-
return coll.toArray(new Object[coll.size()]);
162+
return coll.toArray(new com.fasterxml.jackson.databind.Module[coll.size()]);
163163
}
164164
}

src/main/java/com/fasterxml/jackson/databind/module/SimpleModule.java

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package com.fasterxml.jackson.databind.module;
22

3-
import java.util.Collection;
4-
import java.util.HashMap;
5-
import java.util.LinkedHashSet;
6-
import java.util.List;
7-
import java.util.Map;
3+
import java.util.*;
84

95
import com.fasterxml.jackson.core.Version;
106
import com.fasterxml.jackson.databind.*;
117
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
128
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
139
import com.fasterxml.jackson.databind.jsontype.NamedType;
1410
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
11+
import com.fasterxml.jackson.databind.util.UniqueId;
1512

1613
/**
1714
* Vanilla {@link Module} implementation that allows registration
@@ -48,6 +45,9 @@ public class SimpleModule
4845
/**
4946
* Unique id generated to avoid instances from ever matching so all
5047
* registrations succeed.
48+
*<p>
49+
* NOTE! Id should be {@link java.io.Serializable} to allow serialization
50+
* of mapper instances.
5151
*
5252
* @since 3.0
5353
*/
@@ -108,7 +108,7 @@ public SimpleModule() {
108108
"SimpleModule-"+System.identityHashCode(this)
109109
: getClass().getName();
110110
_version = Version.unknownVersion();
111-
_id = new Object();
111+
_id = _createId();
112112
}
113113

114114
/**
@@ -139,32 +139,13 @@ public SimpleModule(Version version) {
139139
public SimpleModule(String name, Version version) {
140140
_name = name;
141141
_version = version;
142-
_id = new Object();
143-
}
144-
145-
public SimpleModule(String name, Version version,
146-
Map<Class<?>,JsonDeserializer<?>> deserializers) {
147-
this(name, version, deserializers, null);
148-
}
149-
150-
public SimpleModule(String name, Version version,
151-
List<JsonSerializer<?>> serializers) {
152-
this(name, version, null, serializers);
142+
_id = _createId();
153143
}
154144

155-
public SimpleModule(String name, Version version,
156-
Map<Class<?>,JsonDeserializer<?>> deserializers,
157-
List<JsonSerializer<?>> serializers)
158-
{
159-
_name = name;
160-
_version = version;
161-
_id = new Object();
162-
if (deserializers != null) {
163-
_deserializers = new SimpleDeserializers(deserializers);
164-
}
165-
if (serializers != null) {
166-
_serializers = new SimpleSerializers(serializers);
167-
}
145+
// 27-Feb-2018, tatu: Need to create Registration Id that never matches any
146+
// other id, but is serializable
147+
protected Object _createId() {
148+
return new UniqueId();
168149
}
169150

170151
/*
@@ -195,43 +176,49 @@ public Object getRegistrationId() {
195176
/**
196177
* Resets all currently configured serializers.
197178
*/
198-
public void setSerializers(SimpleSerializers s) {
179+
public SimpleModule setSerializers(SimpleSerializers s) {
199180
_serializers = s;
181+
return this;
200182
}
201183

202184
/**
203185
* Resets all currently configured deserializers.
204186
*/
205-
public void setDeserializers(SimpleDeserializers d) {
187+
public SimpleModule setDeserializers(SimpleDeserializers d) {
206188
_deserializers = d;
189+
return this;
207190
}
208191

209192
/**
210193
* Resets all currently configured key serializers.
211194
*/
212-
public void setKeySerializers(SimpleSerializers ks) {
195+
public SimpleModule setKeySerializers(SimpleSerializers ks) {
213196
_keySerializers = ks;
197+
return this;
214198
}
215199

216200
/**
217201
* Resets all currently configured key deserializers.
218202
*/
219-
public void setKeyDeserializers(SimpleKeyDeserializers kd) {
203+
public SimpleModule setKeyDeserializers(SimpleKeyDeserializers kd) {
220204
_keyDeserializers = kd;
205+
return this;
221206
}
222207

223208
/**
224209
* Resets currently configured abstract type mappings
225210
*/
226-
public void setAbstractTypes(SimpleAbstractTypeResolver atr) {
211+
public SimpleModule setAbstractTypes(SimpleAbstractTypeResolver atr) {
227212
_abstractTypes = atr;
213+
return this;
228214
}
229215

230216
/**
231217
* Resets all currently configured value instantiators
232218
*/
233-
public void setValueInstantiators(SimpleValueInstantiators svi) {
219+
public SimpleModule setValueInstantiators(SimpleValueInstantiators svi) {
234220
_valueInstantiators = svi;
221+
return this;
235222
}
236223

237224
public SimpleModule setDeserializerModifier(BeanDeserializerModifier mod) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fasterxml.jackson.databind.util;
2+
3+
/**
4+
* Simple identity value class that may be used as Serializable key
5+
* for entries that need to retain identity of some kind, but where
6+
* actual appearance of id itself does not matter.
7+
*
8+
* @since 3.0
9+
*/
10+
public class UniqueId
11+
implements java.io.Serializable, Comparable<UniqueId>
12+
{
13+
private static final long serialVersionUID = 3L;
14+
15+
protected final String _id;
16+
17+
public UniqueId() {
18+
_id = Long.toHexString(System.identityHashCode(this));
19+
}
20+
21+
@Override
22+
public boolean equals(Object other) {
23+
return this == other;
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return _id.hashCode();
29+
}
30+
31+
@Override
32+
public int compareTo(UniqueId o) {
33+
return _id.compareTo(o._id);
34+
}
35+
}

src/test/java/com/fasterxml/jackson/databind/TestJDKSerialization.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
import com.fasterxml.jackson.annotation.JsonAnyGetter;
77
import com.fasterxml.jackson.annotation.JsonAnySetter;
8+
import com.fasterxml.jackson.core.Version;
89
import com.fasterxml.jackson.databind.DeserializationConfig;
910
import com.fasterxml.jackson.databind.JavaType;
1011
import com.fasterxml.jackson.databind.ObjectMapper;
1112
import com.fasterxml.jackson.databind.ObjectReader;
1213
import com.fasterxml.jackson.databind.ObjectWriter;
1314
import com.fasterxml.jackson.databind.SerializationConfig;
15+
import com.fasterxml.jackson.databind.module.SimpleModule;
1416
import com.fasterxml.jackson.databind.type.TypeFactory;
1517
import com.fasterxml.jackson.databind.util.SimpleLookupCache;
1618

@@ -176,6 +178,33 @@ public void testObjectMapper() throws IOException
176178
assertEquals(p.y, p2.y);
177179
}
178180

181+
public void testMapperWithModule() throws IOException
182+
{
183+
SimpleModule module = new SimpleModule("JDKSerTestModule", Version.unknownVersion());
184+
{
185+
byte[] b = jdkSerialize(module);
186+
assertNotNull(b);
187+
}
188+
189+
ObjectMapper mapper = ObjectMapper.builder()
190+
.enable(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS)
191+
.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
192+
.addModule(module)
193+
.build();
194+
195+
// just force serialization first
196+
final String EXP_JSON = "{\"x\":2,\"y\":3}";
197+
final MyPojo p = new MyPojo(2, 3);
198+
assertEquals(EXP_JSON, mapper.writeValueAsString(p));
199+
200+
byte[] bytes = jdkSerialize(mapper);
201+
ObjectMapper mapper2 = jdkDeserialize(bytes);
202+
assertEquals(EXP_JSON, mapper2.writeValueAsString(p));
203+
MyPojo p2 = mapper2.readValue(EXP_JSON, MyPojo.class);
204+
assertEquals(p.x, p2.x);
205+
assertEquals(p.y, p2.y);
206+
}
207+
179208
public void testTypeFactory() throws Exception
180209
{
181210
TypeFactory orig = TypeFactory.defaultInstance();

src/test/java/com/fasterxml/jackson/databind/module/SimpleModuleArgCheckTest.java

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.fasterxml.jackson.databind.module;
22

3-
import java.util.Collections;
4-
import java.util.List;
5-
import java.util.Map;
6-
73
import com.fasterxml.jackson.core.Version;
84
import com.fasterxml.jackson.databind.*;
95
import com.fasterxml.jackson.databind.jsontype.NamedType;
@@ -18,9 +14,7 @@ public class SimpleModuleArgCheckTest extends BaseMapTest
1814

1915
public void testInvalidForDeserializers() throws Exception
2016
{
21-
SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
22-
(Map<Class<?>,JsonDeserializer<?>>) null);
23-
17+
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
2418
try {
2519
mod.addDeserializer(String.class, null);
2620
fail("Should not pass");
@@ -36,39 +30,6 @@ public void testInvalidForDeserializers() throws Exception
3630
}
3731
}
3832

39-
/*
40-
/**********************************************************
41-
/* Unit tests for invalid serializers
42-
/**********************************************************
43-
*/
44-
45-
public void testInvalidForSerializers() throws Exception
46-
{
47-
SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
48-
(List<JsonSerializer<?>>) null);
49-
50-
try {
51-
mod.addSerializer(String.class, null);
52-
fail("Should not pass");
53-
} catch (IllegalArgumentException e) {
54-
verifyException(e, "Cannot pass `null` as serializer");
55-
}
56-
57-
try {
58-
mod.addSerializer((JsonSerializer<?>) null);
59-
fail("Should not pass");
60-
} catch (IllegalArgumentException e) {
61-
verifyException(e, "Cannot pass `null` as serializer");
62-
}
63-
64-
try {
65-
mod.addKeySerializer(String.class, null);
66-
fail("Should not pass");
67-
} catch (IllegalArgumentException e) {
68-
verifyException(e, "Cannot pass `null` as key serializer");
69-
}
70-
}
71-
7233
/*
7334
/**********************************************************
7435
/* Unit tests for invalid misc other
@@ -77,12 +38,7 @@ public void testInvalidForSerializers() throws Exception
7738

7839
public void testInvalidAbstractTypeMapping() throws Exception
7940
{
80-
// just for funsies let's use more esoteric constructor
81-
Map<Class<?>,JsonDeserializer<?>> desers = Collections.emptyMap();
82-
List<JsonSerializer<?>> sers = Collections.emptyList();
83-
SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
84-
desers, sers);
85-
41+
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
8642
try {
8743
mod.addAbstractTypeMapping(null, String.class);
8844
fail("Should not pass");
@@ -99,8 +55,7 @@ public void testInvalidAbstractTypeMapping() throws Exception
9955

10056
public void testInvalidSubtypeMappings() throws Exception
10157
{
102-
SimpleModule mod = new SimpleModule("test", Version.unknownVersion(),
103-
null, null);
58+
SimpleModule mod = new SimpleModule("test", Version.unknownVersion());
10459
try {
10560
mod.registerSubtypes(String.class, null);
10661
fail("Should not pass");

0 commit comments

Comments
 (0)