Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/20/09 11:42:53 (15 years ago)
Author:
epitzer
Message:

Reuse serializer instances for different types and provide better error information. (#603)

File:
1 edited

Legend:

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

    r1823 r1859  
    6868
    6969    private Dictionary<Type, object> CreateSerializers(IEnumerable<TypeMapping> typeCache) {
     70      Dictionary<Type, object> serializerInstances = new Dictionary<Type, object>();
    7071      try {
    7172        var map = new Dictionary<Type, object>();
     
    7475          typeIds.Add(typeMapping.Id, type);
    7576          Type serializerType = TypeLoader.Load(typeMapping.Serializer);
    76           map.Add(type, Activator.CreateInstance(serializerType, true));
     77          object serializer;
     78          if (serializerInstances.ContainsKey(serializerType))
     79            serializer = serializerInstances[serializerType];
     80          else
     81            serializer = Activator.CreateInstance(serializerType, true);
     82          map.Add(type, serializer);
    7783        }
    7884        return map;
     
    8288        throw new PersistenceException(
    8389          "The serialization type cache could not be loaded.\r\n" +
    84           "This usualy happens when you are missing an Assembly/Plugin.", e);
     90          "This usualy happens when you are missing an Assembly or Plugin.", e);
    8591      }
    8692    }
     
    113119    private void CompositeStartHandler(BeginToken token) {
    114120      Type type = typeIds[(int)token.TypeId];
    115       ICompositeSerializer compositeSerializer = null;
    116       if (serializerMapping.ContainsKey(type))
    117         compositeSerializer = serializerMapping[type] as ICompositeSerializer;
    118       if (compositeSerializer == null)
    119         throw new PersistenceException(String.Format(
    120           "No suitable method for deserialization of type \"{0}\" found.",
    121           type.VersionInvariantName()));
    122       parentStack.Push(new Midwife(type, compositeSerializer, token.Id));
     121      try {
     122        parentStack.Push(new Midwife(type, (ICompositeSerializer)serializerMapping[type], token.Id));
     123      } catch (Exception e) {
     124        if (e is InvalidCastException || e is KeyNotFoundException) {
     125          throw new PersistenceException(String.Format(
     126            "Invalid composite serializer configuration for type \"{0}\".",
     127            type.AssemblyQualifiedName), e);
     128        } else {
     129          throw new PersistenceException(String.Format(
     130            "Unexpected exception while trying to compose object of type \"{0}\".",
     131            type.AssemblyQualifiedName), e);
     132        }
     133      }
    123134    }
    124135
     
    134145    private void PrimitiveHandler(PrimitiveToken token) {
    135146      Type type = typeIds[(int)token.TypeId];
    136       object value = ((IPrimitiveSerializer)serializerMapping[type]).Parse(token.SerialData);
    137       if (token.Id != null)
    138         id2obj[(int)token.Id] = value;
    139       SetValue(token.Name, value);
     147      try {
     148        object value = ((IPrimitiveSerializer)serializerMapping[type]).Parse(token.SerialData);
     149        if (token.Id != null)
     150          id2obj[(int)token.Id] = value;
     151        SetValue(token.Name, value);
     152      } catch (Exception e) {
     153        if (e is InvalidCastException || e is KeyNotFoundException) {
     154          throw new PersistenceException(String.Format(
     155            "Invalid primitive serializer configuration for type \"{0}\".",
     156            type.AssemblyQualifiedName), e);
     157        } else {
     158          throw new PersistenceException(String.Format(
     159            "Unexpected exception while trying to parse object of type \"{0}\".",
     160            type.AssemblyQualifiedName), e);
     161        }
     162      }
    140163    }
    141164
Note: See TracChangeset for help on using the changeset viewer.