Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
PackageVersionSerializer.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using SharpYaml;
5 using SharpYaml.Events;
6 using SharpYaml.Serialization;
7 using SiliconStudio.Core;
8 using SiliconStudio.Core.Yaml;
9 
10 namespace SiliconStudio.Assets.Serializers
11 {
12  /// <summary>
13  /// A Yaml serializer for <see cref="PackageVersion"/>
14  /// </summary>
15  [YamlSerializerFactory]
16  internal class PackageVersionSerializer : AssetScalarSerializerBase
17  {
18  public override bool CanVisit(Type type)
19  {
20  return typeof(PackageVersion).IsAssignableFrom(type);
21  }
22 
23  public override object ConvertFrom(ref ObjectContext context, Scalar fromScalar)
24  {
25  PackageVersion packageVersion;
26  if (!PackageVersion.TryParse(fromScalar.Value, out packageVersion))
27  {
28  throw new YamlException(fromScalar.Start, fromScalar.End, "Invalid version format. Unable to decode [{0}]".ToFormat(fromScalar.Value));
29  }
30  return packageVersion;
31  }
32 
33  public override string ConvertTo(ref ObjectContext objectContext)
34  {
35  return objectContext.Instance.ToString();
36  }
37  }
38 }