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

Unable to register serializers due to visibility problems #923

Closed
gzsombor opened this issue Oct 18, 2022 · 3 comments · Fixed by #933
Closed

Unable to register serializers due to visibility problems #923

gzsombor opened this issue Oct 18, 2022 · 3 comments · Fixed by #933

Comments

@gzsombor
Copy link

Describe the bug
I need to serialize objects which contains fields, which are initialized with java.util.List.of(a,b,c,d) Kyro throws this exception:

Caused by: java.lang.IllegalArgumentException: Class is not registered: java.util.ImmutableCollections$ListN
Note: To register this class use: kryo.register(java.util.ImmutableCollections.ListN.class);

I don't want to kryo.setRegistrationRequired(false), as - in my understanding - could lead to less performance, and potential incompatibilities. However, I can't really register serializer by itself, the best that I could came up with:

        kryo.register(List.of(1, 2, 3, 4).getClass(), 14);
        kryo.register(List.of(1).getClass(), 15);
        kryo.register(List.of().getClass(), 16);
        kryo.register(List.class, new CollectionSerializer<List<Object>>() {
            {
                setAcceptsNull(false);
            }
            @Override
            protected List<Object> create (Kryo kryo, Input input, Class<? extends List<Object>> type, int size) {
                return new ArrayList<>(size);
            }

        }, 17);

It seems to be working, but having helper functions on ImmutableCollectionsSerializers, which would do this initialization would be super helpful

To Reproduce
Provide a minimal reproducible example of the problem, ideally in the form of a runnable test-case.


record X(List<String> values) {}

var kryo = new Kryo();
kryo.writeClassAndObject(output, new X(List.of("hello", "world")));

Environment:

  • OS: Any
  • JDK Version: 17
  • Kryo Version: 5.3.0

Additional context
Add any other context about the problem here.

@theigl
Copy link
Collaborator

theigl commented Oct 30, 2022

@gzsombor: I agree, registering immutable collections is quite cumbersome at the moment. I'm open to adding helper functions to ImmutableCollectionsSerializers. Do you think we should add 3 separate functions for lists, sets, and maps or a single function?

It would be great if you could provide a PR.

@theigl
Copy link
Collaborator

theigl commented Dec 21, 2022

@gzsombor: I created #933 to address this issue. Is this solution OK for you?

@gzsombor
Copy link
Author

Sorry to not sending PR, but your change looks good to me!

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

Successfully merging a pull request may close this issue.

2 participants