Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11359 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

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