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

The accessOrder field in LinkedHashMap is not deserialized correctly. #1157

Open
leonchen83 opened this issue Dec 23, 2024 · 1 comment
Open
Labels

Comments

@leonchen83
Copy link

leonchen83 commented Dec 23, 2024

Describe the bug
Since we set accessOrder to true during serialization, we expect the output order after deserialization to be a, b. However, due to the fact that the accessOrder field is not correctly deserialized, the actual output is b, a.

To Reproduce

public static void main(String[] args) {
    Kryo kryo = new Kryo();
    kryo.setReferences(true);
    kryo.setRegistrationRequired(true);
    kryo.register(LinkedHashMap.class);
    final Output output = new UnsafeOutput(512, 10240);
    
    LinkedHashMap<String, String> map = new LinkedHashMap<>(10, 0.85f, true);
    map.put("a", "a");
    map.put("b", "b");
    
    map.get("b");
    map.get("a");
    
    kryo.writeClassAndObject(output, map);
    LinkedHashMap<String, String> map1 = (LinkedHashMap) kryo.readClassAndObject(new UnsafeInput(output.toBytes()));
    
    map.get("a");
    map.get("b");
    
    for (String key : map1.keySet()) {
        System.out.println(key);
    }
}

Environment:

  • OS: windows11
  • JDK Version: 21
  • Kryo Version: 5.6.2

Additional context
Add any other context about the problem here.

@theigl
Copy link
Collaborator

theigl commented Jan 7, 2025

You are right. It is quite surprising that this has never come up before.

Kryo uses the default MapSerializer for LinkedHashMap that is completely unaware of this parameter. Your best option is to register a custom serializer that writes an additional boolean flag. See TreeMapSerializer for a serializer that extends MapSerializer with additional data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants