private void Initialize(BinarySettings settings)
        {
            //RootName
            RootName = settings.AdvancedSettings.RootName;

            // TypeNameConverter)
            var typeNameConverter = settings.AdvancedSettings.TypeNameConverter ??
                                    DefaultInitializer.GetTypeNameConverter(
                settings.IncludeAssemblyVersionInTypeName,
                settings.IncludeCultureInTypeName,
                settings.IncludePublicKeyTokenInTypeName);


            // Create Serializer and Deserializer
            IBinaryReader reader = null;
            IBinaryWriter writer = null;

            if (settings.Mode == BinarySerializationMode.Burst)
            {
                // Burst mode
                writer = new BurstBinaryWriter(typeNameConverter, settings.Encoding);
                reader = new BurstBinaryReader(typeNameConverter, settings.Encoding);
            }
            else
            {
                // Size optimized mode
                writer = new SizeOptimizedBinaryWriter(typeNameConverter, settings.Encoding);
                reader = new SizeOptimizedBinaryReader(typeNameConverter, settings.Encoding);
            }

            _deserializer = new BinaryPropertyDeserializer(reader);
            _serializer   = new BinaryPropertySerializer(writer);
        }
        public void BinSerial_TwoIdenticalChildsShouldBeSameInstance()
        {
            var parent = new ParentChildTestClass
            {
                Name = "parent"
            };

            var child = new ParentChildTestClass
            {
                Name   = "child",
                Father = parent,
                Mother = parent
            };

            Assert.AreSame(child.Father, child.Mother, "Precondition: Saved Father and Mother are same instance");

            var stream     = new MemoryStream();
            var settings   = new BinarySettings(BinarySerializationMode.SizeOptimized);
            var serializer = GetSut(settings);

            serializer.Serialize(child, stream);

            serializer      = GetSut(settings);
            stream.Position = 0;
            var loaded = serializer.Deserialize(stream) as ParentChildTestClass;

            Assert.AreSame(loaded.Father, loaded.Mother, "Loaded Father and Mother are same instance");
        }
 /// <summary>
 ///     Binary serialization with custom settings
 /// </summary>
 /// <param name="settings"></param>
 public SharpSerializer(BinarySettings settings)
 {
     if (settings == null)
     {
         throw new ArgumentNullException(nameof(settings));
     }
     Initialize(settings);
 }
        public void HelloWorldTest()
        {
            foreach (var testCase in GetHelloWorldTestCases())
            {
                // Binary SizeOptimized
                Serialize(testCase, GetSut());

                // Binary Burst
                var settings = new BinarySettings(BinarySerializationMode.Burst);
                Serialize(testCase, GetSut(settings));
            }
        }
        public void BurstBinaryStream_SizeIsTooShort_throwsDeserializingException()
        {
            //arrange
            var myArray  = new[] { "ala", "ma", null, "kota" };
            var settings = new BinarySettings(BinarySerializationMode.Burst);
            var sut      = GetSut(settings);
            //act
            TestDelegate action = () => Serialize(myArray, sut, ShortenData);

            //assert
            Assert.Throws <DeserializingException>(action);
        }
        public void BinSerial_ShouldSerializeGuid()
        {
            var parent = new ClassWithGuid
            {
                Guid = Guid.NewGuid()
            };

            var stream     = new MemoryStream();
            var settings   = new BinarySettings(BinarySerializationMode.SizeOptimized);
            var serializer = GetSut(settings);

            serializer.Serialize(parent, stream);


            serializer      = GetSut(settings);
            stream.Position = 0;
            var loaded = serializer.Deserialize(stream) as ClassWithGuid;

            Assert.AreEqual(parent.Guid, loaded.Guid, "same guid");
        }
 private SharpSerializer GetSut(BinarySettings settings) => new SharpSerializer(settings);