private SharpSerializer createSerializerWithCustomReaderAndWriter()
        {
            // *************************************************************************************
            // SERIALIZATION
            // The namespace Polenter.Serialization.Advanced contains some classes which are indispensable during the serialization.


            // *************************************************************************************
            // XmlPropertySerializer
            // serializes objects into elements and their attributes.
            // Each element has its begin and end tag.
            // XmlPropertySerializer self is not responsible for the serializing to xml, it doesn't reference the built in .NET XmlWriter.
            // Instead it uses an instance of IXmlWriter to control the element writing.
            // DefaultXmlWriter implements IXmlWriter and contains the build in .NET XmlWriter which is responsible for writing to the stream.

            // To make your own node oriented writer, you need to make a class which implements IXmlWriter
            Polenter.Serialization.Advanced.Xml.IXmlWriter jsonWriter = new MyJsonWriter();

            // this writer is passed to the constructor of the XmlPropertySerializer
            Polenter.Serialization.Advanced.Serializing.IPropertySerializer serializer =
                new Polenter.Serialization.Advanced.XmlPropertySerializer(jsonWriter);

            // in such a was, the default XmlPropertySerializer can store data in any format which is node oriented (contains begin/end tags)


            // *************************************************************************************
            // BinaryPropertySerializer
            // serializes objects into elements which have known length and fixed position in the stream.
            // It doesn't write directly to the stream. Instead, it uses an instance of IBinaryWriter.
            // Actually there are two writers used by the SharpSerializer: BurstBinaryWriter and SizeOptimizedBinaryWriter

            // To make your own binary writer you make a class which implements the IBinaryWriter.
            Polenter.Serialization.Advanced.Binary.IBinaryWriter compressedWriter = new MyVeryStrongCompressedAndEncryptedBinaryWriter();

            // this writer is passed to the constructor of the BinaryPropertySerializer
            serializer = new Polenter.Serialization.Advanced.BinaryPropertySerializer(compressedWriter);

            // Changing only the writer and not the whole serializer allows an easy serialization of data to any binary format



            // *************************************************************************************
            // DESERIALIZATION
            // The namespace Polenter.Serialization.Advanced contains classes which are counterparts of the above serializers/writers
            // XmlPropertySerializer -> XmlPropertyDeserializer
            // DefaultXmlWriter -> DefaultXmlReader
            // BurstBinaryWriter -> BurstBinaryReader
            // SizeOptimizedBinaryWriter -> SizeOptimizedBinaryReader

            // Deserializers are constructed analog or better say - symmetric to the Serializers/Writers, i.e.

            Polenter.Serialization.Advanced.Binary.IBinaryReader compressedReader =
                new MyVeryStrongCompressedAndEncryptedBinaryReader();

            Polenter.Serialization.Advanced.Deserializing.IPropertyDeserializer deserializer =
                new Polenter.Serialization.Advanced.BinaryPropertyDeserializer(compressedReader);

            // If you have created serializer and deserializer, the next step is to create SharpSerializer.


            // *************************************************************************************
            // Creating SharpSerializer
            // Both classes - serializer and deserializer are passed to the overloaded constructor

            var sharpSerializer = new SharpSerializer(serializer, deserializer);


            // there is one more option you can alter directly on your instance of SharpSerializer

            // *************************************************************************************
            // PropertyProvider
            // If the advanced setting PropertiesToIgnore are not enough there is possibility to create your own PropertyProvider
            // As a standard there are only properties serialized which:
            // - are public
            // - are not static
            // - does not contain ExcludeFromSerializationAttribute
            // - have their set and get accessors
            // - are not indexers
            // - are not in PropertyProvider.PropertiesToIgnore
            // You can replace this functionality with an inheritor class of PropertyProvider

            sharpSerializer.PropertyProvider = new MyVerySophisticatedPropertyProvider();

            // Override its methods GetAllProperties() and IgnoreProperty to customize the functionality

            return(sharpSerializer);
        }
Ejemplo n.º 2
0
        private SharpSerializer createSerializerWithCustomReaderAndWriter()
        {
            // *************************************************************************************
            // SERIALIZATION
            // The namespace Polenter.Serialization.Advanced contains some classes which are indispensable during the serialization.

            // *************************************************************************************
            // XmlPropertySerializer
            // serializes objects into elements and their attributes.
            // Each element has its begin and end tag.
            // XmlPropertySerializer self is not responsible for the serializing to xml, it doesn't reference the built in .NET XmlWriter.
            // Instead it uses an instance of IXmlWriter to control the element writing.
            // DefaultXmlWriter implements IXmlWriter and contains the build in .NET XmlWriter which is responsible for writing to the stream.

            // To make your own node oriented writer, you need to make a class which implements IXmlWriter
            Polenter.Serialization.Advanced.Xml.IXmlWriter jsonWriter = new MyJsonWriter();

            // this writer is passed to the constructor of the XmlPropertySerializer
            Polenter.Serialization.Advanced.Serializing.IPropertySerializer serializer =
                new Polenter.Serialization.Advanced.XmlPropertySerializer(jsonWriter);

            // in such a was, the default XmlPropertySerializer can store data in any format which is node oriented (contains begin/end tags)

            // *************************************************************************************
            // BinaryPropertySerializer
            // serializes objects into elements which have known length and fixed position in the stream.
            // It doesn't write directly to the stream. Instead, it uses an instance of IBinaryWriter.
            // Actually there are two writers used by the SharpSerializer: BurstBinaryWriter and SizeOptimizedBinaryWriter

            // To make your own binary writer you make a class which implements the IBinaryWriter.
            Polenter.Serialization.Advanced.Binary.IBinaryWriter compressedWriter = new MyVeryStrongCompressedAndEncryptedBinaryWriter();

            // this writer is passed to the constructor of the BinaryPropertySerializer
            serializer = new Polenter.Serialization.Advanced.BinaryPropertySerializer(compressedWriter);

            // Changing only the writer and not the whole serializer allows an easy serialization of data to any binary format

            // *************************************************************************************
            // DESERIALIZATION
            // The namespace Polenter.Serialization.Advanced contains classes which are counterparts of the above serializers/writers
            // XmlPropertySerializer -> XmlPropertyDeserializer
            // DefaultXmlWriter -> DefaultXmlReader
            // BurstBinaryWriter -> BurstBinaryReader
            // SizeOptimizedBinaryWriter -> SizeOptimizedBinaryReader

            // Deserializers are constructed analog or better say - symmetric to the Serializers/Writers, i.e.

            Polenter.Serialization.Advanced.Binary.IBinaryReader compressedReader =
                new MyVeryStrongCompressedAndEncryptedBinaryReader();

            Polenter.Serialization.Advanced.Deserializing.IPropertyDeserializer deserializer =
                new Polenter.Serialization.Advanced.BinaryPropertyDeserializer(compressedReader);

            // If you have created serializer and deserializer, the next step is to create SharpSerializer.

            // *************************************************************************************
            // Creating SharpSerializer
            // Both classes - serializer and deserializer are passed to the overloaded constructor

            var sharpSerializer = new SharpSerializer(serializer, deserializer);

            // there is one more option you can alter directly on your instance of SharpSerializer

            // *************************************************************************************
            // PropertyProvider
            // If the advanced setting PropertiesToIgnore are not enough there is possibility to create your own PropertyProvider
            // As a standard there are only properties serialized which:
            // - are public
            // - are not static
            // - does not contain ExcludeFromSerializationAttribute
            // - have their set and get accessors
            // - are not indexers
            // - are not in PropertyProvider.PropertiesToIgnore
            // You can replace this functionality with an inheritor class of PropertyProvider

            sharpSerializer.PropertyProvider = new MyVerySophisticatedPropertyProvider();

            // Override its methods GetAllProperties() and IgnoreProperty to customize the functionality

            return sharpSerializer;
        }