Search in sources :

Example 41 with SerializationConfig

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

the class XmlClientConfigBuilder method handleSerialization.

private void handleSerialization(Node node) {
    SerializationConfig serializationConfig = parseSerialization(node);
    clientConfig.setSerializationConfig(serializationConfig);
}
Also used : SerializationConfig(com.hazelcast.config.SerializationConfig)

Example 42 with SerializationConfig

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

the class SerializationTest method testGlobalSerializer_withoutOverrideJavaSerializable.

@Test
public void testGlobalSerializer_withoutOverrideJavaSerializable() {
    GlobalSerializerConfig globalSerializerConfig = new GlobalSerializerConfig();
    globalSerializerConfig.setOverrideJavaSerialization(false);
    final AtomicInteger writeCounter = new AtomicInteger();
    final AtomicInteger readCounter = new AtomicInteger();
    SerializationConfig serializationConfig = new SerializationConfig().setGlobalSerializerConfig(globalSerializerConfig.setImplementation(new StreamSerializer<Object>() {

        @Override
        public void write(ObjectDataOutput out, Object v) throws IOException {
            writeCounter.incrementAndGet();
            out.writeUTF(((DummyValue) v).s);
            out.writeInt(((DummyValue) v).k);
        }

        @Override
        public Object read(ObjectDataInput in) throws IOException {
            readCounter.incrementAndGet();
            return new DummyValue(in.readUTF(), in.readInt());
        }

        public int getTypeId() {
            return 123;
        }

        public void destroy() {
        }
    }));
    SerializationService ss1 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
    DummyValue value = new DummyValue("test", 111);
    Data data1 = ss1.toData(value);
    Data data2 = ss1.toData(new Foo());
    Assert.assertNotNull(data1);
    Assert.assertNotNull(data2);
    assertEquals(1, writeCounter.get());
    SerializationService ss2 = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
    Object o1 = ss2.toObject(data1);
    Object o2 = ss2.toObject(data2);
    Assert.assertEquals(value, o1);
    Assert.assertNotNull(o2);
    assertEquals(1, readCounter.get());
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) ObjectDataOutput(com.hazelcast.nio.ObjectDataOutput) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SerializationConfig(com.hazelcast.config.SerializationConfig) SerializationService(com.hazelcast.spi.serialization.SerializationService) HeapData(com.hazelcast.internal.serialization.impl.HeapData) ObjectDataInput(com.hazelcast.nio.ObjectDataInput) GlobalSerializerConfig(com.hazelcast.config.GlobalSerializerConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 43 with SerializationConfig

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

the class PortableTest method testClassDefinitionConfig.

@Test
public void testClassDefinitionConfig() throws Exception {
    int portableVersion = 1;
    SerializationConfig serializationConfig = new SerializationConfig();
    serializationConfig.addPortableFactory(PORTABLE_FACTORY_ID, new TestPortableFactory());
    serializationConfig.setPortableVersion(portableVersion);
    serializationConfig.addClassDefinition(new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, TestSerializationConstants.RAW_DATA_PORTABLE, portableVersion).addLongField("l").addCharArrayField("c").addPortableField("p", createNamedPortableClassDefinition(portableVersion)).build()).addClassDefinition(new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, TestSerializationConstants.NAMED_PORTABLE, portableVersion).addUTFField("name").addIntField("myint").build());
    SerializationService serializationService = new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
    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 : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationConfig(com.hazelcast.config.SerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) SerializationService(com.hazelcast.spi.serialization.SerializationService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 44 with SerializationConfig

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

the class PortableTest method testClassDefinitionConfigWithErrors.

@Test
public void testClassDefinitionConfigWithErrors() throws Exception {
    SerializationConfig serializationConfig = new SerializationConfig();
    serializationConfig.addPortableFactory(PORTABLE_FACTORY_ID, new TestPortableFactory());
    serializationConfig.setPortableVersion(1);
    serializationConfig.addClassDefinition(new ClassDefinitionBuilder(PORTABLE_FACTORY_ID, TestSerializationConstants.RAW_DATA_PORTABLE, 1).addLongField("l").addCharArrayField("c").addPortableField("p", createNamedPortableClassDefinition(1)).build());
    try {
        new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
        fail("Should throw HazelcastSerializationException!");
    } catch (HazelcastSerializationException ignored) {
    }
    new DefaultSerializationServiceBuilder().setConfig(serializationConfig).setCheckClassDefErrors(false).build();
    // -- OR --
    serializationConfig.setCheckClassDefErrors(false);
    new DefaultSerializationServiceBuilder().setConfig(serializationConfig).build();
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationConfig(com.hazelcast.config.SerializationConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 45 with SerializationConfig

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

the class SampleMetadataResolverTest method test_compactRecord.

@Test
public void test_compactRecord() {
    SerializationConfig serializationConfig = new SerializationConfig();
    serializationConfig.getCompactSerializationConfig().setEnabled(true);
    InternalSerializationService ss = new DefaultSerializationServiceBuilder().setSchemaService(CompactTestUtil.createInMemorySchemaService()).setConfig(serializationConfig).build();
    Metadata metadata = SampleMetadataResolver.resolve(ss, GenericRecordBuilder.compact("type-name").setInt32("field", 1).build(), key);
    assertThat(metadata.fields()).containsExactly(new MappingField("field", QueryDataType.INT, (key ? KEY : VALUE) + ".field"));
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, COMPACT_FORMAT), entry(key ? OPTION_KEY_COMPACT_TYPE_NAME : OPTION_VALUE_COMPACT_TYPE_NAME, "type-name"));
    metadata = SampleMetadataResolver.resolve(ss, ss.toData(GenericRecordBuilder.compact("type-name").setInt32("field", 1).build()), key);
    assertThat(metadata.fields()).containsExactly(new MappingField("field", QueryDataType.INT, (key ? KEY : VALUE) + ".field"));
    assertThat(metadata.options()).containsExactly(entry(key ? OPTION_KEY_FORMAT : OPTION_VALUE_FORMAT, COMPACT_FORMAT), entry(key ? OPTION_KEY_COMPACT_TYPE_NAME : OPTION_VALUE_COMPACT_TYPE_NAME, "type-name"));
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) SerializationConfig(com.hazelcast.config.SerializationConfig) InternalSerializationService(com.hazelcast.internal.serialization.InternalSerializationService) MappingField(com.hazelcast.sql.impl.schema.MappingField) 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