View Javadoc
1   package org.djutils.serialization.serializers;
2   
3   import org.djutils.serialization.EndianUtil;
4   import org.djutils.serialization.SerializationException;
5   
6   /**
7    * Serializer for simple classes.
8    * <p>
9    * Copyright (c) 2019-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
10   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
11   * </p>
12   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
13   * @author <a href="https://www.tudelft.nl/staff/p.knoppers/">Peter Knoppers</a>
14   * @param <T> class
15   */
16  public abstract class ObjectSerializer<T extends Object> extends BasicSerializer<T>
17  {
18      /**
19       * Construct a new ObjectSerializer.
20       * @param type byte; the field type (returned by the <code>fieldType</code> method)
21       * @param dataClassName String; returned by the dataClassName method
22       */
23      public ObjectSerializer(final byte type, final String dataClassName)
24      {
25          super(type, dataClassName);
26      }
27  
28      @Override
29      public final int sizeWithPrefix(final T object) throws SerializationException
30      {
31          return 1 + size(object);
32      }
33  
34      @Override
35      public final void serializeWithPrefix(final T object, final byte[] buffer, final Pointer pointer,
36              final EndianUtil endianUtil) throws SerializationException
37      {
38          buffer[pointer.getAndIncrement(1)] = fieldType();
39          serialize(object, buffer, pointer, endianUtil);
40      }
41  
42      @Override
43      public final int getNumberOfDimensions()
44      {
45          return 0;
46      }
47  
48  }