Example #1
0
        public void BinarySerializer_Serialize_GivenNullInput_ShouldReturnNull()
        {
            var binarySerializer = new BinarySerializer();
            var result = binarySerializer.Serialize(null);

            Assert.IsNull(result);
        }
Example #2
0
        /// <summary>
        /// Loads a custom serializer. If one was not specified, or the loading fails, loads the default serializer.
        /// </summary>
        /// <returns>The serializer.</returns>
        public static IBinarySerializer LoadSerializer()
        {
            var defaultSerializer = new BinarySerializer();

            // Configure custom serializer
            try
            {
                var customSerializerTypeString = CacheClientConfigurationSection.Settings.CustomSerializer.Type;
                // Check for custom serializer
                if (string.IsNullOrWhiteSpace(customSerializerTypeString))
                {
                    // No custom serializer
                    return defaultSerializer;
                }

                // Have a custom serializer, attempt to load it and confirm it
                var customSerializerType = Type.GetType(customSerializerTypeString);
                // Verify that it implements our IBinarySerializer interface
                if (customSerializerType != null && typeof(IBinarySerializer).IsAssignableFrom(customSerializerType))
                {
                    return (IBinarySerializer)Activator.CreateInstance(customSerializerType);
                }
                if (customSerializerType == null) { throw new Exception("custom serializer is null"); }
                if (!customSerializerType.IsAssignableFrom(typeof(IBinarySerializer))) { throw new Exception("custom serializer is not of type IBinarySerializer"); }
            }
            catch (Exception ex)
            {
                // Custom serializer load failed - no custom serialization
                LoadLogger().Error(ex);
            }

            return defaultSerializer;
        }
        public void BinarySerializer_Serialize_GivenValidReferenceTypeInput_ShouldReturnBytes()
        {
            var binarySerializer = new BinarySerializer();
            var input = "test";
            var result = binarySerializer.Serialize(input);

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Length > 0);
        }
        public void BinarySerializer_Deserialize_GivenValidReferenceType_ShouldReturnReferenceType()
        {
            var binarySerializer = new BinarySerializer();
            var input = "test";
            var serialized = binarySerializer.Serialize(input);

            var deserialized = binarySerializer.Deserialize<string>(serialized);

            Assert.IsNotNull(deserialized);
            Assert.AreEqual("test", deserialized);
        }
        public void BinarySerializer_Deserialize_GivenValidValueType_ShouldReturnValueType()
        {
            var binarySerializer = new BinarySerializer();
            var input = 123;
            var serialized = binarySerializer.Serialize(input);

            var deserialized = binarySerializer.Deserialize<int>(serialized);

            Assert.IsNotNull(deserialized);
            Assert.AreEqual(123, deserialized);
        }
Example #6
0
        public void BinarySerializer_Deserialize_GivenValidReferenceTypeAsBytesInput_ShouldReturnReferenceType()
        {
            var binarySerializer = new BinarySerializer();
            // string value of "test" as bytes
            var serializedString = new byte[] { 0, 1, 0, 0, 0, 255, 255, 255, 255, 1, 0, 0, 0, 0, 0, 0, 0, 6, 1, 0, 0, 0, 4, 116, 101, 115, 116, 11 };

            var resultObject = binarySerializer.Deserialize(serializedString);

            Assert.IsNotNull(resultObject);

            var deserializedString = resultObject as string;

            Assert.IsNotNull(deserializedString);
            Assert.AreEqual("test", deserializedString);
        }
Example #7
0
        public void BinarySerializer_Deserialize_GivenValidValueTypeAsBytesInput_ShouldReturnValueType()
        {
            var binarySerializer = new BinarySerializer();
            // int value of 123 as bytes
            var serializedInt = new byte[]
            { 0, 1, 0, 0, 0, 255, 255, 255, 255, 1, 0, 0, 0, 0, 0, 0, 0, 4, 1, 0, 0, 0, 12, 83, 121, 115, 116, 101,
                109, 46, 73, 110, 116, 51, 50, 1, 0, 0, 0, 7, 109, 95, 118, 97, 108, 117, 101, 0, 8, 123, 0, 0, 0, 11 };

            var resultObject = binarySerializer.Deserialize(serializedInt);

            Assert.IsNotNull(resultObject);

            var deserializedInt = (int)resultObject;

            Assert.AreEqual(123, deserializedInt);
        }
Example #8
0
        public void BinarySerializer_Serialize_Deserialize_ShouldReturnOriginalResult()
        {
            var binarySerializer = new BinarySerializer();
            var input = "test";
            var serializedResult = binarySerializer.Serialize(input);

            Assert.IsNotNull(serializedResult);

            var deserializedResult = binarySerializer.Deserialize(serializedResult);

            Assert.IsNotNull(deserializedResult);

            var resultString = deserializedResult as string;

            Assert.IsNotNull(resultString);
            Assert.AreEqual("test", resultString);
        }