Interface SerializationContextWrite


public sealed interface SerializationContextWrite
Context for writing objects and primitives during serialization.

This context is provided to SingleBinarySerializer.write(SerializationContextWrite, Object) and offers methods to write primitives and nested objects to the output stream.

Writing Primitives

Use the provided write methods for primitive types and strings:

@Override
public void write(SerializationContextWrite ctx, MyObject obj) {
    ctx.writeByte(obj.getByteValue());
    ctx.writeShort(obj.getShortValue());
    ctx.writeInt(obj.getIntValue());
    ctx.writeLong(obj.getLongValue());
    ctx.writeBoolean(obj.getBooleanValue());
    ctx.writeFloat(obj.getFloatValue());
    ctx.writeDouble(obj.getDoubleValue());
    ctx.writeUTF(obj.getStringValue());
    ctx.writeBytes(obj.getByteArray());
}

Writing Nested Objects

Use writeObject(Object) to write nested objects. The framework will automatically invoke the appropriate serializer for the object's type:

@Override
public void write(SerializationContextWrite ctx, Order order) {
    ctx.writeObject(order.getCustomer());  // Person object
    ctx.writeObject(order.getShippingAddress());  // Address object
    ctx.writeDouble(order.getTotal());
}

Null Values

writeObject(Object) handles null values automatically. You don't need to write a null flag manually:

// DON'T do this:
ctx.writeBoolean(obj.getAddress() != null);
if (obj.getAddress() != null) {
    ctx.writeObject(obj.getAddress());
}

// DO this instead:
ctx.writeObject(obj.getAddress());  // Null is handled automatically

Byte Arrays

For byte arrays, use writeBytes(byte[]) or writeBytes(byte[], int, int) for partial arrays:

@Override
public void write(SerializationContextWrite ctx, MyObject obj) {
    byte[] data = obj.getData();
    ctx.writeInt(data.length);  // Write length prefix
    ctx.writeBytes(data);       // Write the bytes
}

Strings

writeUTF(String) writes strings in UTF-8 encoding with a length prefix. Maximum string length is 32KB (Short.MAX_VALUE bytes after UTF-8 encoding):

ctx.writeUTF(obj.getName());  // Automatically length-prefixed

For strings longer than 32KB, write them as byte arrays:

byte[] utf8 = largeString.getBytes(StandardCharsets.UTF_8);
ctx.writeInt(utf8.length);
ctx.writeBytes(utf8);

Write Order

Critical: The order in which you write fields must exactly match the order in which you read them in SingleBinarySerializer.read(SerializationContextRead, byte).

Since:
2.0
Author:
José Bolina
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    writeBoolean(boolean v)
    Writes a boolean (1 byte).
    void
    writeByte(int v)
    Writes a single byte.
    void
    writeBytes(byte[] b)
    Writes all bytes from the array.
    void
    writeBytes(byte[] b, int off, int len)
    Writes a portion of a byte array.
    void
    writeDouble(double v)
    Writes a double (8 bytes).
    void
    writeFloat(float v)
    Writes a float (4 bytes).
    void
    writeInt(int v)
    Writes an int (4 bytes).
    void
    writeLong(long v)
    Writes a long (8 bytes).
    void
    Writes an object with automatic type identification.
    void
    writeShort(int v)
    Writes a short (2 bytes).
    void
    Writes a string in UTF-8 encoding with a length prefix.
  • Method Details

    • writeObject

      void writeObject(Object obj)
      Writes an object with automatic type identification.

      The object's type is determined, its type ID is written, and the appropriate serializer is invoked. Null values are handled automatically.

      Parameters:
      obj - The object to write (can be null)
      Throws:
      IllegalStateException - if no serializer is registered for the object's type
    • writeByte

      void writeByte(int v)
      Writes a single byte.
      Parameters:
      v - The byte value (only the lowest 8 bits are written)
    • writeShort

      void writeShort(int v)
      Writes a short (2 bytes).
      Parameters:
      v - The short value
    • writeInt

      void writeInt(int v)
      Writes an int (4 bytes).
      Parameters:
      v - The int value
    • writeLong

      void writeLong(long v)
      Writes a long (8 bytes).
      Parameters:
      v - The long value
    • writeBoolean

      void writeBoolean(boolean v)
      Writes a boolean (1 byte).
      Parameters:
      v - The boolean value
    • writeFloat

      void writeFloat(float v)
      Writes a float (4 bytes).
      Parameters:
      v - The float value
    • writeDouble

      void writeDouble(double v)
      Writes a double (8 bytes).
      Parameters:
      v - The double value
    • writeUTF

      void writeUTF(String s)
      Writes a string in UTF-8 encoding with a length prefix.

      Maximum string length is 32KB (Short.MAX_VALUE bytes after UTF-8 encoding).

      Parameters:
      s - The string to write
      Throws:
      IllegalArgumentException - if the UTF-8 encoded string exceeds 32KB
    • writeBytes

      void writeBytes(byte[] b)
      Writes all bytes from the array.
      Parameters:
      b - The byte array to write
    • writeBytes

      void writeBytes(byte[] b, int off, int len)
      Writes a portion of a byte array.
      Parameters:
      b - The byte array
      off - The starting offset in the array
      len - The number of bytes to write