Skip to content

Commit 0a6b643

Browse files
committed
Always deserialize with type converters
1 parent ebb69aa commit 0a6b643

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

Configuration/ConfigurationManager.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
using NFive.SDK.Core.Plugins;
44
using System;
55
using System.IO;
6-
using YamlDotNet.Serialization;
7-
using YamlDotNet.Serialization.NamingConventions;
86

97
namespace NFive.SDK.Plugins.Configuration
108
{
@@ -51,12 +49,7 @@ public static object Load(string path, Type type)
5149

5250
if (!File.Exists(path)) InitializeType(path, type);
5351

54-
var deserializer = new DeserializerBuilder()
55-
.WithNamingConvention(new UnderscoredNamingConvention())
56-
//.IgnoreUnmatchedProperties()
57-
.Build();
58-
59-
return deserializer.Deserialize(File.ReadAllText(path), type);
52+
return Yaml.Deserialize(File.ReadAllText(path), type);
6053
}
6154

6255
/// <summary>
@@ -111,16 +104,8 @@ public static void InitializeType(string file, Type type)
111104
// Create new instance of type
112105
var configuration = (IControllerConfiguration)Activator.CreateInstance(type);
113106

114-
// Serialize configuration
115-
var yml = new SerializerBuilder()
116-
.WithNamingConvention(new UnderscoredNamingConvention())
117-
.EmitDefaults()
118-
.WithTypeInspector(i => new PluginTypeInspector(i))
119-
.Build()
120-
.Serialize(configuration);
121-
122107
// Write Yaml to file
123-
File.WriteAllText(file, yml);
108+
File.WriteAllText(file, Yaml.Serialize(configuration));
124109
}
125110
}
126111
}

Configuration/Yaml.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using YamlDotNet.Serialization;
23
using YamlDotNet.Serialization.NamingConventions;
34

@@ -22,17 +23,34 @@ public static string Serialize(object obj)
2223
.WithTypeConverter(new TimeSpanConverter())
2324
.WithTypeConverter(new VersionConverter())
2425
.WithTypeConverter(new VersionRangeConverter())
26+
//.EmitDefaults()
2527
.Build()
2628
.Serialize(obj);
2729
}
2830

2931
/// <summary>
3032
/// Deserializes the specified Yaml.
3133
/// </summary>
32-
/// <typeparam name="T">Type to deserialize as.</typeparam>
34+
/// <typeparam name="T">The type to deserialize as.</typeparam>
3335
/// <param name="yml">The Yaml string.</param>
3436
/// <returns>The deserialized object.</returns>
3537
public static T Deserialize<T>(string yml)
38+
{
39+
return Deserializer().Deserialize<T>(yml);
40+
}
41+
42+
/// <summary>
43+
/// Deserializes the specified Yaml.
44+
/// </summary>
45+
/// <param name="yml">The Yaml string.</param>
46+
/// <param name="type">The type to deserialize as.</param>
47+
/// <returns>The deserialized object.</returns>
48+
public static object Deserialize(string yml, Type type)
49+
{
50+
return Deserializer().Deserialize(yml, type);
51+
}
52+
53+
private static IDeserializer Deserializer()
3654
{
3755
return new DeserializerBuilder()
3856
.WithNamingConvention(new UnderscoredNamingConvention())
@@ -42,8 +60,7 @@ public static T Deserialize<T>(string yml)
4260
.WithTypeConverter(new TimeSpanConverter())
4361
.WithTypeConverter(new VersionConverter())
4462
.WithTypeConverter(new VersionRangeConverter())
45-
.Build()
46-
.Deserialize<T>(yml);
63+
.Build();
4764
}
4865
}
4966
}

0 commit comments

Comments
 (0)