Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17197 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 3.3 KB
RevLine 
[3742]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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;
[16565]27using HEAL.Attic;
[1454]28using HeuristicLab.Persistence.Interfaces;
29
[1823]30namespace HeuristicLab.Persistence.Default.CompositeSerializers {
[1566]31
[16571]32  [StorableType("D66B7E71-1399-41F0-A97B-E2A2A0C87D71")]
[3036]33  internal sealed class DictionarySerializer : ICompositeSerializer {
[1454]34
[4806]35    [StorableConstructor]
[16565]36    private DictionarySerializer(StorableConstructorFlag _) { }
[4806]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.