Search in sources :

Example 26 with SerializationConfig

use of com.hazelcast.config.SerializationConfig 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 27 with SerializationConfig

use of com.hazelcast.config.SerializationConfig 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 28 with SerializationConfig

use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.

the class CompactReflectiveSerializerUnsupportedFieldsTest method createSerializationService.

@Before
public void createSerializationService() {
    CompactSerializationConfig compactSerializationConfig = new CompactSerializationConfig();
    compactSerializationConfig.setEnabled(true);
    service = new DefaultSerializationServiceBuilder().setSchemaService(schemaService).setConfig(new SerializationConfig().setCompactSerializationConfig(compactSerializationConfig)).build();
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) Before(org.junit.Before)

Example 29 with SerializationConfig

use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.

the class SerializerHookLoaderTest method testLoad_withParametrizedConstructor.

@Test
public void testLoad_withParametrizedConstructor() {
    SerializerConfig serializerConfig = new SerializerConfig();
    serializerConfig.setClassName("com.hazelcast.internal.serialization.impl.TestSerializerHook$TestSerializerWithTypeConstructor");
    serializerConfig.setTypeClassName("com.hazelcast.internal.serialization.impl.SampleIdentifiedDataSerializable");
    SerializationConfig serializationConfig = getConfig().getSerializationConfig();
    serializationConfig.addSerializerConfig(serializerConfig);
    SerializerHookLoader hook = new SerializerHookLoader(serializationConfig, classLoader);
    Map<Class, Object> serializers = hook.getSerializers();
    TestSerializerHook.TestSerializerWithTypeConstructor serializer = (TestSerializerHook.TestSerializerWithTypeConstructor) serializers.get(SampleIdentifiedDataSerializable.class);
    assertEquals(SampleIdentifiedDataSerializable.class, serializer.getClazz());
}
Also used : SerializerConfig(com.hazelcast.config.SerializerConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 30 with SerializationConfig

use of com.hazelcast.config.SerializationConfig in project hazelcast by hazelcast.

the class SerializerHookLoaderTest method testLoad_withDefaultConstructor.

@Test
public void testLoad_withDefaultConstructor() {
    SerializerConfig serializerConfig = new SerializerConfig();
    serializerConfig.setClassName("com.hazelcast.internal.serialization.impl.TestSerializerHook$TestSerializer");
    serializerConfig.setTypeClassName("com.hazelcast.internal.serialization.impl.SampleIdentifiedDataSerializable");
    SerializationConfig serializationConfig = getConfig().getSerializationConfig();
    serializationConfig.addSerializerConfig(serializerConfig);
    SerializerHookLoader hook = new SerializerHookLoader(serializationConfig, classLoader);
    Map<Class, Object> serializers = hook.getSerializers();
    assertNotNull(serializers);
}
Also used : SerializerConfig(com.hazelcast.config.SerializerConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

SerializationConfig (com.hazelcast.config.SerializationConfig)73 Test (org.junit.Test)45 QuickTest (com.hazelcast.test.annotation.QuickTest)37 DefaultSerializationServiceBuilder (com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder)36 CompactSerializationConfig (com.hazelcast.config.CompactSerializationConfig)23 SerializerConfig (com.hazelcast.config.SerializerConfig)21 InternalSerializationService (com.hazelcast.internal.serialization.InternalSerializationService)21 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)21 Data (com.hazelcast.internal.serialization.Data)18 SerializationService (com.hazelcast.internal.serialization.SerializationService)14 GlobalSerializerConfig (com.hazelcast.config.GlobalSerializerConfig)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)11 ClientConfig (com.hazelcast.client.config.ClientConfig)8 ClassDefinitionBuilder (com.hazelcast.nio.serialization.ClassDefinitionBuilder)7 GenericRecord (com.hazelcast.nio.serialization.GenericRecord)7 ObjectDataInput (com.hazelcast.nio.ObjectDataInput)6 ObjectDataOutput (com.hazelcast.nio.ObjectDataOutput)6 SerializationService (com.hazelcast.spi.serialization.SerializationService)6 ExternalizableEmployeeDTO (example.serialization.ExternalizableEmployeeDTO)6 Config (com.hazelcast.config.Config)5