Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception thrown when Collections.synchronizedList() is serialized with type info, deserialized #3009

Closed
pcloves opened this issue Jan 6, 2021 · 3 comments
Milestone

Comments

@pcloves
Copy link

pcloves commented Jan 6, 2021

Describe the bug
Similar to #1823,When deserialized a Collections.synchronizedList, InvalidDefinitionException is thrown

Version information
2.11.0

To Reproduce

public class TestClass{

    public List<Integer> list = Collections.synchronizedList(Arrays.asList(1, 2));

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        String s = objectMapper.writeValueAsString(new TestClass());

        TestClass testClass = objectMapper.readValue(s, TestClass.class);

        System.out.println(testClass);
    }
}

Expected behavior

  1. deserialized success.
  2. or is there a alternative method to custom the target serialized list type, such as ArrayList to avoiding deserialized exception?

Actual behavior

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.util.Collections$SynchronizedRandomAccessList` (no Creators, like default constructor, exist): no default no-arguments constructor found
 at [Source: (String)"{"@class":"com.cyou.fusion.tetris.plugin.pojo.db.global.TestClass","list":["java.util.Collections$SynchronizedRandomAccessList",[1,2]]}"; line: 1, column: 129] (through reference chain: com.cyou.fusion.tetris.plugin.pojo.db.global.TestClass["list"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1611)
	at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1077)
	at com.fasterxml.jackson.databind.deser.ValueInstantiator.createUsingDefault(ValueInstantiator.java:189)
	at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createUsingDefault(StdValueInstantiator.java:267)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.createDefaultInstance(CollectionDeserializer.java:254)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:244)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:27)
	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:120)
	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:53)
	at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserializeWithType(CollectionDeserializer.java:313)
	at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:147)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:293)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:194)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:166)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:132)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:99)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:1197)
	at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:68)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)
	at com.cyou.fusion.tetris.plugin.pojo.db.global.TestClass.main(TestClass.java:20)
Disconnected from the target VM, address: '127.0.0.1:65066', transport: 'socket'

Process finished with exit code 1

Additional context
None.

@pcloves pcloves added the to-evaluate Issue that has been received but not yet evaluated label Jan 6, 2021
@cowtowncoder
Copy link
Member

JDK version?

@pcloves
Copy link
Author

pcloves commented Jan 7, 2021

JDK version?

The issue is reproduced both on jdk 1.8 and jdk 11.

C:\Users\zhengtongshan>java -version
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
D:\Software\Java\jdk-11.0.3\bin>java -version
java version "11.0.3" 2019-04-16 LTS
Java(TM) SE Runtime Environment 18.9 (build 11.0.3+12-LTS)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11.0.3+12-LTS, mixed mode)

@cowtowncoder
Copy link
Member

@pcloves thank you for verification.

@cowtowncoder cowtowncoder removed the to-evaluate Issue that has been received but not yet evaluated label Jan 8, 2021
@cowtowncoder cowtowncoder modified the milestones: 2.10.0, 2.12.1 Jan 8, 2021
@cowtowncoder cowtowncoder changed the title Exception thrown when Collections.synchronizedList is deserialized. Exception thrown when Collections.synchronizedList() is serialized with type info, deserialized Jan 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants