Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/DictionarySerializer.cs @ 4783

Last change on this file since 4783 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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    public int Priority {
36      get { return 100; }
37    }
38
39
40    public bool CanSerialize(Type type) {
41      return ReflectionTools.HasDefaultConstructor(type) &&
42        type.GetInterface(typeof(IDictionary).FullName) != null;
43    }
44
45    public string JustifyRejection(Type type) {
46      if (!ReflectionTools.HasDefaultConstructor(type))
47        return "no default constructor";
48      return "interface IDictionary not implemented";
49    }
50
51    public IEnumerable<Tag> CreateMetaInfo(object o) {
52      return new Tag[] { };
53    }
54
55    public IEnumerable<Tag> Decompose(object o) {
56      IDictionary dict = (IDictionary)o;
57      foreach (DictionaryEntry entry in dict) {
58        yield return new Tag("key", entry.Key);
59        yield return new Tag("value", entry.Value);
60      }
61    }
62
63    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
64      return Activator.CreateInstance(t, true);
65    }
66
67    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
68      IDictionary dict = (IDictionary)instance;
69      IEnumerator<Tag> iter = o.GetEnumerator();
70      try {
71        while (iter.MoveNext()) {
72          Tag key = iter.Current;
73          iter.MoveNext();
74          Tag value = iter.Current;
75          dict.Add(key.Value, value.Value);
76        }
77      }
78      catch (InvalidOperationException e) {
79        throw new PersistenceException("Dictionaries must contain an even number of elements (key+value).", e);
80      }
81      catch (NotSupportedException e) {
82        throw new PersistenceException("The serialized dictionary type was read-only or had a fixed size and cannot be deserialized.", e);
83      }
84      catch (ArgumentNullException e) {
85        throw new PersistenceException("Dictionary key was null.", e);
86      }
87      catch (ArgumentException e) {
88        throw new PersistenceException("Duplicate dictionary key.", e);
89      }
90    }
91  }
92
93}
Note: See TracBrowser for help on using the repository browser.