4

I am playing a bit with yaml and YamlDotNet

But I have a problem deserializing some it seems very easy to do.

This is my yaml file:

---
# Folders to secure (with recursive content)
folders2Secure: 
 - .git
 - .vs

folders2Delete: 
 - packages
 - obj
 - bin
 - TestResults
 - node_modules
...

Related C# object:

public class FolderPreferences
{
    public List<string> Folders2Secure { get; set; }
    public List<string> Folders2Delete { get; set; }
}

With this code to get it deserialized:

public class ConfigurationReader
{
    public FolderPreferences Read(string configurationFile)
    {
        var input = new StringReader(configurationFile);
        var deserializerBuilder = new DeserializerBuilder().WithNamingConvention(new CamelCaseNamingConvention());

        var deserializer = deserializerBuilder.Build();

        var result = deserializer.Deserialize<FolderPreferences>(input);
        return result;
    }
}

And I have this error/stack race:

YamlDotNet.Core.YamlException was unhandled
  HResult=-2146233088
  Message=(Line: 1, Col: 1, Idx: 0) - (Line: 1, Col: 19, Idx: 18): Exception during deserialization
  Source=YamlDotNet
  StackTrace:
       at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer) in C:\projects\yamldotnet\YamlDotNet\Serialization\ValueDeserializers\NodeValueDeserializer.cs:line 75
       at YamlDotNet.Serialization.ValueDeserializers.AliasValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer) in C:\projects\yamldotnet\YamlDotNet\Serialization\ValueDeserializers\AliasValueDeserializer.cs:line 134
       at YamlDotNet.Serialization.Deserializer.Deserialize(IParser parser, Type type) in C:\projects\yamldotnet\YamlDotNet\Serialization\Deserializer.cs:line 315
       at YamlDotNet.Serialization.Deserializer.Deserialize[T](TextReader input) in C:\projects\yamldotnet\YamlDotNet\Serialization\Deserializer.cs:line 257
       at Ebys.CleanFolders.Library.ConfigurationReader.Read[T](String configurationFile) in E:\WorkingGit\bitbucket\CleanFolders\Ebys.CleanFolders.Library\ConfigurationReader.cs:line 21
       at Ebys.CleanFolders.ConsoleUi.Program.Main(String[] args) in E:\WorkingGit\bitbucket\CleanFolders\Ebys.CleanFolders\Program.cs:line 27
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
       HResult=-2147467262
       Message=Invalid cast from 'System.String' to 'Ebys.CleanFolders.Library.FolderPreferences'.
       Source=mscorlib
       StackTrace:
            at System.Convert.DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
            at System.String.System.IConvertible.ToType(Type type, IFormatProvider provider)
            at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
            at YamlDotNet.Serialization.Utilities.TypeConverter.ChangeType(Object value, Type destinationType, CultureInfo culture) in C:\projects\yamldotnet\YamlDotNet\Serialization\Utilities\TypeConverter.cs:line 128
            at YamlDotNet.Serialization.NodeDeserializers.ScalarNodeDeserializer.YamlDotNet.Serialization.INodeDeserializer.Deserialize(IParser parser, Type expectedType, Func`3 nestedObjectDeserializer, Object& value) in C:\projects\yamldotnet\YamlDotNet\Serialization\NodeDeserializers\ScalarNodeDeserializer.cs:line 96
            at YamlDotNet.Serialization.ValueDeserializers.NodeValueDeserializer.DeserializeValue(IParser parser, Type expectedType, SerializerState state, IValueDeserializer nestedObjectDeserializer) in C:\projects\yamldotnet\YamlDotNet\Serialization\ValueDeserializers\NodeValueDeserializer.cs:line 60
       InnerException: 

Any idea what I am doing wrong ?

Edited

The file in Hexa view inside GVim.

enter image description here

ferpega
  • 3,182
  • 7
  • 45
  • 65

2 Answers2

1

The problem is that you are creating a StringReader, which is a reader for the string that you specify, not a reader for a file. You need to use File.OpenText or a similar API to read from the file:

public FolderPreferences Read(string configurationFile)
{
    using (var input = File.OpenText(configurationFile))
    {
        var deserializerBuilder = new DeserializerBuilder().WithNamingConvention(new CamelCaseNamingConvention());

        var deserializer = deserializerBuilder.Build();

        var result = deserializer.Deserialize<FolderPreferences>(input);
        return result;
    }
}
Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74
0

I would guess that there is extra content at the beginning of your file that gets interpreted as an initial document containing a single string, hence the exception. Check the following fiddle, which uses your code and works as expected:

https://dotnetfiddle.net/tilGHT

Then compare with this one, where additional text has been added before the start of the document:

https://dotnetfiddle.net/d72Jut

You can try removing the initial ---, as it is optional anyways.

Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74
  • I have edited the question to add a hexa-image of the yaml file. In spite it seems to have a few characters at the beginning, I have tried to clean up this characters with no luck. – ferpega Nov 22 '16 at 08:25
  • Can you upload the file somewhere and provide a link? – Antoine Aubry Nov 22 '16 at 14:00
  • I have put the file here: https://expirebox.com/download/4479aa8830f4426f9582670e5d3b12cc.html – ferpega Nov 22 '16 at 21:57