use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class CompactStreamSerializerTest method testWithExplicitSerializer_nested.
@Test
public void testWithExplicitSerializer_nested() {
SerializationConfig serializationConfig = new SerializationConfig();
CompactSerializationConfig compactSerializationConfig = serializationConfig.getCompactSerializationConfig();
compactSerializationConfig.setEnabled(true);
compactSerializationConfig.register(EmployeeDTO.class, "employee", new CompactSerializer<EmployeeDTO>() {
@Nonnull
@Override
public EmployeeDTO read(@Nonnull CompactReader in) {
return new EmployeeDTO(in.readInt32("a"), in.readInt64("i"));
}
@Override
public void write(@Nonnull CompactWriter out, @Nonnull EmployeeDTO object) {
out.writeInt32("a", object.getAge());
out.writeInt64("i", object.getId());
}
});
compactSerializationConfig.register(EmployerDTO.class, "employer", new CompactSerializer<EmployerDTO>() {
@Nonnull
@Override
public EmployerDTO read(@Nonnull CompactReader in) {
String name = in.readString("n");
String status = in.readString("hs");
int age = in.readInt32("a");
long[] ids = in.readArrayOfInt64("ids");
EmployeeDTO s = in.readCompact("s");
EmployeeDTO[] ss = in.readArrayOfCompact("ss", EmployeeDTO.class);
return new EmployerDTO(name, age, status == null ? null : HiringStatus.valueOf(status), ids, s, ss);
}
@Override
public void write(@Nonnull CompactWriter out, @Nonnull EmployerDTO object) {
out.writeString("n", object.getName());
out.writeString("hs", object.getHiringStatus() == null ? null : object.getHiringStatus().name());
out.writeInt32("a", object.getZcode());
out.writeArrayOfInt64("ids", object.getIds());
out.writeCompact("s", object.getSingleEmployee());
out.writeArrayOfCompact("ss", object.getOtherEmployees());
}
});
SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
long[] ids = new long[2];
ids[0] = 22;
ids[1] = 44;
EmployeeDTO[] employeeDTOS = new EmployeeDTO[5];
for (int j = 0; j < employeeDTOS.length; j++) {
employeeDTOS[j] = new EmployeeDTO(20 + j, j * 100);
}
EmployerDTO employerDTO = new EmployerDTO("nbss", 40, HIRING, ids, employeeDTO, employeeDTOS);
Data data = serializationService.toData(employerDTO);
Object object = serializationService.toObject(data);
EmployerDTO o = (EmployerDTO) object;
assertEquals(employerDTO, o);
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class CompactStreamSerializerTest method testOverridesJavaSerializationWhenRegisteredAsReflectivelySerializable.
@Test
public void testOverridesJavaSerializationWhenRegisteredAsReflectivelySerializable() {
SerializationConfig serializationConfig = new SerializationConfig();
serializationConfig.getCompactSerializationConfig().setEnabled(true).register(ExternalizableEmployeeDTO.class);
SerializationService serializationService = new DefaultSerializationServiceBuilder().setSchemaService(schemaService).setConfig(serializationConfig).build();
ExternalizableEmployeeDTO employeeDTO = new ExternalizableEmployeeDTO(30, "John Doe");
Data data = serializationService.toData(employeeDTO);
assertFalse(employeeDTO.usedExternalizableSerialization());
Object object = serializationService.toObject(data);
ExternalizableEmployeeDTO actual = (ExternalizableEmployeeDTO) object;
assertFalse(employeeDTO.usedExternalizableSerialization());
assertEquals(employeeDTO, actual);
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class CompactWithSchemaStreamSerializerTest method testFromObject.
@Test
public void testFromObject() {
SerializationService serializationService = createSerializationService();
EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
Data data = serializationService.toDataWithSchema(employeeDTO);
// Create a second schema service so that schemas are not shared accross these two
// This is to make sure that toObject call will use the schema in the data
SerializationService serializationService2 = createSerializationService();
EmployeeDTO actual = serializationService2.toObject(data);
assertEquals(employeeDTO, actual);
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class PortableTest method testRawDataWithoutRegistering.
@Test
public void testRawDataWithoutRegistering() {
final SerializationService serializationService = createSerializationService(1);
RawDataPortable p = new RawDataPortable(System.currentTimeMillis(), "test chars".toCharArray(), new NamedPortable("named portable", 34567), 9876, "Testing raw portable", new ByteArrayDataSerializable("test bytes".getBytes()));
final Data data = serializationService.toData(p);
assertEquals(p, serializationService.toObject(data));
}
use of com.hazelcast.internal.serialization.SerializationService in project hazelcast by hazelcast.
the class PortableTest method test_1096_ByteArrayContentSame.
// https://github.com/hazelcast/hazelcast/issues/1096
@Test
public void test_1096_ByteArrayContentSame() {
SerializationService ss = new DefaultSerializationServiceBuilder().addPortableFactory(PORTABLE_FACTORY_ID, new TestPortableFactory()).build();
assertRepeatedSerialisationGivesSameByteArrays(ss, new NamedPortable("issue-1096", 1096));
assertRepeatedSerialisationGivesSameByteArrays(ss, new InnerPortable(new byte[] { 0, 1, 2 }, new char[] { 'c', 'h', 'a', 'r' }, new short[] { 3, 4, 5 }, new int[] { 9, 8, 7, 6 }, new long[] { 0, 1, 5, 7, 9, 11 }, new float[] { 0.6543f, -3.56f, 45.67f }, new double[] { 456.456, 789.789, 321.321 }, new NamedPortable[] { new NamedPortable("issue-1096", 1096) }, new BigDecimal[] { new BigDecimal("12345"), new BigDecimal("123456") }, new LocalTime[] { LocalTime.now(), LocalTime.now() }, new LocalDate[] { LocalDate.now(), LocalDate.now() }, new LocalDateTime[] { LocalDateTime.now() }, new OffsetDateTime[] { OffsetDateTime.now() }));
assertRepeatedSerialisationGivesSameByteArrays(ss, new RawDataPortable(1096L, "issue-1096".toCharArray(), new NamedPortable("issue-1096", 1096), 1096, "issue-1096", new ByteArrayDataSerializable(new byte[1])));
}
Aggregations