Exemplo n.º 1
0
        // Serialization of simple elements, that doesn't use caching
        private void SerializeInternal(T[] valueToSerialize, Action arraySerializeAction)
        {
            // Case: null value
            if (valueToSerialize == null)
            {
                SerializerStorage.WriteStorageFormat(new NullArray());
                return;
            }

            // Case: empty Array
            if (valueToSerialize.Length == 0)
            {
                SerializerStorage.WriteStorageFormat(new EmptyArray());
                return;
            }

            // Case: normal Array with Id
            SerializerStorage.WriteStorageFormat(new NormalArray());

            // Store array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);

            intSerializer.Serialize(valueToSerialize.Length);

            // Serialization of array elements
            arraySerializeAction();
        }
Exemplo n.º 2
0
        // Generic deserialization
        private T[] DeserializeInternal(Action <int, T[]> arrayDeserializationAction)
        {
            // Read info about storage format
            ArrayStorageFormats format = (ArrayStorageFormats)SerializerStorage.ReadStorageFormatId(ArrayStorageBase.FormatIdSizeInBits);

            // Case: null Array
            if (format == ArrayStorageFormats.NullArray)
            {
                return(null);
            }

            // Case: empty Array
            if (format == ArrayStorageFormats.EmptyArray)
            {
                return(new T[0]);
            }

            // Restore array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);
            Int32           arrayLength   = intSerializer.Deserialize();

            // Case: normal Array
            T[] outputArray = new T[arrayLength];

            // Deserialize array elements
            arrayDeserializationAction(arrayLength, outputArray);

            // Return result
            return(outputArray);
        }
Exemplo n.º 3
0
        // Generic deserialization
        public T[] Deserialize()
        {
            // Read info about storage format
            ArrayStorageFormats format = (ArrayStorageFormats)SerializerStorage.ReadStorageFormatId(ArrayStorageBase.FormatIdSizeInBits);

            // Case: null Array
            if (format == ArrayStorageFormats.NullArray)
            {
                return(null);
            }

            // Case: empty Array
            if (format == ArrayStorageFormats.EmptyArray)
            {
                return(new T[0]);
            }

            // Restore array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);
            Int32           arrayLength   = intSerializer.Deserialize();

            // Case: normal Array
            T[] outputArray = new T[arrayLength];

            // Deserialize all the elems
            for (int pos = 0; pos < arrayLength; pos++)
            {
                T value = m_ElemDeserializationFunc();
                outputArray[pos] = value;
            }

            // Return result
            return(outputArray);
        }
Exemplo n.º 4
0
        // Serialization of simple elements, that doesn't use caching
        public void Serialize(T[] valueToSerialize)
        {
            // Case: null value
            if (valueToSerialize == null)
            {
                SerializerStorage.WriteStorageFormat(new NullArray());
                return;
            }

            // Case: empty Array
            if (valueToSerialize.Length == 0)
            {
                SerializerStorage.WriteStorageFormat(new EmptyArray());
                return;
            }

            // Case: normal Array with Id
            SerializerStorage.WriteStorageFormat(new NormalArray());

            // Store array size
            Int32Serializer intSerializer = new Int32Serializer(SerializerStorage);

            intSerializer.Serialize(valueToSerialize.Length);

            // Serialization of array elements
            for (int pos = 0; pos < valueToSerialize.Length; pos++)
            {
                m_ElemSerializationAction(valueToSerialize[pos]);
            }
        }
Exemplo n.º 5
0
        // Deserialization
        public string Deserialize()
        {
            // Read info about storage format
            StringStorageFormats format = (StringStorageFormats)SerializerStorage.ReadStorageFormatId(StringStorageBase.FormatIdSizeInBits);

            // Case: null string
            if (format == StringStorageFormats.NullString)
            {
                return(null);
            }

            // Case: empty string
            if (format == StringStorageFormats.EmptyString)
            {
                return(string.Empty);
            }

            // Int32 serializer
            Int32Serializer int32Serializer = new Int32Serializer(SerializerStorage);

            // If caching has been activated
            if (SerializerStorage.UseValCaching)
            {
                // Case: cached string
                if (format == StringStorageFormats.CachedString)
                {
                    // Read object Id
                    this.ObjectId = int32Serializer.Deserialize();

                    // Take value from cache (objects dictionary)
                    return(ObjectCache.GetObjectValueForValueTypeField <String>(this));
                }

                // Case: new string
                if (format == StringStorageFormats.NormalString)
                {
                    this.ObjectId = int32Serializer.Deserialize();                                      // Read object Id
                    int    encodedStringLength = int32Serializer.Deserialize();                         // Read encoded string length
                    byte[] encodedString       = SerializerStorage.ReadPackedData(encodedStringLength); // Read encoded data

                    // Decode string
                    string result = Encoding.UTF8.GetString(encodedString, 0, encodedString.Length);

                    // Register new string in cache
                    ObjectCache.RegisterValue(result, this);

                    // Return result
                    return(result);
                }
            }
            else
            {
                // Value without caching (so without Id)
                int    encodedStringLength = int32Serializer.Deserialize();                         // Read encoded string length
                byte[] encodedString       = SerializerStorage.ReadPackedData(encodedStringLength); // Read encoded data

                // Decode string
                return(Encoding.UTF8.GetString(encodedString, 0, encodedString.Length));
            }

            // Default result
            return(null);
        }
Exemplo n.º 6
0
        // Serialization
        public void Serialize(string valueToSerialize)
        {
            // Case: null value
            if (valueToSerialize == null)
            {
                SerializerStorage.WriteStorageFormat(new NullString());
                return;
            }

            // Case: empty string
            if (valueToSerialize == String.Empty)
            {
                SerializerStorage.WriteStorageFormat(new EmptyString());
                return;
            }

            // Int32 Serializer
            Int32Serializer int32Serializer = new Int32Serializer(SerializerStorage);

            // If caching has been activated
            if (SerializerStorage.UseValCaching)
            {
                // Regular string - we should obtain string Id from cache
                bool shouldStoreFullData = ObjectCache.GetObjectIdForValueTypeField(valueToSerialize, this);

                // If we should store full data
                if (shouldStoreFullData)
                {
                    // Case: normal string with Id
                    SerializerStorage.WriteStorageFormat(new NormalString());

                    // Store Id of string
                    int32Serializer.Serialize(this.ObjectId);

                    // Encode string to utf-8
                    byte[] stringData = Encoding.UTF8.GetBytes(valueToSerialize);

                    // Store string length
                    int32Serializer.Serialize(stringData.Length);

                    // Store string data
                    SerializerStorage.WritePackedData(stringData);
                }
                else
                {
                    // Case: cached string
                    SerializerStorage.WriteStorageFormat(new CachedString());

                    // Store Id of string
                    int32Serializer.Serialize(this.ObjectId);
                }
            }
            else
            {
                // Store value without caching
                SerializerStorage.WriteStorageFormat(new NormalString());

                // Encode string to utf-8
                byte[] stringData = Encoding.UTF8.GetBytes(valueToSerialize);

                // Store string length
                int32Serializer.Serialize(stringData.Length);

                // Store string data
                SerializerStorage.WritePackedData(stringData);
            }
        }