|
6 | 6 | import com.fasterxml.jackson.annotation.JsonIncludeProperties;
|
7 | 7 | import com.fasterxml.jackson.annotation.ObjectIdGenerator;
|
8 | 8 | import com.fasterxml.jackson.annotation.ObjectIdGenerators;
|
| 9 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 10 | +import com.fasterxml.jackson.core.JsonParser; |
| 11 | +import com.fasterxml.jackson.core.TokenStreamFactory; |
9 | 12 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
|
10 | 13 |
|
11 | 14 | import com.fasterxml.jackson.databind.*;
|
|
20 | 23 | import com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer;
|
21 | 24 | import com.fasterxml.jackson.databind.ser.std.MapSerializer;
|
22 | 25 | import com.fasterxml.jackson.databind.ser.std.StdDelegatingSerializer;
|
| 26 | +import com.fasterxml.jackson.databind.ser.std.ToEmptyObjectSerializer; |
23 | 27 | import com.fasterxml.jackson.databind.type.ReferenceType;
|
24 | 28 | import com.fasterxml.jackson.databind.util.BeanUtil;
|
25 | 29 | import com.fasterxml.jackson.databind.util.ClassUtil;
|
@@ -380,12 +384,15 @@ protected JsonSerializer<Object> constructBeanOrAddOnSerializer(SerializerProvid
|
380 | 384 | return prov.getUnknownTypeSerializer(Object.class);
|
381 | 385 | // throw new IllegalArgumentException("Cannot create bean serializer for Object.class");
|
382 | 386 | }
|
383 |
| - |
384 | 387 | JsonSerializer<?> ser = _findUnsupportedTypeSerializer(prov, type, beanDesc);
|
385 | 388 | if (ser != null) {
|
386 | 389 | return (JsonSerializer<Object>) ser;
|
387 | 390 | }
|
388 |
| - |
| 391 | + // 02-Sep-2021, tatu: [databind#3244] Should not try "proper" serialization of |
| 392 | + // things like ObjectMapper, JsonParser or JsonGenerator... |
| 393 | + if (_isUnserializableJacksonType(prov, type)) { |
| 394 | + return new ToEmptyObjectSerializer(type); |
| 395 | + } |
389 | 396 | final SerializationConfig config = prov.getConfig();
|
390 | 397 | BeanSerializerBuilder builder = constructBeanSerializerBuilder(beanDesc);
|
391 | 398 | builder.setConfig(config);
|
@@ -854,4 +861,23 @@ protected JsonSerializer<?> _findUnsupportedTypeSerializer(SerializerProvider ct
|
854 | 861 | }
|
855 | 862 | return null;
|
856 | 863 | }
|
| 864 | + |
| 865 | + /* Helper method used for preventing attempts to serialize various Jackson |
| 866 | + * processor things which are not generally serializable. |
| 867 | + * |
| 868 | + * @since 2.13 |
| 869 | + */ |
| 870 | + protected boolean _isUnserializableJacksonType(SerializerProvider ctxt, |
| 871 | + JavaType type) |
| 872 | + { |
| 873 | + final Class<?> raw = type.getRawClass(); |
| 874 | + return ObjectMapper.class.isAssignableFrom(raw) |
| 875 | + || ObjectReader.class.isAssignableFrom(raw) |
| 876 | + || ObjectWriter.class.isAssignableFrom(raw) |
| 877 | + || DatabindContext.class.isAssignableFrom(raw) |
| 878 | + || TokenStreamFactory.class.isAssignableFrom(raw) |
| 879 | + || JsonParser.class.isAssignableFrom(raw) |
| 880 | + || JsonGenerator.class.isAssignableFrom(raw) |
| 881 | + ; |
| 882 | + } |
857 | 883 | }
|
0 commit comments