Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/DictionarySerializer.cs @ 12417

Last change on this file since 12417 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using HeuristicLab.Persistence.Auxiliary;
26using HeuristicLab.Persistence.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Persistence.Interfaces;
29
30namespace HeuristicLab.Persistence.Default.CompositeSerializers {
31
32  [StorableClass]
33  internal sealed class DictionarySerializer : ICompositeSerializer {
34
35    [StorableConstructor]
36    private DictionarySerializer(bool deserializing) { }
37    public DictionarySerializer() { }
38
39    public int Priority {
40      get { return 100; }
41    }
42
43
44    public bool CanSerialize(Type type) {
45      return ReflectionTools.HasDefaultConstructor(type) &&
46        type.GetInterface(typeof(IDictionary).FullName) != null;
47    }
48
49    public string JustifyRejection(Type type) {
50      if (!ReflectionTools.HasDefaultConstructor(type))
51        return "no default constructor";
52      return "interface IDictionary not implemented";
53    }
54
55    public IEnumerable<Tag> CreateMetaInfo(object o) {
56      return new Tag[] { };
57    }
58
59    public IEnumerable<Tag> Decompose(object o) {
60      IDictionary dict = (IDictionary)o;
61      foreach (DictionaryEntry entry in dict) {
62        yield return new Tag("key", entry.Key);
63        yield return new Tag("value", entry.Value);
64      }
65    }
66
67    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
68      return Activator.CreateInstance(t, true);
69    }
70
71    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
72      IDictionary dict = (IDictionary)instance;
73      IEnumerator<Tag> iter = o.GetEnumerator();
74      try {
75        while (iter.MoveNext()) {
76          Tag key = iter.Current;
77          iter.MoveNext();
78          Tag value = iter.Current;
79          dict.Add(key.Value, value.Value);
80        }
81      }
82      catch (InvalidOperationException e) {
83        throw new PersistenceException("Dictionaries must contain an even number of elements (key+value).", e);
84      }
85      catch (NotSupportedException e) {
86        throw new PersistenceException("The serialized dictionary type was read-only or had a fixed size and cannot be deserialized.", e);
87      }
88      catch (ArgumentNullException e) {
89        throw new PersistenceException("Dictionary key was null.", e);
90      }
91      catch (ArgumentException e) {
92        throw new PersistenceException("Duplicate dictionary key.", e);
93      }
94    }
95  }
96
97}
Note: See TracBrowser for help on using the repository browser.