Skip to content

This project provides efficient serialization/deserialization of objects into bytes.

License

Notifications You must be signed in to change notification settings

Hotmoka/io-hotmoka-marshalling

Repository files navigation

Java-Build Action Status Maven Central License

Efficient serialization/deserialization of objects into bytes in Java

This project provides efficient serialization/deserialization of objects into bytes. The resulting array of bytes is in general shorter than what is obtained with standard Java serialization, but it requires to specify explicitly how to marshal and unmarshal the objects.

One can defined a marshallable object by subclassing the AbstractMarshallable class, defining a method into() for marshalling an object into bytes and a constructor (or a factory method) for unmarshalling the object from bytes:

public class MyMarshallable extends AbstractMarshallable {
  private final String name;
  private final String surname;
  private final int yearOfBirth;

  public MyMarshallable(String name, String surname, int yearOfBirth) {
    this.name = name;
    this.surname = surname;
    this.yearOfBirth = yearOfBirth;
  }

  public MyMarshallable(UnmarshallingContext context) throws IOException {
    // data must be unmarshalled in the same order as it was marshalled inside the into() method
    this(context.readStringShared(), context.readStringShared(), context.readCompactInt());
  }

  @Override
  public void into(MarshallingContext context) throws IOException {
    // by writing shared strings, we could reduce the size of the marshalled data
    // if there are repeated strings marshalled into the same context
    context.writeStringShared(name);
    context.writeStringShared(surname);
    context.writeCompactInt(yearOfBirth);
  }

  @Override
  public boolean equals(Object other) {
    return other instanceof MyMarshallable mm &&
      Objects.equals(name, mm.name) && Objects.equals(surname, mm.surname) && yearOfBirth == mm.yearOfBirth;
  }
}

The following testcase shows how a MyMarshallable can be marshalled into bytes and later unmarshalled from bytes:

@Test
@DisplayName("MyMarshallable marshal unmarshal works")
public void myMarshallableMarshalUnmarshalWorks() throws IOException {
  MyMarshallable expected = new MyMarshallable("Albert", "Einstein", 1879);

  ByteArrayOutputStream stream;
  try (var baos = stream = new ByteArrayOutputStream(); var context = MarshallingContexts.of(baos)) {
    expected.into(context);
  }

  byte[] marshalled = stream.toByteArray();

  try (var bais = new ByteArrayInputStream(marshalled); var context = UnmarshallingContexts.of(bais)) {
    MyMarshallable actual = new MyMarshallable(context);
    assertEquals(expected, actual);
  }
}

This documentation is licensed under a Creative Commons Attribution 4.0 International License

This document is licensed under a Creative Commons Attribution 4.0 International License.

Copyright 2024 by Fausto Spoto ([email protected])

About

This project provides efficient serialization/deserialization of objects into bytes.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages