com.alachisoft.ncache.client.util.SerializationUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ncache-professional-client Show documentation
Show all versions of ncache-professional-client Show documentation
NCache Professional client for java.
package com.alachisoft.ncache.client.util;
import Alachisoft.NCache.Common.BitSet;
import Alachisoft.NCache.Common.BitSetConstants;
import Alachisoft.NCache.Common.Caching.UserBinaryObject;
import Alachisoft.NCache.Common.Enum.SerializationFormat;
import Alachisoft.NCache.Common.Enum.UserObjectType;
import Alachisoft.NCache.Common.JSON.BinaryJsonValue;
import Alachisoft.NCache.Common.JSON.ExtendedJsonValueBase;
import Alachisoft.NCache.Common.JSON.JsonHelper;
import com.alachisoft.ncache.common.monitoring.IntervalCounterDataCollection;
import com.alachisoft.ncache.runtime.JSON.JsonObject;
import com.alachisoft.ncache.runtime.JSON.JsonValueBase;
import com.alachisoft.ncache.runtime.exceptions.GeneralFailureException;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import com.alachisoft.ncache.serialization.JSON.JsonBinaryFormatter;
import com.alachisoft.ncache.serialization.standard.CompactBinaryFormatter;
import com.alachisoft.ncache.serialization.standard.io.CompactWriter;
import com.fasterxml.jackson.core.JsonProcessingException;
import tangible.RefObject;
import java.io.IOException;
import java.util.ArrayList;
public class SerializationUtil {
public static Object safeSerializeOutProc(Object serializableObject, String serializationContext, BitSet flag, boolean serializationEnabled, SerializationFormat serializationFormat,RefObject size, UserObjectType userObjectType, boolean isCustomAtributeBaseSerialized) throws GeneralFailureException, OperationFailedException {
if(serializableObject == null || !serializationEnabled)
return serializableObject;
switch (userObjectType)
{
case CacheItem:
Class> type = serializableObject.getClass();
if (byte[].class.equals(type) && flag != null)
{
flag.SetBit((byte) BitSetConstants.BinaryData);
return serializableObject;
}
try {
if(JsonValueBase.class.isAssignableFrom(serializableObject.getClass())) {
serializableObject = JsonBinaryFormatter.toByteArray(serializableObject);
flag.SetBit((byte) BitSetConstants.JsonData);
return serializableObject;
}
switch (serializationFormat)
{
case Binary:
serializableObject = CompactBinaryFormatter.toByteBuffer(serializableObject, serializationContext);
break;
case Json:
serializableObject = JsonBinaryFormatter.toByteArray(serializableObject);
flag.SetBit((byte) BitSetConstants.JsonData);
break;
}
} catch (IOException | OperationFailedException e) {
throw new GeneralFailureException(e);
}
break;
case DistributedDataType:
ExtendedJsonValueBase jsonSerializedValue = JsonHelper.getBinaryJsonValue(serializableObject, isCustomAtributeBaseSerialized);
serializableObject = jsonSerializedValue;
break;
}
return serializableObject;
}
public static T safeDeserializeOutProc(Object serializedObject, String serializationContext, BitSet flag, boolean isSerializationEnabled, UserObjectType userObjectType, Class> cls) throws IOException, ClassNotFoundException, OperationFailedException {
if(cls == null)
cls = Object.class;
Object deserialized = serializedObject;
boolean userAskedForJson = JsonValueBase.class.isAssignableFrom(cls);
switch (userObjectType)
{
case CacheItem:
if (!(serializedObject instanceof byte[]) || !isSerializationEnabled)
break;
if (flag != null && flag.IsBitSet((byte) BitSetConstants.BinaryData))
{
if (!userAskedForJson)
return (T)serializedObject;
throw new UnsupportedOperationException("Cannot interpret binary serialized data as JSON serialized data.");
}
if (flag != null && flag.IsBitSet((byte) BitSetConstants.JsonData))
{
if(userAskedForJson)
deserialized = JsonBinaryFormatter.fromByteBuffer((byte[]) serializedObject, cls);
else
deserialized = JsonBinaryFormatter.deserializeObject(JsonBinaryFormatter.decodeString((byte[]) serializedObject), cls);
}
else
{
deserialized = CompactBinaryFormatter.fromByteBuffer((byte[])serializedObject, serializationContext);
if(!cls.isAssignableFrom(deserialized.getClass()))
{
throw new ClassCastException("class "+deserialized.getClass().getName()+" cannot be cast to "+cls);
}
}
break;
case DistributedDataType:
JsonValueBase jsonValue = (JsonValueBase) serializedObject;
if(jsonValue != null)
deserialized = JsonBinaryFormatter.deserializeObject((String) jsonValue.toJson(), cls);
break;
}
return (T)deserialized;
}
public static Object safeSerializeInProc(Object serializableObject, String serializationContext, BitSet flag, boolean serializationEnabled,
SerializationFormat serializationFormat, RefObject size, UserObjectType userObjectType, boolean isCustomAtributeBaseSerialized) throws GeneralFailureException, OperationFailedException, IOException {
if (serializationEnabled)
{
return safeSerializeOutProc(serializableObject, serializationContext, flag, serializationEnabled, serializationFormat, size, userObjectType,isCustomAtributeBaseSerialized);
}
Object serializableObjectUnser = serializableObject;
if (size.argvalue <= 0)
{
switch (userObjectType)
{
case CacheItem:
java.lang.Class type = serializableObject.getClass();
if (byte[].class.equals(type) && flag != null)
{
flag.SetBit((byte) BitSetConstants.BinaryData);
size.setValue((long) ((byte[])serializableObject).length);
return serializableObject;
}
if(JsonValueBase.class.isAssignableFrom(serializableObject.getClass())) {
serializableObject = JsonBinaryFormatter.toByteArray(serializableObject);
flag.SetBit((byte) BitSetConstants.JsonData);
return serializableObject;
}
switch (serializationFormat)
{
case Binary:
serializableObject = CompactBinaryFormatter.toByteBuffer(serializableObject, serializationContext);
break;
case Json:
serializableObject = JsonBinaryFormatter.toByteArray(serializableObject);
flag.SetBit((byte) BitSetConstants.JsonData);
break;
}
size.argvalue = (long)(serializableObject instanceof byte[] ? ((byte[])serializableObject).length : 0);
break;
case DistributedDataType:
ExtendedJsonValueBase jsonSerializedValue = JsonHelper.getUserObjectJsonValue(serializableObject, isCustomAtributeBaseSerialized);
serializableObject = jsonSerializedValue;
size.argvalue =(long) jsonSerializedValue.getSize();
return jsonSerializedValue;
}
}
return serializableObjectUnser;
}
public static T safeDeserializeInProc(Object serializedObject, String serializationContext, BitSet flag, boolean isSerializationEnabled, UserObjectType userObjectType, Class> cls) throws IOException, ClassNotFoundException, OperationFailedException
{
if(cls == null)
cls = Object.class;
if (isSerializationEnabled)
{
return safeDeserializeOutProc(serializedObject, serializationContext, flag, isSerializationEnabled, userObjectType,cls);
}
if (serializedObject == null)
{
return (T)serializedObject;
}
boolean userAskedForJson = JsonValueBase.class.isAssignableFrom(cls);
switch (userObjectType)
{
case CacheItem:
boolean valueFromCacheIsJson = serializedObject instanceof JsonValueBase;
if (!valueFromCacheIsJson && userAskedForJson)
{
throw new UnsupportedOperationException("Caches with object data format do not support JSON formatted data from user data.");
}
if(!cls.isAssignableFrom(serializedObject.getClass()))
{
throw new ClassCastException("class "+serializedObject.getClass().getName()+" cannot be cast to "+cls);
}
if (valueFromCacheIsJson && !userAskedForJson)
{
return JsonBinaryFormatter.deserializeObject(JsonBinaryFormatter.serializeObject(serializedObject),cls);
}
return (T)serializedObject;
case DistributedDataType:
ExtendedJsonValueBase extendedJsonValueBase = (ExtendedJsonValueBase)((serializedObject instanceof ExtendedJsonValueBase) ? serializedObject : null);
if (extendedJsonValueBase != null)
{
boolean dataStructureHasJsonData = extendedJsonValueBase.getValue() instanceof JsonValueBase;
if (!dataStructureHasJsonData && userAskedForJson)
{
throw new UnsupportedOperationException("Caches with object data format do not support JSON formatted data from user data.");
}
if (dataStructureHasJsonData && !userAskedForJson)
{
return JsonBinaryFormatter.deserializeObject(JsonBinaryFormatter.serializeObject(extendedJsonValueBase.getValue(),false),cls);
}
return (T)extendedJsonValueBase.getValue();
}
return (T)serializedObject;
default:
return (T)serializedObject;
}
}
//region For JNI Don't Remove
static Object safeSerialize(Object serializableObject, SerializationFormat serializer, String serializationContext, BitSet flag) throws IOException, OperationFailedException {
if (serializableObject != null)
{
if (serializableObject instanceof byte[]) {
flag.SetBit((byte) BitSetConstants.BinaryData);
return serializableObject;
}
if(JsonValueBase.class.isAssignableFrom(serializableObject.getClass())) {
serializableObject = JsonBinaryFormatter.toByteArray(serializableObject);
flag.SetBit((byte) BitSetConstants.JsonData);
return serializableObject;
}
switch (serializer)
{
case Binary:
serializableObject = CompactBinaryFormatter.toByteBuffer(serializableObject, serializationContext);
break;
case Json:
flag.SetBit((byte) BitSetConstants.JsonData);
serializableObject = JsonBinaryFormatter.toByteArray(serializableObject);
break;
}
}
return serializableObject;
}
//endregion
}