Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2993


Ignore:
Timestamp:
03/10/10 16:37:52 (15 years ago)
Author:
epitzer
Message:

add justifications for rejecting a type for serialization in ICompositeSerializer (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/Serializer.cs

    r2737 r2993  
    137137        if (compositeSerializer != null)
    138138          return CompositeEnumerator(accessor.Name, compositeSerializer.Decompose(value), id, typeId, compositeSerializer.CreateMetaInfo(value));
    139         throw new PersistenceException(
    140             String.Format(
    141             "No suitable method for serializing values of type \"{0}\" found\r\n" +
    142             "primitive serializers:\r\n{1}\r\n" +
    143             "composite serializers:\r\n{2}",
    144             value.GetType().VersionInvariantName(),
    145             string.Join("\r\n", configuration.PrimitiveSerializers.Select(f => f.GetType().VersionInvariantName()).ToArray()),
    146             string.Join("\r\n", configuration.CompositeSerializers.Select(d => d.GetType().VersionInvariantName()).ToArray())
    147             ));
     139        throw CreatePersistenceException(type);
    148140      } catch (Exception x) {
    149141        if (isTestRun) {
     
    154146        }
    155147      }
     148    }
     149
     150    private PersistenceException CreatePersistenceException(Type type) {
     151      StringBuilder sb = new StringBuilder();
     152      sb.Append("Could not determine how to serialize a value of type \"")
     153        .Append(type.VersionInvariantName())
     154        .AppendLine("\"");
     155      sb.AppendLine("No registered primitive serializer for this type:");
     156      foreach (var ps in configuration.PrimitiveSerializers)
     157        sb.Append(ps.SourceType.VersionInvariantName())
     158          .Append(" ---- (")
     159          .Append(ps.GetType().VersionInvariantName())
     160          .AppendLine(")");         
     161      sb.AppendLine("Rejected by all composite serializers:");
     162      foreach (var cs in configuration.CompositeSerializers)
     163        sb.Append("\"")
     164          .Append(cs.JustifyRejection(type))
     165          .Append("\" ---- (")
     166          .Append(cs.GetType().VersionInvariantName())
     167          .AppendLine(")");
     168      return new PersistenceException(sb.ToString());             
    156169    }
    157170
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/ArraySerializer.cs

    r1823 r2993  
    44using System.Collections.Generic;
    55using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     6using System.Text;
    67
    78namespace HeuristicLab.Persistence.Default.CompositeSerializers {
     
    1617    public bool CanSerialize(Type type) {
    1718      return type.IsArray || type == typeof(Array);
     19    }
     20
     21    public string JustifyRejection(Type type) {
     22      return "not an array and not of type System.Array";
    1823    }
    1924
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs

    r1823 r2993  
    2424        (type.IsArray || type == typeof(Array)) &&
    2525        numberConverter.CanSerialize(type.GetElementType());
     26    }
     27
     28    public string JustifyRejection(Type type) {
     29      if (!type.IsArray && type != typeof(Array))
     30        return "not an array";
     31      return string.Format("number converter cannot serialize elements: " +
     32        numberConverter.JustifyRejection(type.GetElementType()));
    2633    }
    2734
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/DictionarySerializer.cs

    r1823 r2993  
    2020      return ReflectionTools.HasDefaultConstructor(type) &&
    2121        type.GetInterface(typeof(IDictionary).FullName) != null;
     22    }
     23
     24    public string JustifyRejection(Type type) {
     25      if (!ReflectionTools.HasDefaultConstructor(type))
     26        return "no default constructor";
     27      return "interface IDictionary not implemented";
    2228    }
    2329
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumSerializer.cs

    r1823 r2993  
    1616    public bool CanSerialize(Type type) {
    1717      return type.IsEnum || type == typeof(Enum);
     18    }
     19
     20    public string JustifyRejection(Type type) {
     21      return "not an enum and not System.Enum";
    1822    }
    1923
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/EnumerableSerializer.cs

    r1823 r2993  
    2626    }
    2727
     28    public string JustifyRejection(Type type) {
     29      if (!ReflectionTools.HasDefaultConstructor(type))
     30        return "no default constructor";
     31      if (type.GetInterface(typeof(IEnumerable).FullName) == null)
     32        return "interface IEnumerable not implemented";
     33      if (type.GetMethod("Add") == null)
     34        return "no 'Add()' method";     
     35      return "no 'Add()' method with one argument";
     36    }
     37
    2838    public IEnumerable<Tag> CreateMetaInfo(object o) {
    2939      return new Tag[] { };
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/KeyValuePairSerializer.cs

    r1823 r2993  
    1616    }
    1717
     18    private static readonly Type genericKeyValuePairType =
     19      typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
    1820
    1921    public bool CanSerialize(Type type) {
    2022      return type.IsGenericType &&
    21              type.GetGenericTypeDefinition() ==
    22              typeof(KeyValuePair<int, int>).GetGenericTypeDefinition();
     23             type.GetGenericTypeDefinition() == genericKeyValuePairType;             
     24    }
     25
     26    public string JustifyRejection(Type type) {
     27      if (!type.IsGenericType)
     28        return "not even generic";     
     29      return "not generic KeyValuePair<,>";
    2330    }
    2431
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Number2StringSerializer.cs

    r1853 r2993  
    11using System;
     2using System.Linq;
    23using HeuristicLab.Persistence.Interfaces;
    34using HeuristicLab.Persistence.Core;
     
    4344    public bool CanSerialize(Type type) {
    4445      return numberParsers.ContainsKey(type);
     46    }
     47
     48    public string JustifyRejection(Type type) {
     49      return string.Format("not a number type (one of {0})",
     50        string.Join(", ", numberTypes.Select(n => n.Name).ToArray()));
    4551    }
    4652
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/NumberEnumerable2StringSerializer.cs

    r1823 r2993  
    6060    }
    6161
     62    public string JustifyRejection(Type type) {
     63      if (!ReflectionTools.HasDefaultConstructor(type))
     64        return "no default constructor";
     65      if (!ImplementsGenericEnumerable(type))
     66        return "IEnumerable<> not implemented";
     67      return "no Add method with one parameter";
     68    }
     69
    6270    public IEnumerable<Tag> CreateMetaInfo(object o) {
    6371      return new Tag[] { };
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/StackSerializer.cs

    r1823 r2993  
    2222        type.IsGenericType &&
    2323        type.GetGenericTypeDefinition() == typeof(Stack<>);
     24    }
     25
     26    public string JustifyRejection(Type type) {
     27        return "not Stack or generic Stack<>";
    2428    }
    2529
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r2991 r2993  
    1717
    1818    public bool CanSerialize(Type type) {
    19       if (!ReflectionTools.HasDefaultConstructor(type))
     19      if (!ReflectionTools.HasDefaultConstructor(type) &&
     20        StorableConstructorAttribute.GetStorableConstructor(type) == null)
    2021        return false;
    2122      while (type != null) {
     
    2627      }
    2728      return true;
     29    }
     30
     31    public string JustifyRejection(Type type) {
     32      if (!ReflectionTools.HasDefaultConstructor(type) &&
     33        StorableConstructorAttribute.GetStorableConstructor(type) == null)
     34        return "no default constructor and no storable constructor";
     35      while (type != null) {
     36        if (StorableAttribute.GetStorableMembers(type, false).Count() == 0 &&
     37            !EmptyStorableClassAttribute.IsEmptyStorable(type))
     38          return string.Format("{0} has no storable members and is not marked [EmtpyStorableClass]",
     39            type);
     40        type = type.BaseType;
     41      }
     42      return "no reason";
    2843    }
    2944
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/StructSerializer.cs

    r2939 r2993  
    1919    public bool CanSerialize(Type type) {
    2020      return type.IsValueType && !type.IsPrimitive && !type.IsEnum && type.IsSealed;     
     21    }
     22
     23    public string JustifyRejection(Type type) {
     24      if (!type.IsValueType)
     25        return "not a value type";
     26      if (type.IsPrimitive)
     27        return "type is primitive (int, float, ...)";
     28      if (type.IsEnum)
     29        return "type is enum";
     30      return "type is not sealed";
    2131    }
    2232
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/TypeSerializer.cs

    r1843 r2993  
    1818      return type == typeof(Type) ||
    1919             type.VersionInvariantName() == "System.RuntimeType, mscorlib";
     20    }
     21
     22    public string JustifyRejection(Type type) {
     23      return "not System.Type nor System.RuntimeType";
    2024    }
    2125
  • trunk/sources/HeuristicLab.Persistence/3.3/Interfaces/ICompositeSerializer.cs

    r1823 r2993  
    2020    /// </summary>   
    2121    bool CanSerialize(Type type);
     22
     23    /// <summary>
     24    /// Give a reason if possibly why the given type cannot be serialized by this
     25    /// ICompositeSerializer.
     26    /// </summary>
     27    string JustifyRejection(Type type);
    2228
    2329    /// <summary>
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r2990 r2993  
    736736    }
    737737
     738    [TestMethod]
     739    public void TestRejectionJustifications() {
     740      NonSerializable ns = new NonSerializable();
     741      try {
     742        XmlGenerator.Serialize(ns, tempFile);
     743        Assert.Fail("PersistenceException expected");
     744      } catch (PersistenceException x) {
     745        Assert.IsTrue(x.Message.Contains(new StorableSerializer().JustifyRejection(typeof(NonSerializable))));       
     746      }
     747    }
     748
    738749    [ClassInitialize]
    739750    public static void Initialize(TestContext testContext) {
Note: See TracChangeset for help on using the changeset viewer.