Free cookie consent management tool by TermsFeed Policy Generator

Changeset 12638


Ignore:
Timestamp:
07/07/15 13:02:06 (9 years ago)
Author:
ascheibe
Message:

#2368

  • created enum for distinguishing between zip and gzip
  • shrank interface of XmlParser by merging methods
Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r12455 r12638  
    398398    /// <param name="obj">The object.</param>
    399399    /// <param name="stream">The stream.</param>
    400     /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param>
    401     public static void Serialize(object obj, Stream stream, bool useZip = false) {
    402       Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), useZip);
     400    /// <param name="compressionType">Type of compression, default is GZip.</param>
     401    public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip) {
     402      Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType);
    403403    }
    404404
     
    410410    /// <param name="stream">The stream.</param>
    411411    /// <param name="config">The configuration.</param>
    412     /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param>
    413     public static void Serialize(object obj, Stream stream, Configuration config, bool useZip = false) {
    414       Serialize(obj, stream, config, false, useZip);
     412    /// <param name="compressionType">Type of compression, default is GZip.</param>
     413    public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip) {
     414      Serialize(obj, stream, config, false, compressionType);
    415415    }
    416416
     
    422422    /// <param name="config">The configuration.</param>
    423423    /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param>
    424     /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param>
    425     public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, bool useZip = false) {
     424    /// <param name="compressionType">Type of compression, default is GZip.</param>
     425    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies,
     426                                 CompressionType compressionType = CompressionType.GZip) {
    426427      try {
    427428        Serializer serializer = new Serializer(obj, config);
    428         if (useZip) {
     429        if (compressionType == CompressionType.Zip) {
    429430          Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal);
    430431        } else {
     
    448449    /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param>
    449450    /// <param name="types">The list of all serialized types.</param>
    450     /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param>
    451     public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types, bool useZip = false) {
     451    /// <param name="compressionType">Type of compression, default is GZip.</param>
     452    public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types,
     453                                 CompressionType compressionType = CompressionType.GZip) {
    452454      try {
    453455        Serializer serializer = new Serializer(obj, config);
    454         if (useZip) {
     456        if (compressionType == CompressionType.Zip) {
    455457          Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer);
    456458        } else {
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r12456 r12638  
    3131
    3232namespace HeuristicLab.Persistence.Default.Xml {
     33  /// <summary>
     34  /// Type of compression used for the Xml stream or file.
     35  /// </summary>
     36  public enum CompressionType {
     37    GZip,
     38    Zip
     39  }
    3340
    3441  /// <summary>
     
    216223    }
    217224
    218 
    219     /// <summary>
    220     /// Deserializes an object from the specified stream using GZip compression.
    221     /// </summary>
    222     /// <param name="stream">The stream.</param>
    223     /// <returns>A fresh object instance.</returns>
    224     private static object DeserializeWithGZip(Stream stream) {
    225       try {
    226         using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {
    227           XmlParser parser = new XmlParser(reader);
    228           Deserializer deserializer = new Deserializer(new TypeMapping[] { });
    229           return deserializer.Deserialize(parser);
    230         }
    231       }
    232       catch (PersistenceException) {
    233         throw;
    234       }
    235       catch (Exception x) {
    236         throw new PersistenceException("Unexpected exception during deserialization", x);
    237       }
    238     }
    239 
    240     /// <summary>
    241     /// Deserializes an object from the specified stream using Zip compression.
    242     /// </summary>
    243     /// <param name="stream">The stream.</param>
    244     /// <returns>A fresh object instance.</returns>
    245     private static object DeserializeWithZip(Stream stream) {
    246       ZipArchive zipFile = new ZipArchive(stream);
    247       return Deserialize(zipFile);
    248     }
    249 
    250225    /// <summary>
    251226    /// Deserializes an object from the specified stream.
     
    253228    /// <typeparam name="T">object type expected from the serialized stream</typeparam>
    254229    /// <param name="stream">The stream.</param>
    255     /// <param name="useZip">If true, uses zip for decompression, otherwise gzip.</param>
     230    /// <param name="compressionType">Type of compression, default is GZip.</param>
    256231    /// <returns>A fresh object instance.</returns>
    257     public static T Deserialize<T>(Stream stream, bool useZip = false) {
    258       return (T)Deserialize(stream, useZip);
     232    public static T Deserialize<T>(Stream stream, CompressionType compressionType = CompressionType.GZip) {
     233      return (T)Deserialize(stream, compressionType);
    259234    }
    260235
     
    263238    /// </summary>
    264239    /// <param name="stream">The stream.</param>
    265     /// <param name="useZip">If true, uses zip for decompression, otherwise gzip.</param>
     240    /// <param name="compressionType">Type of compression, default is GZip.</param>
    266241    /// <returns>A fresh object instance.</returns>
    267     public static object Deserialize(Stream stream, bool useZip = false) {
    268       if (useZip) {
    269         return DeserializeWithZip(stream);
     242    public static object Deserialize(Stream stream, CompressionType compressionType = CompressionType.GZip) {
     243      if (compressionType == CompressionType.Zip) {
     244        ZipArchive zipFile = new ZipArchive(stream);
     245        return Deserialize(zipFile);
    270246      } else {
    271         return DeserializeWithGZip(stream);
     247        try {
     248          using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {
     249            XmlParser parser = new XmlParser(reader);
     250            Deserializer deserializer = new Deserializer(new TypeMapping[] { });
     251            return deserializer.Deserialize(parser);
     252          }
     253        }
     254        catch (PersistenceException) {
     255          throw;
     256        }
     257        catch (Exception x) {
     258          throw new PersistenceException("Unexpected exception during deserialization", x);
     259        }
    272260      }
    273261    }
Note: See TracChangeset for help on using the changeset viewer.