Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleList2XmlSerializer.cs @ 3913

Last change on this file since 3913 was 3913, checked in by epitzer, 14 years ago

Accelerate persistence: (#646)

  • dynamically compile field and property accessors
  • enable caching of compiled field and property accessors
  • delay costly custom attribute retrieval by checking signatures first
  • implement custom List<double> serializer
  • log deserialization time span
File size: 2.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.Collections;
23using System.Collections.Generic;
24using System;
25using System.Linq;
26using HeuristicLab.Persistence.Core;
27using HeuristicLab.Persistence.Default.Xml.Primitive;
28using System.Text;
29
30namespace HeuristicLab.Persistence.Default.Xml.Compact {
31
32  internal sealed class DoubleList2XmlSerializer : CompactXmlSerializerBase<List<double>> {
33
34    private static readonly char[] separators = new char[] { ';' };
35
36    public override XmlString Format(List<double> list) {
37      StringBuilder sb = new StringBuilder();
38      foreach (var d in list) {
39        sb.Append(Double2XmlSerializer.FormatG17(d)).Append(';');
40      }
41      return new XmlString(sb.ToString());
42    }
43
44    public override List<double> Parse(XmlString data) {
45      try {
46        var values = data.Data.Split(separators, StringSplitOptions.RemoveEmptyEntries);
47        List<double> list = new List<double>(values.Length);
48        foreach (var value in values) {
49          list.Add(Double2XmlSerializer.ParseG17(value));
50        }
51        return list;
52      } catch (InvalidCastException e) {
53        throw new PersistenceException("Invalid element data during reconstruction of List<double>.", e);
54      } catch (OverflowException e) {
55        throw new PersistenceException("Overflow during element parsing while trying to reconstruct List<double>.", e);
56      }
57    }
58  }
59}
Note: See TracBrowser for help on using the repository browser.