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

[Java] add forward serializer #276

Merged
merged 1 commit into from
May 19, 2023
Merged

[Java] add forward serializer #276

merged 1 commit into from
May 19, 2023

Conversation

chaokunyang
Copy link
Collaborator

@chaokunyang chaokunyang commented May 18, 2023

What do these changes do?

This PR a ForwardSerializer to support forward serialization to other serialization libraries. In this way, the serialization lib switch will be more convinient

Example:

ForwardSerializer serializer = new ForwardSerializer(
    new ForwardSerializer.SerializerProxy<Kryo>() {
      private final ThreadLocal<Output> outputLocal =
          ThreadLocal.withInitial(() -> new Output(32, Integer.MAX_VALUE));

      @Override
      protected Kryo newSerializer() {
        // We can register custom serializers here.
        return new Kryo();
      }

      @Override
      protected void register(Kryo serializer, Class clz) {
        serializer.register(clz);
      }

      @Override
      protected byte[] serialize(Kryo serializer, Object obj) {
        Output output = outputLocal.get();
        output.clear();
        serializer.writeClassAndObject(output, obj);
        return output.toBytes();
      }

      @Override
      protected Object deserialize(Kryo serializer, byte[] bytes) {
        Input input = new Input(bytes);
        return serializer.readClassAndObject(input);
      }
    });

ForwardSerializer kryo = createSerializer("Kryo");
assertEquals(kryo.deserialize(kryo.serialize(beanA)), beanA);

Related issue number

Closes #277

Check code requirements

  • tests added / passed (if needed)
  • Ensure all linting tests pass, see here for how to run them

@chaokunyang chaokunyang merged commit d971d29 into main May 19, 2023
5 checks passed
@chaokunyang chaokunyang deleted the add_forward_serializer branch May 19, 2023 02:16
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

Successfully merging this pull request may close these issues.

[Java] support forward serialization to other libraries
1 participant