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
RevLine 
[3742]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;
[1454]23using System.Collections;
[4068]24using System.Collections.Generic;
25using HeuristicLab.Persistence.Auxiliary;
[1454]26using HeuristicLab.Persistence.Core;
[4068]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[1454]28using HeuristicLab.Persistence.Interfaces;
29
[1823]30namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[1566]31
[3017]32  [StorableClass]
[3036]33  internal sealed class DictionarySerializer : ICompositeSerializer {
[1454]34
[1539]35    public int Priority {
36      get { return 100; }
37    }
38
39
[1823]40    public bool CanSerialize(Type type) {
[1705]41      return ReflectionTools.HasDefaultConstructor(type) &&
42        type.GetInterface(typeof(IDictionary).FullName) != null;
[1454]43    }
44
[2993]45    public string JustifyRejection(Type type) {
46      if (!ReflectionTools.HasDefaultConstructor(type))
47        return "no default constructor";
48      return "interface IDictionary not implemented";
49    }
50
[1553]51    public IEnumerable<Tag> CreateMetaInfo(object o) {
52      return new Tag[] { };
53    }
54
[1542]55    public IEnumerable<Tag> Decompose(object o) {
[1566]56      IDictionary dict = (IDictionary)o;
57      foreach (DictionaryEntry entry in dict) {
[1454]58        yield return new Tag("key", entry.Key);
59        yield return new Tag("value", entry.Value);
60      }
61    }
62
[1553]63    public object CreateInstance(Type t, IEnumerable<Tag> metaInfo) {
[1454]64      return Activator.CreateInstance(t, true);
65    }
66
[1553]67    public void Populate(object instance, IEnumerable<Tag> o, Type t) {
[1454]68      IDictionary dict = (IDictionary)instance;
69      IEnumerator<Tag> iter = o.GetEnumerator();
[1625]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        }
[4068]77      }
78      catch (InvalidOperationException e) {
[1625]79        throw new PersistenceException("Dictionaries must contain an even number of elements (key+value).", e);
[4068]80      }
81      catch (NotSupportedException e) {
[1625]82        throw new PersistenceException("The serialized dictionary type was read-only or had a fixed size and cannot be deserialized.", e);
[4068]83      }
84      catch (ArgumentNullException e) {
[1625]85        throw new PersistenceException("Dictionary key was null.", e);
[4068]86      }
87      catch (ArgumentException e) {
[1625]88        throw new PersistenceException("Duplicate dictionary key.", e);
[1705]89      }
[1454]90    }
91  }
92
93}
Note: See TracBrowser for help on using the repository browser.