Search in sources :

Example 56 with SerializationService

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);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) Nonnull(javax.annotation.Nonnull) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) EmployerDTO(example.serialization.EmployerDTO) EmployeeDTO(example.serialization.EmployeeDTO) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 57 with SerializationService

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);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 58 with SerializationService

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);
}
Also used : EmployeeDTO(example.serialization.EmployeeDTO) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 59 with SerializationService

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));
}
Also used : InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) QuickTest(com.hazelcast.test.annotation.QuickTest) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) Test(org.junit.Test)

Example 60 with SerializationService

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])));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) OffsetDateTime(java.time.OffsetDateTime) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.internal.serialization.SerializationService) LocalDate(java.time.LocalDate) BigDecimal(java.math.BigDecimal) QuickTest(com.hazelcast.test.annotation.QuickTest) CustomSerializationTest(com.hazelcast.internal.serialization.impl.CustomSerializationTest) Test(org.junit.Test)

Aggregations

SerializationService (com.hazelcast.internal.serialization.SerializationService)170 Data (com.hazelcast.internal.serialization.Data)117 Test (org.junit.Test)100 QuickTest (com.hazelcast.test.annotation.QuickTest)99 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)59 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)50 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)43 ArrayList (java.util.ArrayList)15 SerializationConfig (com.hazelcast.config.SerializationConfig)14 HazelcastInstance (com.hazelcast.core.HazelcastInstance)13 Node (com.hazelcast.instance.impl.Node)12 CustomSerializationTest (com.hazelcast.internal.serialization.impl.CustomSerializationTest)12 Accessors.getNode (com.hazelcast.test.Accessors.getNode)12 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)11 EmployeeDTO (example.serialization.EmployeeDTO)11 ExternalizableEmployeeDTO (example.serialization.ExternalizableEmployeeDTO)11 PortableFactory (com.hazelcast.nio.serialization.PortableFactory)8 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8