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

Alachisoft.NCache.Common.JSON.JsonHelper Maven / Gradle / Ivy

There is a newer version: 5.3.3
Show newest version
package Alachisoft.NCache.Common.JSON;

import Alachisoft.NCache.Common.Caching.UserBinaryObject;
import Alachisoft.NCache.Common.MemoryUtil;
import com.alachisoft.ncache.common.protobuf.CollectionItemProtocol;
import com.alachisoft.ncache.runtime.JSON.JsonValueBase;
import com.alachisoft.ncache.runtime.exceptions.OperationFailedException;
import com.alachisoft.ncache.serialization.JSON.JsonBinaryFormatter;

import java.io.IOException;
import java.nio.charset.Charset;

public class JsonHelper {
    public static ExtendedJsonValueBase getUserObjectJsonValue(Object item, boolean isCustomAttributeBaseSerialzed) throws OperationFailedException {
        String json = null;
        JsonValueBase possiblyJsonValue = (JsonValueBase) ((item instanceof JsonValueBase) ? item : null);

        if (possiblyJsonValue != null)
            json = possiblyJsonValue.toString();
        else
            json = JsonBinaryFormatter.serializeObject(item, isCustomAttributeBaseSerialzed);

        return new UserObjectJsonValue(item, MemoryUtil.getStringSizeWithoutNetOverhead(json), MemoryUtil.getStringSize(json));
    }

    public static ExtendedJsonValueBase getBinaryJsonValue(Object serializableObject, boolean isCustomAtributeBaseSerialized) throws OperationFailedException {
        String json = new String();
        JsonValueBase possibleJsonValue = serializableObject instanceof JsonValueBase? (JsonValueBase)serializableObject:null;
        if (possibleJsonValue != null)
            json = possibleJsonValue.toString();
        else
            json = JsonBinaryFormatter.serializeObject(serializableObject, isCustomAtributeBaseSerialized);
        return new BinaryJsonValue(UserBinaryObject.createUserBinaryObject(json.getBytes(JsonBinaryFormatter.SERIALIZATION_CHARSET)));
    }

    public static ExtendedJsonValueBase getDeserializedJsonValue(ExtendedJsonValueBase jsonValueBase, int binarySize, int binaryInMemorySize, boolean isCustomAttributeBase) throws OperationFailedException {
        Object value = null;
        try {
            value = JsonBinaryFormatter.deserializeObject(jsonValueBase.toJson(), null);
        } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
            throw new OperationFailedException(e);
        }
        return JsonDocumentUtil.ToExtendedJsonValueBase(value, binarySize, binaryInMemorySize);
    }

    public static Object GetSafeDeserializeValue(ExtendedJsonValueBase jsonValueBase) throws IOException, OperationFailedException {
        if (jsonValueBase instanceof UserObjectJsonValue)
            return jsonValueBase.getValue();
        return JsonBinaryFormatter.deserializeObject(jsonValueBase.toJson(), null);

    }

    public static ExtendedJsonValueBase getExtendedJsonValueBaseFromCollectionItem(com.alachisoft.ncache.common.protobuf.CollectionItemProtocol.CollectionItem collectionItem) {
        return new BinaryJsonValue(UserBinaryObject.createUserBinaryObject(collectionItem.getDataList()));
    }

    public static JsonValueBase getJsonValueBaseFromCollectionItem(CollectionItemProtocol.CollectionItem collectionItem) {
        return getExtendedJsonValueBaseFromCollectionItem(collectionItem);
    }

    public static UserBinaryObject createUserBinaryObject(JsonValueBase jsonValue) {
        return UserBinaryObject.createUserBinaryObject(jsonValue.toJson().getBytes(Charset.forName("UTF-8")));
    }

    public static ExtendedJsonValueBase getJsonValue(Object item, boolean inProc) throws OperationFailedException {
        ExtendedJsonValueBase jsonValue;

        if (inProc)
            jsonValue = getUserObjectJsonValue(item, false);
        else
            jsonValue = getBinaryJsonValue(item, false);
        return jsonValue;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy