1 | using System;
|
---|
2 | using System.Text;
|
---|
3 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
4 |
|
---|
5 | namespace HeuristicLab.Persistence.Interfaces {
|
---|
6 |
|
---|
7 | /// <summary>
|
---|
8 | /// Base class for primitive serializers. These are serializers that map
|
---|
9 | /// directly to a single datatype and directly produce a serializable object.
|
---|
10 | /// </summary>
|
---|
11 | /// <typeparam name="Source">The source type.</typeparam>
|
---|
12 | /// <typeparam name="SerialData">The serialized type.</typeparam>
|
---|
13 | [StorableClass]
|
---|
14 | public abstract class PrimitiveSerializerBase<Source, SerialData> :
|
---|
15 | IPrimitiveSerializer<Source, SerialData>
|
---|
16 | where SerialData : ISerialData {
|
---|
17 |
|
---|
18 | /// <summary>
|
---|
19 | /// Formats the specified object.
|
---|
20 | /// </summary>
|
---|
21 | /// <param name="o">The object.</param>
|
---|
22 | /// <returns>A serialized version of the object.</returns>
|
---|
23 | public abstract SerialData Format(Source o);
|
---|
24 |
|
---|
25 | /// <summary>
|
---|
26 | /// Parses the specified serialized data back into an object.
|
---|
27 | /// </summary>
|
---|
28 | /// <param name="data">The serial data.</param>
|
---|
29 | /// <returns>
|
---|
30 | /// A newly created object representing the serialized data.
|
---|
31 | /// </returns>
|
---|
32 | public abstract Source Parse(SerialData data);
|
---|
33 |
|
---|
34 | /// <summary>
|
---|
35 | /// Gets the type of the serial data.
|
---|
36 | /// </summary>
|
---|
37 | /// <value>The type of the serial data.</value>
|
---|
38 | public Type SerialDataType { get { return typeof(SerialData); } }
|
---|
39 |
|
---|
40 | /// <summary>
|
---|
41 | /// Gets the type of the source.
|
---|
42 | /// </summary>
|
---|
43 | /// <value>The type of the source.</value>
|
---|
44 | public Type SourceType { get { return typeof(Source); } }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Formats the specified object.
|
---|
48 | /// </summary>
|
---|
49 | /// <param name="o">The object.</param>
|
---|
50 | /// <returns>A serialized version of the object.</returns>
|
---|
51 | ISerialData IPrimitiveSerializer.Format(object o) {
|
---|
52 | return Format((Source)o);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /// <summary>
|
---|
56 | /// Parses the specified serialized data back into an object.
|
---|
57 | /// </summary>
|
---|
58 | /// <param name="data">The serial data.</param>
|
---|
59 | /// <returns>A newly created object representing the serialized data.</returns>
|
---|
60 | object IPrimitiveSerializer.Parse(ISerialData data) {
|
---|
61 | return Parse((SerialData)data);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /// <summary>
|
---|
65 | /// Returns a <see cref="System.String"/> that represents this instance.
|
---|
66 | /// </summary>
|
---|
67 | /// <returns>
|
---|
68 | /// A <see cref="System.String"/> that represents this instance.
|
---|
69 | /// </returns>
|
---|
70 | public override string ToString() {
|
---|
71 | return new StringBuilder()
|
---|
72 | .Append(this.GetType().Name)
|
---|
73 | .Append('(')
|
---|
74 | .Append(SourceType.Name)
|
---|
75 | .Append("->")
|
---|
76 | .Append(SerialDataType.Name)
|
---|
77 | .ToString();
|
---|
78 | }
|
---|
79 |
|
---|
80 | }
|
---|
81 |
|
---|
82 | } |
---|