All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.internal.serialization.impl.SerializationService Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2008-2024, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.internal.serialization.impl;

import com.hazelcast.core.ManagedContext;
import com.hazelcast.internal.serialization.Data;
import com.hazelcast.partition.PartitioningStrategy;

/**
 * SPI to serialize user objects to { com.hazelcast.internal.serialization.Data} and back to Object
 * { com.hazelcast.internal.serialization.Data} is the internal representation of binary data in hazelcast.
 */
public interface SerializationService {

    /**
     * Serializes an object to a { com.hazelcast.internal.serialization.Data}.
     * 

* This method can safely be called with a { com.hazelcast.internal.serialization.Data} instance. * In that case, that instance is returned. *

* If this method is called with null, null is returned. * * @param obj the object to serialize. * @return the serialized object. * @throws com.hazelcast.nio.serialization.HazelcastSerializationException when serialization fails. */ B toData(Object obj); /** * Serializes an object to a { com.hazelcast.internal.serialization.Data} that contains the * { com.hazelcast.internal.serialization.impl.compact.Schema} in the * binary, if the {@code object} is compact serializable. If not, * this method is same as the { #toData(Object)}. *

* An object is compact serializable if *

    *
  • it is registered for compact serialization via * { com.hazelcast.config.CompactSerializationConfig}
  • *
  • there is no serializer registered for it, and it can serialized * reflectively
  • *
*

* This method can safely be called with a { com.hazelcast.internal.serialization.Data} instance with a * caveat. To include all the possible schemas, the data will be * deserialized and serialized again, if it does not already include * the schema. * * @param obj the object to serialize. * @return the serialized object. * @throws com.hazelcast.nio.serialization.HazelcastSerializationException when serialization fails. */ B toDataWithSchema(Object obj); /** * Serializes an object to a { com.hazelcast.internal.serialization.Data}. *

* This method can safely be called with a { com.hazelcast.internal.serialization.Data} instance. * In that case, that instance is returned. *

* If this method is called with null, null is returned. * * @param obj the object to serialize. * @param strategy strategy is used to calculate partition ID of the resulting data see { PartitioningStrategy} * @return the serialized object. * @throws com.hazelcast.nio.serialization.HazelcastSerializationException when serialization fails. */ B toData(Object obj, PartitioningStrategy strategy); /** * Deserializes an object. *

* This method can safely be called on an object that is already deserialized. In that case, that instance * is returned. *

* If this method is called with null, null is returned. * * @param data the data to deserialize. * @return the deserialized object. * @throws com.hazelcast.nio.serialization.HazelcastSerializationException when deserialization fails. */ T toObject(Object data); /** * Deserializes an object. *

* This method can safely be called on an object that is already deserialized. In that case, that instance * is returned. *

* If this method is called with null, null is returned. * * @param data the data to deserialize. * @param klazz The class to instantiate when deserializing the object. * @return the deserialized object. * @throws com.hazelcast.nio.serialization.HazelcastSerializationException when deserialization fails. */ T toObject(Object data, Class klazz); /** * see { com.hazelcast.config.Config#setManagedContext(ManagedContext)} * * @return ManagedContext that is set by user in Config */ ManagedContext getManagedContext(); /** * Trims the { com.hazelcast.internal.serialization.impl.compact.Schema} * from the {@code data}, if it includes the schema in its serialized form. * If not, this method will return the {@code data} as it is, without * changing it. *

* Trimming process takes care of the nested schemas, as well as the top * level schema. * * @param data the data to be trimmed, if it contains a schema. * @return the data without schema in it. */ B trimSchema(Data data); }