1 package org.djutils.serialization.serializers;
2
3 import org.djunits.unit.Unit;
4 import org.djunits.value.vfloat.scalar.base.FloatScalar;
5 import org.djutils.serialization.EndianUtil;
6 import org.djutils.serialization.FieldTypes;
7 import org.djutils.serialization.SerializationException;
8
9
10
11
12
13
14
15
16
17
18
19 public class FloatScalarSerializer<U extends Unit<U>, S extends FloatScalar<U, S>>
20 extends ObjectWithUnitSerializer<U, S>
21 {
22
23 public FloatScalarSerializer()
24 {
25 super(FieldTypes.FLOAT_32_UNIT, "Djunits_FloatScalar");
26 }
27
28 @Override
29 public int size(final S afs) throws SerializationException
30 {
31 return 2 + 4;
32 }
33
34 @Override
35 public void serialize(final S afs, final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil)
36 throws SerializationException
37 {
38 encodeUnit(afs.getDisplayUnit(), buffer, pointer, endianUtil);
39 float v = afs.getSI();
40 endianUtil.encodeFloat(v, buffer, pointer.getAndIncrement(4));
41 }
42
43 @Override
44 public S deSerialize(final byte[] buffer, final Pointer pointer, final EndianUtil endianUtil) throws SerializationException
45 {
46 U unit = getUnit(buffer, pointer, endianUtil);
47 S afs = FloatScalar.instantiateAnonymous(endianUtil.decodeFloat(buffer, pointer.getAndIncrement(4)),
48 unit.getStandardUnit());
49 afs.setDisplayUnit(unit);
50 return afs;
51 }
52
53 }