Ejemplo n.º 1
0
        /// <summary>
        /// Deserialize an object from a byte array
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <param name="options">The serialization options</param>
        /// <param name="typeMaps">A list of type mappings</param>
        /// <returns></returns>
        public object Deserialize(Type type, byte[] bytes, SerializerOptions options, params SerializationTypeMap[] typeMaps)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (bytes.Length == 0)
            {
                return(null);
            }

            SerializationTypeRegistry typeRegistry = null;

            if (typeMaps != null && typeMaps.Length > 0)
            {
                SerializationTypeRegistry.Configure((config) =>
                {
                    foreach (var typeMap in typeMaps)
                    {
                        config.Mappings.Add(typeMap);
                    }
                });
            }

            return(Deserialize(type, bytes, options, typeRegistry));
        }
        /// <summary>
        /// Configure a new type registry
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static SerializationTypeRegistry Configure(Action <SerializationTypeRegistry> config)
        {
            var registry = new SerializationTypeRegistry();

            config(registry);
            return(registry);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Deserialize an object from a byte array
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="bytes"></param>
        /// <param name="options">The serialization options</param>
        /// <param name="typeRegistry">A list of type mappings</param>
        /// <returns></returns>
        public T Deserialize <T>(byte[] bytes, SerializerOptions options, SerializationTypeRegistry typeRegistry)
        {
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }
            if (bytes.Length == 0)
            {
                return(default(T));
            }

            return(_deserializer.InspectAndDeserialize <T>(bytes, Constants.DefaultMaxDepth, options, _ignoreAttributes, typeRegistry));
        }
Ejemplo n.º 4
0
        private TypeRegistry ConvertToTypeRegistry(SerializationTypeRegistry typeRegistry)
        {
            if (typeRegistry == null)
            {
                return(null);
            }
            var registry = TypeRegistry.Configure(c => {
                foreach (var factory in typeRegistry.Factories)
                {
                    var newFactory = _objectFactory.CreateEmptyObject <TypeFactory>(factory.Source, factory.Factory);
                    c.Factories.Add(newFactory);
                }
                foreach (var map in typeRegistry.Mappings)
                {
                    var newMap         = _objectFactory.CreateEmptyObject <TypeMap>();
                    newMap.Source      = map.Source;
                    newMap.Destination = map.Destination;
                    c.Mappings.Add(newMap);
                }
            });

            return(registry);
        }
        /// <summary>
        /// Inspect a type and deserialize its contents
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceExtendedType"></param>
        /// <param name="sourceBytes"></param>
        /// <param name="maxDepth"></param>
        /// <param name="options">The serialization options</param>
        /// <param name="ignoreAttributes"></param>
        /// <param name="typeRegistry">Custom type registry</param>
        /// <returns></returns>
        internal object InspectAndDeserialize(Type type, byte[] sourceBytes, uint maxDepth, SerializerOptions options, ICollection <object> ignoreAttributes, SerializationTypeRegistry typeRegistry = null, ICollection <string> ignorePropertiesOrPaths = null)
        {
            if (sourceBytes == null)
            {
                return(null);
            }

            using (var stream = new MemoryStream(sourceBytes))
            {
                using (var reader = new BinaryReader(stream))
                {
                    var obj = TypeReader.Read(reader, type.GetExtendedType(), maxDepth, options, ignoreAttributes, typeRegistry, ignorePropertiesOrPaths);
                    return(obj);
                }
            }
        }
 /// <summary>
 /// Inspect a type and deserialize its contents
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="sourceBytes"></param>
 /// <param name="maxDepth"></param>
 /// <param name="options">The serialization options</param>
 /// <param name="ignoreAttributes"></param>
 /// <param name="typeRegistry">Custom type registry</param>
 /// <returns></returns>
 internal T InspectAndDeserialize <T>(byte[] sourceBytes, uint maxDepth, SerializerOptions options, ICollection <object> ignoreAttributes, SerializationTypeRegistry typeRegistry = null, ICollection <string> ignorePropertiesOrPaths = null)
 {
     return((T)InspectAndDeserialize(typeof(T), sourceBytes, maxDepth, options, ignoreAttributes, typeRegistry, ignorePropertiesOrPaths));
 }
Ejemplo n.º 7
0
 public TypeReader(SerializerDataSettings dataSettings, SerializerOptions options, uint maxDepth, ICollection <object> ignoreAttributes, SerializationTypeRegistry typeRegistry, ICollection <string> ignorePropertiesOrPaths = null)
 {
     _dataSettings            = dataSettings;
     _options                 = options;
     _maxDepth                = maxDepth;
     _ignoreAttributes        = ignoreAttributes;
     _ignorePropertiesOrPaths = ignorePropertiesOrPaths;
     _typeRegistry            = ConvertToTypeRegistry(typeRegistry);
     _objectReferences        = new Dictionary <ushort, object>();
     _customSerializers       = new Dictionary <Type, Lazy <ICustomSerializer> >
     {
         { typeof(Point), new Lazy <ICustomSerializer>(() => new PointSerializer()) },
         { typeof(Enum), new Lazy <ICustomSerializer>(() => new EnumSerializer()) },
         { typeof(XDocument), new Lazy <ICustomSerializer>(() => new XDocumentSerializer()) },
     };
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Read the parent object, and recursively process it's children
        /// </summary>
        /// <param name="reader"></param>
        /// <param name="typeSupport">The type of the root object</param>
        /// <param name="maxDepth">The max depth tree to process</param>
        /// <param name="options">The serialization options</param>
        /// <param name="objectTree">Tracks the tree that has been traversed</param>
        /// <param name="ignoreAttributes">Properties/Fields with these attributes will be ignored from processing</param>
        /// <param name="typeRegistry">A registry that contains custom type mappings</param>
        /// <returns></returns>
        internal static object Read(BinaryReader reader, ExtendedType typeSupport, uint maxDepth, SerializerOptions options, ICollection <object> ignoreAttributes, SerializationTypeRegistry typeRegistry = null, ICollection <string> ignorePropertiesOrPaths = null)
        {
            var  currentDepth = 0;
            uint dataLength   = 0;
            uint headerLength = 0;

            var dataReader = reader;
            // read in byte 0, the data settings
            var dataSettings = (SerializerDataSettings)reader.ReadByte();

            if (dataSettings.BitwiseHasFlag(SerializerDataSettings.Compress))
            {
                // decompress the stream
                dataReader = Decompress(reader);
            }

            var typeReader = new TypeReader(dataSettings, options, maxDepth, ignoreAttributes, typeRegistry, ignorePropertiesOrPaths);

            return(typeReader.ReadObject(dataReader, typeSupport, currentDepth, string.Empty, ref dataLength, ref headerLength));
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Deserialize an object from a byte array
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="bytes"></param>
 /// <param name="typeRegistry">A list of type mappings</param>
 /// <returns></returns>
 public object Deserialize(Type type, byte[] bytes, SerializationTypeRegistry typeRegistry)
 {
     return(Deserialize(type, bytes, SerializerOptions.None, typeRegistry));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Deserialize an object from a byte array
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="bytes"></param>
 /// <param name="typeRegistry">A list of type mappings</param>
 /// <returns></returns>
 public T Deserialize <T>(byte[] bytes, SerializationTypeRegistry typeRegistry)
 {
     return(Deserialize <T>(bytes, SerializerOptions.None, typeRegistry));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Deserialize an object of type <typeparamref name="T"/>
 /// </summary>
 /// <typeparam name="T">The type to deserialize</typeparam>
 /// <param name="bytes"></param>
 /// <param name="options">The serializer options</param>
 /// <param name="typeRegistry">A list of type mappings</param>
 /// <returns>Deserlized <typeparamref name="T"/></returns>
 public static T Deserialize <T>(byte[] bytes, SerializerOptions options, SerializationTypeRegistry typeRegistry)
 {
     return(Extensions.SerializerExtensions.Deserialize <T>(bytes, options, typeRegistry));
 }