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

How to use TreeMapSerializer ? #767

Closed
Mr14huashao opened this issue Sep 17, 2020 · 4 comments
Closed

How to use TreeMapSerializer ? #767

Mr14huashao opened this issue Sep 17, 2020 · 4 comments
Labels

Comments

@Mr14huashao
Copy link

Mr14huashao commented Sep 17, 2020

Kryo doesn't serialize the TreeMapSerializer comparator.
How to reproduce:

        HashMap map = new HashMap();
	map.put(1,122);
	map.put(2,314);
	map.put(4,2511);
	map.put(8,"hello");
	map.put(10,"wasted");
	map.put(3,"Great");
	TreeMap treeMap = new TreeMap(Comparator.reverseOrder());
	treeMap.putAll(map);
           
        final Kryo kryo = new Kryo();
	kryo.register(TreeMap.class, new DefaultSerializers.TreeMapSerializer());
	kryo.writeClassAndObject(output, treeMap);
	Object o = kryo.readClassAndObject(new Input(output.getBuffer()));

Did I use it wrong?
Thanks in advance

@Mr14huashao Mr14huashao changed the title How to use TreeMapSerializer comparator? How to use TreeMapSerializer ? Sep 17, 2020
@theigl
Copy link
Collaborator

theigl commented Sep 17, 2020

@Mr14huashao: What happens? An error message? Is the comparator null after deserialization?

I'm guessing the problem is with Comparator.reverseOrder() and not the TreeMapSerializer. reverseOrder() is basically a singleton and maybe can't be instantiated correctly.

@theigl
Copy link
Collaborator

theigl commented Sep 18, 2020

You have to register the comparator as well. This test passes:

@Test
public void testTreeMapWithReverseComparator () {
  kryo.register(TreeMap.class);
  kryo.register(Comparator.reverseOrder().getClass());

  HashMap<Integer, Integer> map = new HashMap<>();
  map.put(1, 122);
  map.put(2, 314);
  map.put(4, 2511);

  TreeMap<Integer, Integer> treeMap = new TreeMap<>(Comparator.reverseOrder());
  treeMap.putAll(map);

  roundTrip(18, treeMap);
}

@Mr14huashao
Copy link
Author

Thanks for your help! @theigl. Your code is available.
Now I want to add a ConcurrentSkipListMapSerializer.
Do you think it is necessary?

@Mr14huashao
Copy link
Author

@theigl Thank you very much for your reply and check.
I update #763 and submit a new PR #768
Welcome to the PR to provide your valuable comments.

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