Search in sources :

Example 61 with SerializationConfig

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

the class GenericRecordTest method testGenericRecordToStringValidJson.

@Test
public void testGenericRecordToStringValidJson() throws IOException {
    CompactSerializationConfig compactSerializationConfig = new CompactSerializationConfig();
    compactSerializationConfig.setEnabled(true);
    InternalSerializationService serializationService = new DefaultSerializationServiceBuilder().setSchemaService(schemaService).setConfig(new SerializationConfig().setCompactSerializationConfig(compactSerializationConfig)).build();
    MainDTO expectedDTO = createMainDTO();
    expectedDTO.nullableBool = null;
    expectedDTO.p.localDateTimes[0] = null;
    Data data = serializationService.toData(expectedDTO);
    assertTrue(data.isCompact());
    // internal generic record created on the servers on query
    InternalGenericRecord internalGenericRecord = serializationService.readAsInternalGenericRecord(data);
    String string = internalGenericRecord.toString();
    Json.parse(string);
    // generic record read from a remote instance without classes on the classpath
    List<String> excludes = Collections.singletonList("example.serialization");
    FilteringClassLoader classLoader = new FilteringClassLoader(excludes, null);
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(classLoader);
        InternalSerializationService ss2 = new DefaultSerializationServiceBuilder().setSchemaService(schemaService).setClassLoader(classLoader).setConfig(new SerializationConfig().setCompactSerializationConfig(new CompactSerializationConfig())).build();
        GenericRecord genericRecord = ss2.toObject(data);
        Json.parse(genericRecord.toString());
        // generic record build by API
        GenericRecord apiGenericRecord = createCompactGenericRecord(expectedDTO);
        Json.parse(apiGenericRecord.toString());
    } finally {
        Thread.currentThread().setContextClassLoader(contextClassLoader);
    }
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) MainDTO(example.serialization.MainDTO) CompactTestUtil.createMainDTO(com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createMainDTO) FilteringClassLoader(com.hazelcast.internal.util.FilteringClassLoader) FilteringClassLoader(com.hazelcast.internal.util.FilteringClassLoader) Data(com.hazelcast.internal.serialization.Data) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) GenericRecord(com.hazelcast.nio.serialization.GenericRecord) CompactTestUtil.createCompactGenericRecord(com.hazelcast.internal.serialization.impl.compact.CompactTestUtil.createCompactGenericRecord) InternalGenericRecord(com.hazelcast.internal.serialization.impl.InternalGenericRecord) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 62 with SerializationConfig

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

the class CompactStreamSerializerTest method createSerializationService.

private InternalSerializationService createSerializationService() {
    CompactSerializationConfig compactSerializationConfig = new CompactSerializationConfig();
    compactSerializationConfig.setEnabled(true);
    return 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)

Example 63 with SerializationConfig

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

the class CompactStreamSerializerTest method testSchemaEvolution_fieldAdded.

@Test
public void testSchemaEvolution_fieldAdded() {
    SerializationConfig serializationConfig = new SerializationConfig();
    // Using this registration to mimic schema evolution. This is usage is not advised.
    serializationConfig.getCompactSerializationConfig().setEnabled(true).register(EmployeeDTO.class, EmployeeDTO.class.getName(), new CompactSerializer<EmployeeDTO>() {

        @Nonnull
        @Override
        public EmployeeDTO read(@Nonnull CompactReader in) {
            throw new UnsupportedOperationException("We will not read from here on this test");
        }

        @Override
        public void write(@Nonnull CompactWriter out, @Nonnull EmployeeDTO object) {
            out.writeInt32("age", object.getAge());
            out.writeInt64("id", object.getId());
            out.writeString("surname", "sir");
        }
    });
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setSchemaService(schemaService).build();
    EmployeeDTO expected = new EmployeeDTO(20, 102310312);
    Data data = serializationService.toData(expected);
    SerializationConfig serializationConfig2 = new SerializationConfig();
    serializationConfig2.getCompactSerializationConfig().setEnabled(true);
    SerializationService serializationService2 = new DefaultSerializationServiceBuilder().setSchemaService(schemaService).setConfig(serializationConfig2).build();
    EmployeeDTO actual = serializationService2.toObject(data);
    assertEquals(expected.getAge(), actual.getAge());
    assertEquals(expected.getId(), actual.getId());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) CompactWriter(com.hazelcast.nio.serialization.compact.CompactWriter) EmployeeDTO(example.serialization.EmployeeDTO) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) CompactReader(com.hazelcast.nio.serialization.compact.CompactReader) 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) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 64 with SerializationConfig

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

the class CompactStreamSerializerTest method testWithExplicitSerializer.

@Test
public void testWithExplicitSerializer() {
    SerializationConfig serializationConfig = new SerializationConfig();
    serializationConfig.getCompactSerializationConfig().setEnabled(true).register(EmployeeDTO.class, "employee", new EmployeeDTOSerializer());
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setSchemaService(schemaService).setConfig(serializationConfig).build();
    EmployeeDTO employeeDTO = new EmployeeDTO(30, 102310312);
    Data data = serializationService.toData(employeeDTO);
    Object object = serializationService.toObject(data);
    EmployeeDTO actual = (EmployeeDTO) object;
    assertEquals(employeeDTO, actual);
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) EmployeeDTO(example.serialization.EmployeeDTO) ExternalizableEmployeeDTO(example.serialization.ExternalizableEmployeeDTO) SerializationConfig(com.hazelcast.config.SerializationConfig) CompactSerializationConfig(com.hazelcast.config.CompactSerializationConfig) EmployeeDTOSerializer(example.serialization.EmployeeDTOSerializer) 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 65 with SerializationConfig

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

the class CompactWithSchemaStreamSerializerTest method createSerializationService.

private InternalSerializationService createSerializationService() {
    SchemaService schemaService = CompactTestUtil.createInMemorySchemaService();
    CompactSerializationConfig compactSerializationConfig = new CompactSerializationConfig();
    compactSerializationConfig.setEnabled(true);
    return 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)

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